You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Graham Leggett <mi...@sharp.fm> on 2001/12/30 14:41:58 UTC

cloning an Enumeration - how?

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
-- 
-----------------------------------------
minfrin@sharp.fm		"There's a moon
					over Bourbon Street
						tonight..."

Re: cloning an Enumeration - how?

Posted by Richard Sand <rs...@vgalleries.com>.
This is a java question not a Tomcat question.

Iterate through the enumeration and add each object to a java.util.Vector.
Then use the vector as you see fit.  Or retrieve the ListIterator for the
vector.  A listiterator is like an enumeration but you can go both forwards
and backwards.

Best regards,

Richard

----- Original Message -----
From: "Graham Leggett" <mi...@sharp.fm>
To: "Tomcat User" <to...@jakarta.apache.org>
Sent: Sunday, December 30, 2001 2:41 PM
Subject: cloning an Enumeration - how?


> 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
> --
> -----------------------------------------
> minfrin@sharp.fm "There's a moon
> over Bourbon Street
> tonight..."


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: cloning an Enumeration - how?

Posted by Micael Padraig Og mac Grene <ca...@harbornet.com>.
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.

Re: cloning an Enumeration - how?

Posted by Micael Padraig Og mac Grene <ca...@harbornet.com>.
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

By the way, Graham, I have no idea whether you can or cannot clone the 
object you get, but the object you get is not an instance of Enumeration 
(even though it has the type of Enumeration) but is an instance of an 
anonymous inner-subclass of Enumeration.  So, your reason for not cloning 
it are not valid.  Maybe it is cloneable.  Have you tried?  Cannot see 
right off why it would not be cloneable.  Maybe you cannot because it 
cannot be casted.  However, did I not read something about there being a 
way around casting in j2sdk1.4.0-beta3?

Why don't you wrap the class and stick in your own inner class with a 
method giving an Enumeration that allows you to reset the cursor on the 
array so that you can reuse the Enumeration object?

Micael


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>