views:

258

answers:

3

Hey Folks,

Just wondering if there is a way to clone a NamingEnumeration in Java? My program searches an LDAP server for people and it can take a few seconds to complete. To get the number of results I am using the following:

NamingEnumeration results = null;
NamingEnumeration results2 = null;

results = ctx.search("", "("+searchAt+"=" +searchVal +")", controls);
results2 = result;
int i = 0;
while(results2.hasMore())
{
    results2.next();
    i++;
}

But since results2 is just a reference to result when i go through displaying the results results.hasMore() will always return false.

is there a way to clone 'results' without having to re-preform the search and assign it to 'results2'?

Thanks, -Pete

+4  A: 

There is no guarantee that if you redo the search you'll get the same answer back.

The simple solution is to copy the results into a List (ArrayList to be specific).

Tom Hawtin - tackline
thanks guys! fifteen characters
Petey B
I think Yishai's answer is better. Upvote Yishai!
Tom Hawtin - tackline
+3  A: 

In general if you need to make a copy of an Enumeration (or Iterator) you move the contents to a List. Most enumerations are not directly cloneable. You can create the list by hand or use the method Collections.list (which I missed for a moment there).

Kathy Van Stone
+2  A: 

Use Collections.list(results) to put the results in a list and then use List.size to get the count, and iterate over the list.

Yishai