KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > CommaListIterator


1 package xdoclet.modules.ojb;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.util.Iterator JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 /**
22  * Small helper class for dealing with comma-separated string lists.
23  *
24  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
25  */

26 public class CommaListIterator implements Iterator JavaDoc
27 {
28     /** The tokenizer for the comma-separated list */
29     private StringTokenizer JavaDoc _list;
30     /** The current token */
31     private String JavaDoc _current = "";
32
33     /**
34      * Creates a new string iterator for the given comma-separated list.
35      *
36      * @param list The list
37      */

38     public CommaListIterator(String JavaDoc list)
39     {
40         _list = new StringTokenizer JavaDoc(list == null ? "" : list, ",");
41     }
42
43     /* (non-Javadoc)
44      * @see java.util.Iterator#remove()
45      */

46     public void remove()
47     {
48         throw new UnsupportedOperationException JavaDoc();
49     }
50
51     /* (non-Javadoc)
52      * @see java.util.Iterator#hasNext()
53      */

54     public boolean hasNext()
55     {
56         while (_list.hasMoreTokens() && (_current.length() == 0))
57         {
58             _current = _list.nextToken();
59         }
60         return (_current.length() > 0);
61     }
62
63     /* (non-Javadoc)
64      * @see java.util.Iterator#next()
65      */

66     public Object JavaDoc next()
67     {
68         return getNext();
69     }
70
71     /**
72      * Returns the next string from the list.
73      *
74      * @return The next string
75      */

76     public String JavaDoc getNext()
77     {
78         String JavaDoc result = _current;
79
80         _current = "";
81         return result.trim();
82     }
83
84     /**
85      * Determines whether one of the remaining elements is the given element. If it is found
86      * then the iterator is moved after the element, otherwise the iterator is after the end
87      * of the list.
88      *
89      * @param element The element to search for
90      * @return <code>true</code> if the element has been found
91      */

92     public boolean contains(String JavaDoc element)
93     {
94         while (hasNext())
95         {
96             if (element.equals(getNext()))
97             {
98                 return true;
99             }
100         }
101         return false;
102     }
103
104     /**
105      * Compares this iterator with the other given one. Note that this consumes
106      * the iterators, so you should not use them afterwards.
107      *
108      * @param obj The other object
109      * @return If the other object is a comma list iterator and it delivers the same values
110      * as this iterator
111      * @see java.lang.Object#equals(java.lang.Object)
112      */

113     public boolean equals(Object JavaDoc obj)
114     {
115         if (!(obj instanceof CommaListIterator))
116         {
117             return false;
118         }
119
120         CommaListIterator otherIt = (CommaListIterator)obj;
121
122         while (hasNext() || otherIt.hasNext())
123         {
124             if (!hasNext() || !otherIt.hasNext())
125             {
126                 return false;
127             }
128             if (!getNext().equals(otherIt.getNext()))
129             {
130                 return false;
131             }
132         }
133         return true;
134     }
135
136     /**
137      * Compares the two comma-separated lists.
138      *
139      * @param list1 The first list
140      * @param list2 The second list
141      * @return <code>true</code> if the lists are equal
142      */

143     public static boolean sameLists(String JavaDoc list1, String JavaDoc list2)
144     {
145         return new CommaListIterator(list1).equals(new CommaListIterator(list2));
146     }
147 }
148
Popular Tags