KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > naming > DXNamingEnumeration


1 package com.ca.commons.naming;
2
3 import java.util.*;
4 import java.text.Collator JavaDoc;
5 import java.text.CollationKey JavaDoc;
6
7 import javax.naming.*;
8 //import javax.naming.directory.*;
9

10 /**
11  * A simple class that implements NamingEnumeration, used to
12  * bundle up search results 'n stuff. Used by not-really-jndi
13  * ftns to wrap things nicely for higher level ftns that are
14  * expecting NamingEnumerations.
15  *
16  * as usual, the enumeration can be enumerated using the model:
17  * <pre>
18  * while (myEnumeration.hasMoreElements())
19  * doSomethingWith(myEnumeration.nextElement();
20  * </pre>
21  * in addition, it supports the NamingEnumeration interface
22  * equivalent, which (<b>in the future</b>) may also throw a NamingException.
23  * <pre>
24  * try
25  * {
26  * while (myEnumeration.hasMore())
27  * doSomethingWith(myEnumeration.next();
28  * }
29  * catch (NamingException e) { }
30  * </pre>
31  *
32  * As a spot of convenience evil, it implements a sort() ftn as well...
33  * (Since it's not a 'dynamic' enumeration as a normal naming enumeration
34  * would be, but uses a vector base object), and also allows the enumeration
35  * to be dumped out as a vector or a string array :-) .
36  */

37  
38 public class DXNamingEnumeration implements NamingEnumeration
39 {
40     private int pointer = 0;
41     private ArrayList data;
42
43         // get a single platform specific language collator for use in sorting.
44
private static Collator JavaDoc myCollator = Collator.getInstance();
45
46     /**
47      * <p>A quickie class for ordering stuff using their intrinsic toString() methods...
48      * ... not necessarily the fastest because it requires many, many 'toString()'
49      * calls. Preferable is to implement 'Comparable' interface in the object and
50      * use the 'fastSort()' method.</p>
51      */

52
53     private class SimpleComparator implements Comparator
54     {
55         public int compare(Object JavaDoc o1, Object JavaDoc o2)
56         {
57             return myCollator.compare(o1.toString(), o2.toString());
58         }
59     }
60
61
62 /*
63     class SimpleComparator implements Comparable
64     {
65         Object myObject;
66         String compareString;
67
68         public SimpleComparator(Object o)
69         {
70             myObject = (o==null)?"null":o;
71
72             if (myObject instanceof DXAttribute)
73                 compareString = ((DXAttribute)myObject).getID().toLowerCase();
74             else
75                 compareString = myObject.toString().toLowerCase();
76         }
77
78         public int compareTo(Object compObject)
79         {
80             return compareString.compareTo(compObject.toString());
81         }
82
83         public String toString() { return compareString; }
84
85         public Object getObject() {return myObject; }
86     }
87 */

88
89                                 // with this if needed in future...
90
/**
91      * The constructor does nothing except initialise class variables.
92      */

93     public DXNamingEnumeration() { data = new ArrayList(); }
94
95     /**
96      * A quicky to wrap normal enumerations with as well...
97      */

98     public DXNamingEnumeration(Enumeration ne)
99     {
100         data = new ArrayList();
101         while (ne.hasMoreElements())
102             add(ne.nextElement());
103     }
104
105
106
107     /**
108      * The constructor takes another NamingEnumeration and uses
109      * it to initialise with.
110      */

111      
112     public DXNamingEnumeration(NamingEnumeration ne)
113     {
114         data = new ArrayList();
115         if (ne!=null)
116             while (ne.hasMoreElements())
117                 add(ne.nextElement());
118     }
119
120     /**
121      * A convenience constructor to wrap an existing ArrayList.
122      * Note that since the enumeration is supposed to be read-only,
123      * the initialising ArrayList is <i>not</i> cloned, but used as is.
124      */

125
126     public DXNamingEnumeration(ArrayList listData)
127     {
128         data = listData;
129     }
130
131     /**
132      * Adds an object to the enumeration.
133      * @param o object to be added.
134      */

135     public void add(Object JavaDoc o) { data.add(o); }
136
137     /**
138      * Removes an object from the enumeration.
139      * @param o the object to be removed.
140      */

141     public void remove(Object JavaDoc o) { data.remove(o); }
142     
143     
144     /**
145      * Enumerations can't usually be re-used. Sometimes it would be
146      * nice though... this resets the enumeration so you can reread it,
147      * which is useful for debugging (i.e. you can print it before use...)
148      */

149     public void reset() { pointer = 0; }
150     
151     /**
152      * Not really necessary, this returns the number of elements in the
153      * enumeration.
154      * @return number of objects in enumeration
155      */

156     public int size() { return data.size(); }
157     
158     /*
159      * identical in ftn to hasMoreElements(). In future, this may
160      * throw NamingEnumerationExceptions.
161      * @return true if more elements are available.
162      */

163     public boolean hasMore() { return (pointer < data.size()); }
164     
165     /*
166      * standard enumeration ftn.
167      * @return true if more elements are available
168      */

169     public boolean hasMoreElements() { return hasMore(); }
170         
171     /*
172      * identical in ftn to nextElement().
173      *
174      * @return returns the next element in the enumeration.
175      */

176     public Object JavaDoc next() throws NoSuchElementException
177     {
178         try
179         { return data.get(pointer++); }
180         catch (ArrayIndexOutOfBoundsException JavaDoc e)
181         { throw new NoSuchElementException(); }
182     }
183     
184     /*
185      * standard enumeration ftn.
186      *
187      * @return returns the next element in the enumeration.
188      */

189     public Object JavaDoc nextElement() throws NoSuchElementException { return next(); }
190     
191     
192     /**
193      * <p>This method attempts to order the components of the
194      * SimpleEnumeration using their intrinsic 'toString()'
195      * methods sorted by a language sensitive collator
196      * (this may be meaningless for some components).</p>
197      */

198      
199     public DXNamingEnumeration sort()
200     {
201         Collections.sort(data, new SimpleComparator());
202
203         return this;
204     }
205
206     /**
207      * <p>This sorts a DXNamingEnumeration that contains an
208      * enumeration of objects that implement the Comparable interface
209      * (such as DXAttribute).</p>
210      * @return
211      */

212
213     public DXNamingEnumeration fastSort()
214     {
215         Collections.sort(data);
216
217         return this;
218     }
219
220     /**
221      * A simple existance test against the core ArrayList list of
222      * objects.
223      */

224       
225     public boolean contains(Object JavaDoc test)
226     {
227         return data.contains(test);
228     }
229     
230     /**
231     * Included for Naming Enumeration compatibility... does nothing,
232     * 'cause DXNamingEnumeration isn't really an enumeration, and
233     * has already slurped all the data... :-)
234     */

235     public void close() {;}
236     
237     public String JavaDoc toString() // mainly used for debugging
238
{
239         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
240         for (int i=0; i<data.size(); i++)
241         {
242             Object JavaDoc o = data.get(i);
243             ret.append((o==null)?"null":o.toString() + "\n");
244         }
245         return ret.toString();
246     }
247     
248     public Object JavaDoc[] toArray()
249     {
250         return data.toArray();
251     }
252      
253     
254     public String JavaDoc[] toStringArray()
255     {
256         String JavaDoc[] ret = new String JavaDoc[data.size()];
257         for (int i=0; i<data.size(); i++)
258         {
259             Object JavaDoc o = data.get(i);
260             ret[i] = ((o==null)?null:o.toString());
261         }
262         return ret;
263     }
264  
265     public ArrayList getArrayList() { return data; }
266 }
Popular Tags