KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > ListSet


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Sep 3, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27
28 public class ListSet implements List, Set {
29     private final List list = new ArrayList();
30     private final Set set = new HashSet();
31     
32     public ListSet() {
33         super();
34     }
35
36     /* (non-Javadoc)
37      * @see java.util.List#add(int, E)
38      */

39     public void add(int index, Object JavaDoc element) {
40         if(this.set.add(element)) {
41             list.add(index, element);
42         }
43     }
44
45     /* (non-Javadoc)
46      * @see java.util.List#add(E)
47      */

48     public boolean add(Object JavaDoc o) {
49         if(this.set.add(o)) {
50             return this.list.add(o);
51         }
52         else {
53             return false;
54         }
55     }
56
57     /* (non-Javadoc)
58      * @see java.util.List#addAll(java.util.Collection)
59      */

60     public boolean addAll(Collection c) {
61         boolean changed = false;
62         Iterator i = c.iterator();
63         while(i.hasNext()) {
64             Object JavaDoc element = i.next();
65             if(this.add(element)) {
66                 changed = true;
67             }
68         }
69         return changed;
70     }
71
72     /* (non-Javadoc)
73      * @see java.util.List#addAll(int, java.util.Collection)
74      */

75     public boolean addAll(int index, Collection c) {
76         boolean changed = false;
77         int insertIndex = index;
78         Iterator i = c.iterator();
79         while(i.hasNext()) {
80             Object JavaDoc element = i.next();
81             if(this.set.add(element)) {
82                 this.list.add(insertIndex++, element);
83                 changed = true;
84             }
85         }
86         return changed;
87     }
88
89     /* (non-Javadoc)
90      * @see java.util.List#clear()
91      */

92     public void clear() {
93         this.set.clear();
94         this.list.clear();
95     }
96
97     /* (non-Javadoc)
98      * @see java.util.List#contains(java.lang.Object)
99      */

100     public boolean contains(Object JavaDoc o) {
101         return this.set.contains(o);
102     }
103
104     /* (non-Javadoc)
105      * @see java.util.List#containsAll(java.util.Collection)
106      */

107     public boolean containsAll(Collection c) {
108         return this.set.containsAll(c);
109     }
110
111     /* (non-Javadoc)
112      * @see java.util.List#get(int)
113      */

114     public Object JavaDoc get(int index) {
115         return this.list.get(index);
116     }
117
118     /* (non-Javadoc)
119      * @see java.util.List#indexOf(java.lang.Object)
120      */

121     public int indexOf(Object JavaDoc o) {
122         return this.list.indexOf(o);
123     }
124
125     /* (non-Javadoc)
126      * @see java.util.List#isEmpty()
127      */

128     public boolean isEmpty() {
129         return this.set.isEmpty();
130     }
131
132     /* (non-Javadoc)
133      * @see java.util.List#iterator()
134      */

135     public Iterator iterator() {
136         return this.list.iterator();
137     }
138
139     /* (non-Javadoc)
140      * @see java.util.List#lastIndexOf(java.lang.Object)
141      */

142     public int lastIndexOf(Object JavaDoc o) {
143         return this.list.lastIndexOf(o);
144     }
145
146     /* (non-Javadoc)
147      * @see java.util.List#listIterator()
148      */

149     public ListIterator listIterator() {
150         return this.list.listIterator();
151     }
152
153     /* (non-Javadoc)
154      * @see java.util.List#listIterator(int)
155      */

156     public ListIterator listIterator(int index) {
157         return this.list.listIterator(index);
158     }
159
160     /* (non-Javadoc)
161      * @see java.util.List#remove(int)
162      */

163     public Object JavaDoc remove(int index) {
164         Object JavaDoc element = this.list.remove(index);
165         if(element != null) {
166             this.set.remove(element);
167         }
168         return element;
169     }
170
171     /* (non-Javadoc)
172      * @see java.util.List#remove(java.lang.Object)
173      */

174     public boolean remove(Object JavaDoc o) {
175         if(this.set.remove(o)) {
176             this.list.remove(o);
177             return true;
178         }
179         else {
180             return false;
181         }
182     }
183
184     /* (non-Javadoc)
185      * @see java.util.List#removeAll(java.util.Collection)
186      */

187     public boolean removeAll(Collection c) {
188         if(this.set.removeAll(c)) {
189             this.list.removeAll(c);
190             return true;
191         }
192         else {
193             return false;
194         }
195     }
196
197     /* (non-Javadoc)
198      * @see java.util.List#retainAll(java.util.Collection)
199      */

200     public boolean retainAll(Collection c) {
201         if(this.set.retainAll(c)) {
202             this.list.retainAll(c);
203             return true;
204         }
205         else {
206             return false;
207         }
208     }
209
210     /* (non-Javadoc)
211      * @see java.util.List#set(int, E)
212      */

213     public Object JavaDoc set(int index, Object JavaDoc element) {
214         this.set.add(element);
215         return this.list.set(index, element);
216     }
217
218     /* (non-Javadoc)
219      * @see java.util.List#size()
220      */

221     public int size() {
222         return this.list.size();
223     }
224
225     /* (non-Javadoc)
226      * @see java.util.List#subList(int, int)
227      */

228     public List subList(int fromIndex, int toIndex) {
229         return this.list.subList(fromIndex, toIndex);
230     }
231
232     /* (non-Javadoc)
233      * @see java.util.List#toArray()
234      */

235     public Object JavaDoc[] toArray() {
236         return this.list.toArray();
237     }
238
239     /* (non-Javadoc)
240      * @see java.util.List#toArray(T[])
241      */

242     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
243         return this.list.toArray(a);
244     }
245     
246     public boolean equals(Object JavaDoc other) {
247         return other instanceof ListSet && this.list.equals(((ListSet) other).list);
248     }
249     
250     public int hashCode() {
251         return this.list.hashCode();
252     }
253 }
254
Popular Tags