1
2
3
4 package jwutil.collections;
5
6 import java.util.AbstractSet;
7 import java.util.Collection;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Set;
12
13 /***
14 * <code>LinearSet</code> is a simplistic light-weight
15 * <code>Set</code> designed for use when the number of entries is
16 * small. It is backed by a <code>List</code>.
17 *
18 * @author Felix S. Klock II <pnkfelix@mit.edu>
19 * @version $Id: LinearSet.java 2249 2005-04-29 02:32:27Z joewhaley $
20 */
21 public class LinearSet extends AbstractSet implements Cloneable,
22 java.io.Serializable {
23 /***
24 * Version ID for serialization.
25 */
26 private static final long serialVersionUID = 3256443586294919225L;
27
28 private List list;
29 private ListFactory lf;
30
31 /*** Creates a <code>LinearSet</code>. Uses an
32 <code>ArrayList</code> as the backing store.*/
33 public LinearSet() {
34 this(ListFactory.arrayListFactory);
35 }
36
37 /*** Creates a <code>LinearSet</code> with given capacity.
38 Uses an <code>ArrayList</code> as the backing store.
39 */
40 public LinearSet(final int capacity) {
41 this(ListFactory.arrayListFactory, capacity);
42 }
43
44 /*** Creates a <code>LinearSet</code>, filling it with the elements
45 of <code>set</code>. Uses an <code>ArrayList</code> as the
46 backing store.
47 */
48 public LinearSet(final Set set) {
49 this(ListFactory.arrayListFactory, set);
50 }
51
52 /*** Creates an empty <code>LinearSet</code>, using a
53 <code>List</code> generated by <code>lf</code> as the backing
54 store.
55 */
56 public LinearSet(final ListFactory lf) {
57 list = lf.makeList();
58 this.lf = lf;
59 }
60
61 /*** Creates an empty <code>LinearSet</code> with a given capacity,
62 using a <code>List</code> generated by <code>lf</code> as the
63 backing store.
64 */
65 public LinearSet(final ListFactory lf, int capacity) {
66 list = lf.makeList(capacity);
67 this.lf = lf;
68 }
69
70 /*** Creates an empty <code>LinearSet</code>, using a
71 <code>List</code> generated by <code>lf</code> as the backing
72 store, and fills it with the elements of <code>set</code>.
73 */
74 public LinearSet(final ListFactory lf, final Set set) {
75 list = lf.makeList(set);
76 this.lf = lf;
77 }
78
79 LinearSet(ListFactory lf, List list) {
80 this.list = list;
81 this.lf = lf;
82 }
83
84 public Iterator iterator() {
85 return list.iterator();
86 }
87
88 public int size() {
89 return list.size();
90 }
91
92 public boolean add(Object o) {
93 if (list.contains(o)) {
94 return false;
95 } else {
96 list.add(o);
97 return true;
98 }
99 }
100
101 public boolean addAll(Collection c) {
102 HashSet s = new HashSet(this.size() + c.size());
103 s.addAll(this);
104 boolean r = s.addAll(c);
105 this.list = lf.makeList(s);
106 return r;
107 }
108
109 public boolean remove(Object o) {
110 int index = list.indexOf(o);
111 if (index == -1) {
112 return false;
113 } else {
114 list.remove(index);
115 return true;
116 }
117 }
118
119 public void clear() {
120 list.clear();
121 }
122
123 public Object get(Object o) {
124 int index = list.indexOf(o);
125 if (index == -1) {
126 return null;
127
128 }
129 return list.get(index);
130 }
131
132 public Object clone() {
133 try{
134 LinearSet newset = (LinearSet) super.clone();
135 newset.list = lf.makeList(list);
136 return newset;
137 } catch(CloneNotSupportedException e) {
138 throw new InternalError();
139 }
140 }
141 }