KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > ListSet


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.collection;
23
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.AbstractSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 import org.jboss.util.NullArgumentException;
33
34 /**
35  * A thin wrapper around a <code>List</code> transforming it into a
36  * modifiable <code>Set</code>.
37  *
38  * @version <tt>$Revision: 1958 $</tt>
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  */

41 public class ListSet
42    extends AbstractSet JavaDoc
43    implements Set JavaDoc, Cloneable JavaDoc, Serializable JavaDoc
44 {
45    /** The <tt>List</tt> which will be used for element storage. */
46    protected final List JavaDoc list;
47
48    /**
49     * Construct a <tt>ListSet</tt>.
50     *
51     * @param list The <tt>List</tt> which will be used for element storage.
52     *
53     * @throws IllegalArgumentException List is <tt>null</tt> or contains
54     * duplicate entries.
55     */

56    public ListSet(final List JavaDoc list) {
57       if (list == null)
58          throw new NullArgumentException("list");
59
60       // make sure there are no duplicates
61
int size = list.size();
62       for (int i=0; i<size; i++) {
63          Object JavaDoc obj = list.get(i);
64          if (list.indexOf(obj) != list.lastIndexOf(obj)) {
65             throw new IllegalArgumentException JavaDoc
66                ("list contains duplicate entries");
67          }
68       }
69
70       this.list = list;
71    }
72
73    /**
74     * Construct a <tt>ListSet</tt> using an <tt>ArrayList</tt> for backing.
75     */

76    public ListSet() {
77       this(new ArrayList JavaDoc());
78    }
79
80    /**
81     * Construct a <tt>ListSet</tt> using an <tt>ArrayList</tt> for backing
82     * and populated with the given elements.
83     *
84     * @param elements The elements for the list.
85     */

86    public ListSet(final Collection JavaDoc elements) {
87       this(new ArrayList JavaDoc(elements));
88    }
89    
90    public List JavaDoc getList()
91    {
92       return list;
93    }
94    
95    /**
96     * Return the size of the set.
97     *
98     * @return The size of the set.
99     */

100    public int size() {
101       return list.size();
102    }
103
104    /**
105     * Return an iteration over the elements in the set.
106     *
107     * @return An iteration over the elements in the set.
108     */

109    public Iterator JavaDoc iterator() {
110       return list.iterator();
111    }
112
113    /**
114     * Add an element to the set.
115     *
116     * @param obj Element to add to the set.
117     * @return True if the element was added.
118     */

119    public boolean add(final Object JavaDoc obj) {
120       boolean added = false;
121
122       if (!list.contains(obj)) {
123          added = list.add(obj);
124       }
125
126       return added;
127    }
128
129    /**
130     * Returns <tt>true</tt> if this set contains no elements.
131     *
132     * @return <tt>true</tt> if this set contains no elements.
133     */

134    public boolean isEmpty() {
135       return list.isEmpty();
136    }
137
138    /**
139     * Returns <tt>true</tt> if this set contains the specified element.
140     *
141     * @param obj Element whose presence in this set is to be tested.
142     * @return <tt>true</tt> if this set contains the specified element.
143     */

144    public boolean contains(final Object JavaDoc obj) {
145       return list.contains(obj);
146    }
147
148    /**
149     * Removes the given element from this set if it is present.
150     *
151     * @param obj Object to be removed from this set, if present.
152     * @return <tt>true</tt> if the set contained the specified element.
153     */

154    public boolean remove(final Object JavaDoc obj) {
155       return list.remove(obj);
156    }
157
158    /**
159     * Removes all of the elements from this set.
160     */

161    public void clear() {
162       list.clear();
163    }
164
165    /**
166      * Returns a shallow copy of this <tt>ListSet</tt> instance.
167      *
168      * @return A shallow copy of this set.
169      */

170    public Object JavaDoc clone() {
171       try {
172          return super.clone();
173       }
174       catch (CloneNotSupportedException JavaDoc e) {
175          throw new InternalError JavaDoc();
176       }
177    }
178 }
179
Popular Tags