01: /* SimpleSet Copyright (C) 1998-2002 Jochen Hoenicke.
02: *
03: * This program is free software; you can redistribute it and/or modify
04: * it under the terms of the GNU Lesser General Public License as published by
05: * the Free Software Foundation; either version 2, or (at your option)
06: * any later version.
07: *
08: * This program is distributed in the hope that it will be useful,
09: * 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:
22: import java.util.AbstractSet;
23: import java.util.Iterator;
24:
25: public class SimpleSet extends AbstractSet implements Cloneable {
26: Object[] elementObjects;
27: int count = 0;
28:
29: public SimpleSet() {
30: this (2);
31: }
32:
33: public SimpleSet(int initialSize) {
34: elementObjects = new Object[initialSize];
35: }
36:
37: public int size() {
38: return count;
39: }
40:
41: public boolean add(Object element) {
42: if (element == null)
43: throw new NullPointerException();
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[] newArray = new Object[(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 clone() {
60: try {
61: SimpleSet other = (SimpleSet) super .clone();
62: other.elementObjects = (Object[]) elementObjects.clone();
63: return other;
64: } catch (CloneNotSupportedException ex) {
65: throw new jode.AssertError("Clone?");
66: }
67: }
68:
69: public Iterator iterator() {
70: return new Iterator() {
71: int pos = 0;
72:
73: public boolean hasNext() {
74: return pos < count;
75: }
76:
77: public Object 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: }
|