001: /*
002: * 01/07/2003 - 15:19:32
003: *
004: * StateProSet.java -
005: * Copyright (C) 2003 Buero fuer Softwarearchitektur GbR
006: * ralf.meyer@karneim.com
007: * http://jrexx.sf.net
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
022: */
023: package com.tc.jrexx.set;
024:
025: public class StateProSet {
026:
027: public class Iterator {
028: final int offset;
029: Wrapper_State current = null;
030:
031: protected Iterator() {
032: this (0);
033: }
034:
035: protected Iterator(int offset) {
036: this .offset = offset;
037: }
038:
039: public IStatePro next() {
040: if (this .current == null) {
041: this .current = StateProSet.this .elements;
042: try {
043: for (int i = offset; i > 0; --i)
044: this .current = this .current.next;
045: } catch (NullPointerException e) {
046: if (this .current != null)
047: throw e; // else null is returned
048: }
049: // this.offset = 0;
050: } else
051: this .current = this .current.next;
052: return (this .current == null) ? null : this .current.state;
053: }
054: }
055:
056: protected static final class Wrapper_State {
057: public final IStatePro state;
058: public Wrapper_State next = null;
059:
060: protected Wrapper_State(IStatePro state) {
061: this .state = state;
062: }
063: }
064:
065: protected Wrapper_State elements = null;
066: protected Wrapper_State lastElement = null;
067:
068: transient int size = 0;
069:
070: public StateProSet() {
071: }
072:
073: public StateProSet(IStatePro state) {
074: this .add(state);
075: }
076:
077: public boolean add(IStatePro state) {
078: //if (state.getParent()!=this) throw new IllegalArgumentException("");
079: if (this .contains(state))
080: return false;
081:
082: if (this .lastElement == null) {
083: this .elements = new Wrapper_State(state);
084: this .lastElement = this .elements;
085: } else {
086: this .lastElement.next = new Wrapper_State(state);
087: this .lastElement = this .lastElement.next;
088: }
089: ++this .size;
090: return true;
091: }
092:
093: int addAll(StateProSet stateSet) {
094: int result = 0;
095: for (Wrapper_State wrapper = stateSet.elements; wrapper != null; wrapper = wrapper.next) {
096: if (this .add(wrapper.state))
097: ++result;
098: }
099: return result;
100: }
101:
102: public boolean remove(IStatePro state) {
103: if (this .contains(state) == false)
104: return false;
105:
106: Wrapper_State prev = null;
107: for (Wrapper_State wrapper = this .elements; wrapper != null; wrapper = wrapper.next) {
108: if (wrapper.state == state) {
109: if (prev == null)
110: this .elements = wrapper.next;
111: else
112: prev.next = wrapper.next;
113:
114: if (wrapper == this .lastElement)
115: this .lastElement = prev;
116: --this .size;
117: return true;
118: }
119: prev = wrapper;
120: }
121: return false;
122: }
123:
124: int removeAll(StateProSet stateSet) {
125: int answer = 0;
126: Wrapper_State prev = null;
127: for (Wrapper_State wrapper = this .elements; wrapper != null; wrapper = wrapper.next) {
128: if (stateSet.contains(wrapper.state)) {
129: if (prev == null)
130: this .elements = wrapper.next;
131: else
132: prev.next = wrapper.next;
133:
134: if (wrapper == this .lastElement)
135: this .lastElement = prev;
136: --this .size;
137: if (++answer == stateSet.size())
138: return answer;
139: }
140: prev = wrapper;
141: }
142: return answer;
143: }
144:
145: public boolean contains(IStatePro state) {
146: for (Wrapper_State wrapper = this .elements; wrapper != null; wrapper = wrapper.next) {
147: if (wrapper.state == state)
148: return true;
149: }
150: return false;
151: }
152:
153: public void clear() {
154: this .elements = null;
155: this .lastElement = null;
156: this .size = 0;
157: }
158:
159: public int size() {
160: return this .size;
161: }
162:
163: public boolean isEmpty() {
164: return this .size == 0;
165: }
166:
167: public StateProSet.Iterator iterator() {
168: return new Iterator();
169: }
170:
171: public StateProSet.Iterator iterator(int offset) {
172: return new Iterator(offset);
173: }
174:
175: public boolean equals(Object obj) {
176: if (this == obj)
177: return true;
178: if (obj == null)
179: return false;
180: if (obj.getClass() != this .getClass())
181: throw new ClassCastException("");
182:
183: final StateProSet set = (StateProSet) obj;
184: if (this .size != set.size)
185: return false;
186: for (Wrapper_State wrapper = set.elements; wrapper != null; wrapper = wrapper.next) {
187: if (this .contains(wrapper.state) == false)
188: return false;
189: }
190: return true;
191: }
192:
193: public int hashCode() {
194: long hash = 0;
195: for (Wrapper_State wrapper = this .elements; wrapper != null; wrapper = wrapper.next) {
196: hash = ((hash << 32) + wrapper.state.hashCode()) % 4294967291L;
197: }
198: return (int) hash;
199: }
200:
201: /*
202: public IStatePro[] toArray() {
203: }
204: public IStatePro[] toArray(IStatePro[] destination) {
205:
206: }
207: */
208: }
|