At 03:41 PM 12/30/01 +0200, you wrote:
>Hi all,
>
>Forgive what may be a stupid question:
>
>I am trying to retrieve all the locales from the Accept-Language header
>in a request. To do this I use the following call:
>
>Enumeration locales = pageContext.getRequest().getLocales();
>
>My problem is that once I have enumerated through this variable locales,
>there is no way I can see to "reset" it so I can enumerate through it
>again.
>
>I also cannot seems to find a way to clone this object, as Enumeration
>is not a child of java.lang.Object, but an interface.
>
>Can anyone shed some light on the "correct" way to enumerate through an
>Enumeration more than once?
>
>Regards,
>Graham

Take a look at the Enumeration interface, which has only the following 
methods:

         boolean hasMoreElements();
         Object nextElement();

Each component that wishes to use this interface can do so as it 
pleases.  Note that Vector uses an anonymous inner class in its elements() 
method as follows:
public Enumeration elements() {
    return new Enumeration() {
               int count = 0;

               public boolean hasMoreElements() {
                   return count < elementCount;
               }

               public Object nextElement() {
                   synchronized (Vector.this) {
                        if (count < elementCount) {
                        return elementData[count++];
                    }
                    }
                    throw new NoSuchElementException("Vector Enumeration");
                }
            };
}

So, what you have to do, if you are using Vector, to use the Enumeration 
again is to call elements() again.  You could roll your own, if you want 
more functionality.  Usually I do the opposite, e.g., roll my own to get 
less functionality.

Hope this helps.

Reply via email to