001: package xdoclet.modules.ojb;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.Iterator;
019: import java.util.StringTokenizer;
020:
021: /**
022: * Small helper class for dealing with comma-separated string lists.
023: *
024: * @author <a href="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
025: */
026: public class CommaListIterator implements Iterator {
027: /** The tokenizer for the comma-separated list */
028: private StringTokenizer _list;
029: /** The current token */
030: private String _current = "";
031:
032: /**
033: * Creates a new string iterator for the given comma-separated list.
034: *
035: * @param list The list
036: */
037: public CommaListIterator(String list) {
038: _list = new StringTokenizer(list == null ? "" : list, ",");
039: }
040:
041: /* (non-Javadoc)
042: * @see java.util.Iterator#remove()
043: */
044: public void remove() {
045: throw new UnsupportedOperationException();
046: }
047:
048: /* (non-Javadoc)
049: * @see java.util.Iterator#hasNext()
050: */
051: public boolean hasNext() {
052: while (_list.hasMoreTokens() && (_current.length() == 0)) {
053: _current = _list.nextToken();
054: }
055: return (_current.length() > 0);
056: }
057:
058: /* (non-Javadoc)
059: * @see java.util.Iterator#next()
060: */
061: public Object next() {
062: return getNext();
063: }
064:
065: /**
066: * Returns the next string from the list.
067: *
068: * @return The next string
069: */
070: public String getNext() {
071: String result = _current;
072:
073: _current = "";
074: return result.trim();
075: }
076:
077: /**
078: * Determines whether one of the remaining elements is the given element. If it is found
079: * then the iterator is moved after the element, otherwise the iterator is after the end
080: * of the list.
081: *
082: * @param element The element to search for
083: * @return <code>true</code> if the element has been found
084: */
085: public boolean contains(String element) {
086: while (hasNext()) {
087: if (element.equals(getNext())) {
088: return true;
089: }
090: }
091: return false;
092: }
093:
094: /**
095: * Compares this iterator with the other given one. Note that this consumes
096: * the iterators, so you should not use them afterwards.
097: *
098: * @param obj The other object
099: * @return If the other object is a comma list iterator and it delivers the same values
100: * as this iterator
101: * @see java.lang.Object#equals(java.lang.Object)
102: */
103: public boolean equals(Object obj) {
104: if (!(obj instanceof CommaListIterator)) {
105: return false;
106: }
107:
108: CommaListIterator otherIt = (CommaListIterator) obj;
109:
110: while (hasNext() || otherIt.hasNext()) {
111: if (!hasNext() || !otherIt.hasNext()) {
112: return false;
113: }
114: if (!getNext().equals(otherIt.getNext())) {
115: return false;
116: }
117: }
118: return true;
119: }
120:
121: /**
122: * Compares the two comma-separated lists.
123: *
124: * @param list1 The first list
125: * @param list2 The second list
126: * @return <code>true</code> if the lists are equal
127: */
128: public static boolean sameLists(String list1, String list2) {
129: return new CommaListIterator(list1)
130: .equals(new CommaListIterator(list2));
131: }
132: }
|