KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > util > SimpleSet


1 /* SimpleSet Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: SimpleSet.java.in,v 1.1.2.1 2002/05/28 17:34:24 hoenicke Exp $
18  */

19
20 package jode.util;
21 import java.util.AbstractSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 public class SimpleSet extends AbstractSet JavaDoc implements Cloneable JavaDoc
25 {
26     Object JavaDoc[] elementObjects;
27     int count = 0;
28
29     public SimpleSet() {
30     this(2);
31     }
32
33     public SimpleSet(int initialSize) {
34     elementObjects = new Object JavaDoc[initialSize];
35     }
36
37     public int size() {
38     return count;
39     }
40
41     public boolean add(Object JavaDoc element) {
42     if (element == null)
43         throw new NullPointerException JavaDoc();
44
45     for (int i=0; i< count; i++) {
46         if (element.equals(elementObjects[i]))
47         return false;
48     }
49     
50     if (count == elementObjects.length) {
51             Object JavaDoc[] newArray = new Object JavaDoc[(count+1)*3/2];
52             System.arraycopy(elementObjects,0,newArray,0,count);
53             elementObjects = newArray;
54         }
55         elementObjects[count++] = element;
56     return true;
57     }
58     
59     public Object JavaDoc clone() {
60         try {
61             SimpleSet other = (SimpleSet) super.clone();
62         other.elementObjects = (Object JavaDoc[]) elementObjects.clone();
63             return other;
64         } catch (CloneNotSupportedException JavaDoc ex) {
65             throw new jode.AssertError("Clone?");
66         }
67     }
68
69     public Iterator JavaDoc iterator() {
70     return new Iterator JavaDoc() {
71         int pos = 0;
72
73         public boolean hasNext() {
74         return pos < count;
75         }
76         
77         public Object JavaDoc next() {
78         return elementObjects[pos++];
79         }
80       
81         public void remove() {
82         if (pos < count)
83             System.arraycopy(elementObjects, pos,
84                      elementObjects, pos-1, count - pos);
85         count--;
86         pos--;
87         }
88     };
89     }
90 }
91
Popular Tags