1 23 package com.tc.jrexx.set; 24 25 public class StateProSet { 26 27 public class Iterator { 28 final int offset; 29 Wrapper_State current = null; 30 31 protected Iterator() { 32 this(0); 33 } 34 protected Iterator(int offset) { 35 this.offset = offset; 36 } 37 38 public IStatePro next() { 39 if (this.current==null) { 40 this.current = StateProSet.this.elements; 41 try { 42 for (int i=offset; i>0; --i) this.current = this.current.next; 43 } catch(NullPointerException e) { 44 if (this.current!=null) throw e; } 46 } else this.current = this.current.next; 48 return (this.current==null) ? null : this.current.state; 49 } 50 } 51 52 protected static final class Wrapper_State { 53 public final IStatePro state; 54 public Wrapper_State next=null; 55 56 protected Wrapper_State(IStatePro state) { 57 this.state = state; 58 } 59 } 60 61 protected Wrapper_State elements = null; 62 protected Wrapper_State lastElement = null; 63 64 transient int size = 0; 65 66 public StateProSet(){} 67 68 public StateProSet(IStatePro state) { 69 this.add(state); 70 } 71 72 public boolean add(IStatePro state) { 73 if (this.contains(state)) return false; 75 76 if (this.lastElement==null) { 77 this.elements = new Wrapper_State(state); 78 this.lastElement = this.elements; 79 } else { 80 this.lastElement.next = new Wrapper_State(state); 81 this.lastElement = this.lastElement.next; 82 } 83 ++this.size; 84 return true; 85 } 86 87 int addAll(StateProSet stateSet) { 88 int result = 0; 89 for (Wrapper_State wrapper=stateSet.elements; wrapper!=null; wrapper=wrapper.next) { 90 if (this.add(wrapper.state)) ++result; 91 } 92 return result; 93 } 94 95 public boolean remove(IStatePro state) { 96 if (this.contains(state)==false) return false; 97 98 Wrapper_State prev = null; 99 for (Wrapper_State wrapper=this.elements; wrapper!=null; wrapper=wrapper.next) { 100 if (wrapper.state==state) { 101 if (prev==null) this.elements = wrapper.next; 102 else prev.next = wrapper.next; 103 104 if (wrapper==this.lastElement) this.lastElement = prev; 105 --this.size; 106 return true; 107 } 108 prev = wrapper; 109 } 110 return false; 111 } 112 113 int removeAll(StateProSet stateSet) { 114 int answer = 0; 115 Wrapper_State prev = null; 116 for (Wrapper_State wrapper=this.elements; wrapper!=null; wrapper=wrapper.next) { 117 if (stateSet.contains(wrapper.state)) { 118 if (prev==null) this.elements = wrapper.next; 119 else prev.next = wrapper.next; 120 121 if (wrapper==this.lastElement) this.lastElement = prev; 122 --this.size; 123 if (++answer==stateSet.size()) return answer; 124 } 125 prev = wrapper; 126 } 127 return answer; 128 } 129 130 131 public boolean contains(IStatePro state) { 132 for (Wrapper_State wrapper=this.elements; wrapper!=null; wrapper=wrapper.next) { 133 if (wrapper.state==state) return true; 134 } 135 return false; 136 } 137 138 public void clear() { 139 this.elements = null; 140 this.lastElement = null; 141 this.size = 0; 142 } 143 144 public int size() { 145 return this.size; 146 } 147 148 public boolean isEmpty() { 149 return this.size==0; 150 } 151 152 public StateProSet.Iterator iterator() { 153 return new Iterator(); 154 } 155 156 public StateProSet.Iterator iterator(int offset) { 157 return new Iterator(offset); 158 } 159 160 public boolean equals(Object obj) { 161 if (this==obj) return true; 162 if (obj==null) return false; 163 if (obj.getClass()!=this.getClass()) throw new ClassCastException (""); 164 165 final StateProSet set = (StateProSet)obj; 166 if (this.size!=set.size) return false; 167 for (Wrapper_State wrapper=set.elements; wrapper!=null; wrapper=wrapper.next) { 168 if (this.contains(wrapper.state)==false) return false; 169 } 170 return true; 171 } 172 173 public int hashCode() { 174 long hash = 0; 175 for (Wrapper_State wrapper=this.elements; wrapper!=null; wrapper=wrapper.next) { 176 hash = ( (hash<<32) + wrapper.state.hashCode() ) % 4294967291L; 177 } 178 return (int)hash; 179 } 180 181 188 } | Popular Tags |