KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > list > AbstractBooleanList


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2002, 2003 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.list;
20
21 import bak.pcj.BooleanIterator;
22 import bak.pcj.BooleanCollection;
23 import bak.pcj.AbstractBooleanCollection;
24 import bak.pcj.hash.DefaultBooleanHashFunction;
25 import bak.pcj.util.Exceptions;
26
27 /**
28  * This class represents an abstract base for implementing
29  * lists of boolean values. All operations that can be implemented
30  * using iterators and the <tt>get()</tt> and <tt>set()</tt> methods
31  * are implemented as such. In most cases, this is
32  * hardly an efficient solution, and at least some of those
33  * methods should be overridden by sub-classes.
34  *
35  * @author S&oslash;ren Bak
36  * @version 1.2 21-08-2003 19:14
37  * @since 1.0
38  */

39 public abstract class AbstractBooleanList extends AbstractBooleanCollection implements BooleanList {
40
41     /** Default constructor to be invoked by sub-classes. */
42     protected AbstractBooleanList() { }
43
44     public boolean add(boolean v)
45     { add(size(), v); return true; }
46
47     /**
48      * Throws <tt>UnsupportedOperationException</tt>.
49      *
50      * @throws UnsupportedOperationException
51      * unconditionally.
52      */

53     public void add(int index, boolean v)
54     { Exceptions.unsupported("add"); }
55
56
57     public boolean addAll(int index, BooleanCollection c) {
58         if (index < 0 || index > size())
59             Exceptions.indexOutOfBounds(index, 0, size());
60         BooleanIterator i = c.iterator();
61         boolean result = i.hasNext();
62         while (i.hasNext()) {
63             add(index, i.next());
64             index++;
65         }
66         return result;
67     }
68
69     public int indexOf(boolean c) {
70         return indexOf(0, c);
71     }
72
73     /**
74      * @since 1.2
75      */

76     public int indexOf(int index, boolean c) {
77         BooleanListIterator i = listIterator(index);
78         while (i.hasNext())
79             if (i.next() == c)
80                 return i.previousIndex();
81         return -1;
82     }
83
84     public BooleanIterator iterator()
85     { return listIterator(); }
86
87     public int lastIndexOf(boolean c) {
88         BooleanListIterator i = listIterator(size());
89         while (i.hasPrevious())
90             if (i.previous() == c)
91                 return i.nextIndex();
92         return -1;
93     }
94
95     public int lastIndexOf(int index, boolean c) {
96         BooleanListIterator i = listIterator(index);
97         while (i.hasPrevious())
98             if (i.previous() == c)
99                 return i.nextIndex();
100         return -1;
101     }
102
103     public BooleanListIterator listIterator()
104     { return listIterator(0); }
105
106     public BooleanListIterator listIterator(final int index) {
107         if (index < 0 || index > size())
108             Exceptions.indexOutOfBounds(index, 0, size());
109
110         return new BooleanListIterator() {
111             private int ptr = index;
112             private int lptr = -1;
113
114             // -------------------------------------------------------
115
// Implementation of Iterator
116
// -------------------------------------------------------
117

118             public boolean hasNext() {
119                 return ptr < size();
120             }
121
122             public boolean next() {
123                 if (ptr == size())
124                     Exceptions.endOfIterator();
125                 lptr = ptr++;
126                 return get(lptr);
127             }
128
129             public void remove() {
130                 if (lptr == -1)
131                     Exceptions.noElementToRemove();
132                 AbstractBooleanList.this.removeElementAt(lptr);
133                 if (lptr < ptr) ptr--;
134                 lptr = -1;
135             }
136
137             // -------------------------------------------------------
138
// Implementation of ListIterator
139
// -------------------------------------------------------
140

141             public void add(boolean v) {
142                 AbstractBooleanList.this.add(ptr++, v);
143                 lptr = -1;
144             }
145
146             public boolean hasPrevious() {
147                 return ptr > 0;
148             }
149
150             public int nextIndex()
151             { return ptr; }
152
153             public boolean previous() {
154                 if (ptr == 0)
155                     Exceptions.startOfIterator();
156                 ptr--;
157                 lptr = ptr;
158                 return get(ptr);
159             }
160
161             public int previousIndex()
162             { return ptr-1; }
163
164             public void set(boolean v) {
165                 if (lptr == -1)
166                     Exceptions.noElementToSet();
167                 AbstractBooleanList.this.set(lptr, v);
168             }
169
170         };
171     }
172
173     /**
174      * Throws <tt>UnsupportedOperationException</tt>.
175      *
176      * @throws UnsupportedOperationException
177      * unconditionally.
178      */

179     public boolean removeElementAt(int index)
180     { Exceptions.unsupported("removeElementAt"); throw new RuntimeException JavaDoc(); }
181
182     public boolean equals(Object JavaDoc obj) {
183         if (this == obj)
184             return true;
185         if (!(obj instanceof BooleanList))
186             return false;
187         BooleanListIterator i1 = listIterator();
188         BooleanListIterator i2 = ((BooleanList)obj).listIterator();
189         while(i1.hasNext() && i2.hasNext())
190             if (i1.next() != i2.next())
191                 return false;
192         return !(i1.hasNext() || i2.hasNext());
193     }
194
195     public int hashCode() {
196         int h = 1;
197         BooleanIterator i = iterator();
198         while (i.hasNext())
199             h = (int)(31*h + DefaultBooleanHashFunction.INSTANCE.hash(i.next()));
200         return h;
201     }
202
203 }
Popular Tags