KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > set > BooleanDirectSet


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002 Søren Bak
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.set;
20
21 import bak.pcj.BooleanIterator;
22 import bak.pcj.BooleanCollection;
23 import bak.pcj.util.Exceptions;
24 import java.util.NoSuchElementException JavaDoc;
25
26 /**
27  * This class represents sets of boolean values. The elements of the
28  * set are represented by a single state variable:
29  * 0 -> {}, 1 -> {F}, 2 -> {T}, and 3 -> {F, T}.
30  *
31  * @author Søren Bak
32  * @version 1.3 21-08-2003 20:22
33  * @since 1.0
34  */

35 public class BooleanDirectSet extends AbstractBooleanSet {
36
37     private int values;
38
39     private static final int EMPTY = 0;
40     private static final int FALSE = 1;
41     private static final int TRUE = 2;
42     private static final int FALSETRUE = 3;
43
44     /**
45      * Creates a new empty set.
46      */

47     public BooleanDirectSet() {
48         values = EMPTY;
49     }
50
51     /**
52      * Creates a new set with the same values as a specified
53      * collection.
54      *
55      * @param c
56      * the collection whose elements to add to the
57      * new set.
58      *
59      * @throws NullPointerException
60      * if <tt>c</tt> is <tt>null</tt>.
61      */

62     public BooleanDirectSet(BooleanCollection c) {
63         this();
64         addAll(c);
65     }
66
67     private void removeError() {
68         Exceptions.noElementToRemove();
69     }
70
71     private void nextError() {
72         Exceptions.endOfIterator();
73     }
74
75     public boolean contains(boolean v) {
76         switch (values) {
77         case EMPTY: return false;
78         case FALSE: return !v;
79         case TRUE: return v;
80         case FALSETRUE: return true;
81         default: throw new RuntimeException JavaDoc("Internal error");
82         }
83     }
84
85     public boolean add(boolean v) {
86         switch (values) {
87         case EMPTY: if (v) values = TRUE; else values = FALSE; return true;
88         case FALSE: if (v) { values = FALSETRUE; return true; } return false;
89         case TRUE: if (!v) { values = FALSETRUE; return true; } return false;
90         case FALSETRUE: return false;
91         default: throw new RuntimeException JavaDoc("Internal error");
92         }
93     }
94
95     public boolean remove(boolean v) {
96         switch (values) {
97         case EMPTY: return false;
98         case FALSE: if (!v) { values=EMPTY; return true; } return false;
99         case TRUE: if (v) { values=EMPTY; return true; } return false;
100         case FALSETRUE: if (v) values=FALSE; else values=TRUE; return true;
101         default: throw new RuntimeException JavaDoc("Internal error");
102         }
103     }
104
105     public int size() {
106         switch (values) {
107         case EMPTY: return 0;
108         case FALSE: return 1;
109         case TRUE: return 1;
110         case FALSETRUE: return 2;
111         default: throw new RuntimeException JavaDoc("Internal error");
112         }
113     }
114
115     public boolean isEmpty()
116     { return values == EMPTY; }
117
118     public void clear()
119     { values = EMPTY; }
120
121     public BooleanIterator iterator() {
122         return new BIterator();
123     }
124
125     private class BIterator implements BooleanIterator {
126         private static final int I_EMPTY = 0;
127         private static final int I_F_0 = 1; // before F
128
private static final int I_F_1 = 2; // after F
129
private static final int I_F_2 = 3; // F removed
130
private static final int I_T_0 = 4; // before T
131
private static final int I_T_1 = 5; // after T
132
private static final int I_T_2 = 6; // T removed
133
private static final int I_FT_0 = 7; // before FT
134
private static final int I_FT_1 = 8; // between FT
135
private static final int I_FT_2 = 9; // between FT, F removed
136
private static final int I_FT_3 = 10; // after FT
137
private static final int I_FT_4 = 11; // after FT, T removed
138
private static final int I_FT_5 = 12; // after FT, F removed
139
private static final int I_FT_6 = 13; // after FT, FT removed
140

141         private int state;
142
143         BIterator() {
144             switch (values) {
145             case EMPTY: state = I_EMPTY; break;
146             case FALSE: state = I_F_0; break;
147             case TRUE: state = I_T_0; break;
148             case FALSETRUE: state = I_FT_0; break;
149             default: throw new RuntimeException JavaDoc("Internal error");
150             }
151         }
152
153         public boolean hasNext() {
154             switch (state) {
155             case I_EMPTY: return false;
156             case I_F_0: return true;
157             case I_F_1: return false;
158             case I_F_2: return false;
159             case I_T_0: return true;
160             case I_T_1: return false;
161             case I_T_2: return false;
162             case I_FT_0: return true;
163             case I_FT_1: return true;
164             case I_FT_2: return true;
165             case I_FT_3: return false;
166             case I_FT_4: return false;
167             case I_FT_5: return false;
168             case I_FT_6: return false;
169             default: throw new RuntimeException JavaDoc("Internal error");
170             }
171         }
172
173         public boolean next() {
174             switch (state) {
175             case I_EMPTY:
176                 nextError();
177             case I_F_0:
178                 state = I_F_1;
179                 return false;
180             case I_F_1:
181                 nextError();
182             case I_F_2:
183                 nextError();
184             case I_T_0:
185                 state = I_T_1;
186                 return true;
187             case I_T_1:
188                 nextError();
189             case I_T_2:
190                 nextError();
191             case I_FT_0:
192                 state = I_FT_1;
193                 return false;
194             case I_FT_1:
195                 state = I_FT_3;
196                 return true;
197             case I_FT_2:
198                 state = I_FT_5;
199                 return true;
200             case I_FT_3:
201                 nextError();
202             case I_FT_4:
203                 nextError();
204             case I_FT_5:
205                 nextError();
206             case I_FT_6:
207                 nextError();
208             default:
209                 throw new RuntimeException JavaDoc("Internal error");
210             }
211         }
212
213         public void remove() {
214             switch (state) {
215             case I_EMPTY:
216                 removeError();
217             case I_F_0:
218                 removeError();
219             case I_F_1:
220                 values = EMPTY;
221                 state = I_F_2;
222                 break;
223             case I_F_2:
224                 removeError();
225             case I_T_0:
226                 removeError();
227             case I_T_1:
228                 values = EMPTY;
229                 state = I_T_2;
230                 break;
231             case I_T_2:
232                 removeError();
233             case I_FT_0:
234                 removeError();
235             case I_FT_1:
236                 values = TRUE;
237                 state = I_FT_2;
238                 break;
239             case I_FT_2:
240                 removeError();
241             case I_FT_3:
242                 values = FALSE;
243                 state = I_FT_4;
244                 break;
245             case I_FT_4:
246                 removeError();
247             case I_FT_5:
248                 values = EMPTY;
249                 state = I_FT_6;
250                 break;
251             case I_FT_6:
252                 removeError();
253             default:
254                 throw new RuntimeException JavaDoc("Internal error");
255             }
256         }
257     }
258
259 }
260
Popular Tags