001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.util;
051:
052: import java.util.*;
053: import java.io.*;
054:
055: /**
056: * This class is backed by an ArrayList. Features are
057: * 1) Ensure the Set functionality of unique elements(including a null) in this data structure
058: * 2) Iterate through the list in the order in which the entries were made
059: */
060: public class ListSet implements Set, Cloneable, Serializable {
061:
062: List m_set = null;
063:
064: /** Creates new ListSet */
065: public ListSet() {
066: m_set = new ArrayList();
067: }
068:
069: /** Creates new ListSet specifying the initial capacity.
070: * @param initialCapacity The initial capacity.
071: */
072: public ListSet(int initialCapacity) {
073: m_set = new ArrayList(initialCapacity);
074: }
075:
076: /** Creates new ListSet from an existing Collection.
077: * @param c An existing collection.
078: */
079: public ListSet(Collection c) {
080: m_set = new ArrayList();
081: if (c != null) {
082: for (Iterator itr = c.iterator(); itr.hasNext();) {
083: Object obj = itr.next();
084: if (!m_set.contains(obj))
085: m_set.add(obj);
086: }
087: }
088: }
089:
090: // *** Set Interface methods ***
091: /** Retains only the elements in this set that are contained in the specified collection.
092: * @param c collection that defines which elements this set will retain.
093: * @return true if this collection changed as a result of the call.
094: */
095: public boolean retainAll(Collection c) {
096: return m_set.retainAll(c);
097: }
098:
099: /** Returns true if this set contains the specified element.
100: * @param o element whose presence in this set is to be tested.
101: * @return true if this set contains the specified element.
102: */
103: public boolean contains(Object o) {
104: return m_set.contains(o);
105: }
106:
107: /** Returns an array containing all of the elements in this set.
108: * @return an array containing all of the elements in this set.
109: */
110: public Object[] toArray() {
111: return m_set.toArray();
112: }
113:
114: /** Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.
115: * @param a the array into which the elements of this set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
116: * @return an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array
117: */
118: public Object[] toArray(Object[] a) {
119: return m_set.toArray(a);
120: }
121:
122: /** Returns an iterator over the elements in this set.
123: * @return an iterator over the elements in this set.
124: */
125: public Iterator iterator() {
126: return m_set.iterator();
127: }
128:
129: /** Removes from this set all of its elements that are contained in the specified collection.
130: * @param c collection that defines which elements will be removed from this set.
131: * @return true if this set changed as a result of the call.
132: */
133: public boolean removeAll(Collection c) {
134: return m_set.removeAll(c);
135: }
136:
137: /** Removes the specified element from this set if it is present.
138: * @param o object to be removed from this set, if present.
139: * @return true if the set contained the specified element.
140: */
141: public boolean remove(Object o) {
142: return m_set.remove(o);
143: }
144:
145: /** Removes all of the elements from this set.*/
146: public void clear() {
147: m_set.clear();
148: }
149:
150: /** Returns the hash code value for this set.
151: * @return the hash code value for this set.
152: */
153: public int hashCode() {
154: return m_set.hashCode();
155: }
156:
157: /** Adds all of the elements in the specified collection to this set if they're not already present.
158: * @param c collection whose elements are to be added to this set.
159: * @return true if this set changed as a result of the call.
160: */
161: public boolean addAll(Collection c) {
162: boolean added = false;
163: if (c != null) {
164: for (Iterator itr = c.iterator(); itr.hasNext();) {
165: Object obj = itr.next();
166: if (!m_set.contains(obj)) {
167: m_set.add(obj);
168: added = true;
169: }
170: }
171: }
172: return added;
173: }
174:
175: /** Returns the number of elements in this set.
176: * @return the number of elements in this set.
177: */
178: public int size() {
179: return m_set.size();
180: }
181:
182: /** Returns true if this set contains all of the elements of the specified collection.
183: * @param c collection to be checked for containment in this set.
184: * @return true if this set contains all of the elements of the specified collection.
185: */
186: public boolean containsAll(Collection c) {
187: return m_set.containsAll(c);
188: }
189:
190: /** Adds the specified element to this set if it is not already present.
191: * @param o element to be added to this set.
192: * @return true if this set did not already contain the specified element.
193: */
194: public boolean add(Object o) {
195: boolean added = false;
196: if (!m_set.contains(o)) {
197: m_set.add(o);
198: added = true;
199: }
200: return added;
201: }
202:
203: /** Compares the specified object with this set for equality.
204: * @param o Object to be compared for equality with this set.
205: * @return true if the specified Object is equal to this set.
206: */
207: public boolean equals(Object o) {
208: return m_set.equals(o);
209: }
210:
211: /** Returns true if this set contains no elements.
212: * @return true if this set contains no elements.
213: */
214: public boolean isEmpty() {
215: return m_set.isEmpty();
216: }
217:
218: // *** Additional Methods ***
219: /** Adds the specified element to this set if it is not already present, at the specified index.
220: * @param index The position at which the element is to be added.
221: * @param o element to be added to this set.
222: */
223: public void add(int index, Object o) {
224: if (!m_set.contains(o))
225: m_set.add(index, o);
226: }
227:
228: /** Returns the element from the specified position.
229: * @param index The position from which the element is to be retrieved.
230: * @return the element from the specified position.
231: */
232: public Object get(int index) {
233: return m_set.get(index);
234: }
235:
236: /** Remove the element from the specified position.
237: * @param index The position from which the element is to be removed.
238: * @return the element being removed.
239: */
240: public Object remove(int index) {
241: return m_set.remove(index);
242: }
243:
244: /** Returns the index of the element in this set.
245: * @param o The element whose index is to be found.
246: * @return the index of the element in this set.
247: */
248: public int indexOf(Object o) {
249: return m_set.indexOf(o);
250: }
251:
252: // *** CLONEABLE INTERFACE METHODS ***
253: /** Returns a clone of the Set.
254: * @throws CloneNotSupportedException if cloning is not supported. Should never happen.
255: * @return a clone of the Set.
256: */
257: public Object clone() throws CloneNotSupportedException {
258: Object obj = super .clone();
259: if (m_set != null && m_set instanceof Cloneable)
260: ((ListSet) obj).m_set = (List) ((ArrayList) m_set).clone();
261: return obj;
262: }
263: }
|