1 package org.apache.maven.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Set;
27
28 /**
29 * Set class that maintain insertion order (first wins). This is necessary because the only alternatives are
30 * LinkedHashSet (JDK 1.4), or SetUtils.orderedSet (commons-collections-3.0), both of which are problematic for
31 * Maven to require.
32 *
33 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34 * @version $Id: InsertionOrderedSet.java 517014 2007-03-11 21:15:50Z ltheussl $
35 */
36 public class InsertionOrderedSet
37 implements Set
38 {
39 private List order = new ArrayList();
40
41 private Set set = new HashSet();
42
43 public InsertionOrderedSet()
44 {
45 }
46
47 public InsertionOrderedSet( Collection c )
48 {
49 addAll( c );
50 }
51
52 public Iterator iterator()
53 {
54 return new InsertionOrderedSetIterator();
55 }
56
57 public boolean isEmpty()
58 {
59 return set.isEmpty();
60 }
61
62 public int size()
63 {
64 return set.size();
65 }
66
67 public void clear()
68 {
69 set.clear();
70 order.clear();
71 }
72
73 public boolean add( Object o )
74 {
75 if ( !set.contains( o ) )
76 {
77 set.add( o );
78 return order.add( o );
79 }
80 return false;
81 }
82
83 public boolean containsAll( Collection c )
84 {
85 return set.containsAll( c );
86 }
87
88 public boolean addAll( Collection c )
89 {
90 boolean retVal = false;
91
92 for ( Iterator i = c.iterator(); i.hasNext(); )
93 {
94 boolean result = add( i.next() );
95 retVal = retVal || result;
96 }
97 return retVal;
98 }
99
100 public boolean removeAll( Collection c )
101 {
102 order.removeAll( c );
103 return set.removeAll( c );
104 }
105
106 public boolean retainAll( Collection c )
107 {
108 order.retainAll( c );
109 return set.retainAll( c );
110 }
111
112 public boolean contains( Object o )
113 {
114 return set.contains( o );
115 }
116
117 public Object[] toArray()
118 {
119 return order.toArray();
120 }
121
122 public Object[] toArray( Object[] a )
123 {
124 return order.toArray( a );
125 }
126
127 public boolean remove( Object o )
128 {
129 if ( set.remove( o ) )
130 {
131 order.remove( o );
132 return true;
133 }
134 return false;
135 }
136
137 public String toString()
138 {
139 return order.toString();
140 }
141
142 public boolean equals( Object o )
143 {
144 return set.equals( o );
145 }
146
147 public int hashCode()
148 {
149 return set.hashCode();
150 }
151
152 private class InsertionOrderedSetIterator
153 implements Iterator
154 {
155 private Iterator i;
156
157 private Object current = null;
158
159 private InsertionOrderedSetIterator()
160 {
161 i = order.iterator();
162 }
163
164 public boolean hasNext()
165 {
166 return i.hasNext();
167 }
168
169 public Object next()
170 {
171 current = i.next();
172 return current;
173 }
174
175 public void remove()
176 {
177 i.remove();
178 set.remove( current );
179 }
180 }
181 }