KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > AbstractShortCollection


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;
20
21 import bak.pcj.util.Exceptions;
22
23 /**
24  * This class represents an abstract base for implementing
25  * collections of short values. All operations that can be implemented
26  * using iterators are implemented as such. In most cases, this is
27  * hardly an efficient solution, and at least some of those
28  * methods should be overridden by sub-classes.
29  *
30  * <p>In this implementation, <tt>size()</tt> is calculated by
31  * iterating over the collection. Make sure that <tt>size()</tt>
32  * is overwritten or that iterators do not depend on the
33  * <tt>size()</tt> method.
34  *
35  * @author S&oslash;ren Bak
36  * @version 1.3 21-08-2003 20:16
37  * @since 1.0
38  */

39 public abstract class AbstractShortCollection implements ShortCollection {
40
41     /** Default constructor to be invoked by sub-classes. */
42     protected AbstractShortCollection() { }
43
44     /**
45      * Throws <tt>UnsupportedOperationException</tt>.
46      *
47      * @throws UnsupportedOperationException
48      * unconditionally.
49      */

50     public boolean add(short v)
51     { Exceptions.unsupported("add"); return false; }
52
53     public boolean addAll(ShortCollection c) {
54         ShortIterator i = c.iterator(); // Throws NullPointerException
55
boolean result = false;
56         while (i.hasNext())
57             result = result | add(i.next());
58         return result;
59     }
60
61     public void clear() {
62         ShortIterator i = iterator();
63         while (i.hasNext()) {
64             i.next();
65             i.remove();
66         }
67     }
68
69     public boolean contains(short v) {
70         ShortIterator i = iterator();
71         while (i.hasNext())
72             if (i.next() == v)
73                 return true;
74         return false;
75     }
76
77     public boolean containsAll(ShortCollection c) {
78         ShortIterator i = c.iterator(); // Throws NullPointerException
79
while (i.hasNext())
80             if (!contains(i.next()))
81                 return false;
82         return true;
83     }
84
85     public boolean isEmpty()
86     { return size() == 0; }
87
88     public boolean remove(short v) {
89         ShortIterator i = iterator();
90         boolean result = false;
91         while (i.hasNext()) {
92             if (i.next() == v) {
93                 i.remove();
94                 result = true;
95                 break;
96             }
97         }
98         return result;
99     }
100
101     public boolean removeAll(ShortCollection c) {
102         if (c == null)
103             Exceptions.nullArgument("collection");
104         ShortIterator i = iterator();
105         boolean result = false;
106         while (i.hasNext()) {
107             if (c.contains(i.next())) {
108                 i.remove();
109                 result = true;
110             }
111         }
112         return result;
113     }
114
115     public boolean retainAll(ShortCollection c) {
116         if (c == null)
117             Exceptions.nullArgument("collection");
118         ShortIterator i = iterator();
119         boolean result = false;
120         while (i.hasNext()) {
121             if (!c.contains(i.next())) {
122                 i.remove();
123                 result = true;
124             }
125         }
126         return result;
127     }
128
129     public int size() {
130         ShortIterator i = iterator();
131         int size = 0;
132         while (i.hasNext()) {
133             i.next();
134             size++;
135         }
136         return size;
137     }
138
139     public short[] toArray() {
140         return toArray(null);
141     }
142
143     public short[] toArray(short[] a) {
144         int size = size();
145         if (a == null || a.length < size)
146             a = new short[size];
147         ShortIterator i = iterator();
148         int index = 0;
149         while (i.hasNext()) {
150             a[index] = i.next();
151             index++;
152         }
153         return a;
154     }
155
156     /**
157      * Does nothing. Sub-classes may provide an implementation to
158      * minimize memory usage, but this is not required since many
159      * implementations will always have minimal memory usage.
160      */

161     public void trimToSize()
162     { }
163
164     /**
165      * Returns a string representation of this collection.
166      *
167      * @return a string representation of this collection.
168      */

169     public String JavaDoc toString() {
170         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
171         s.append('[');
172         ShortIterator i = iterator();
173         while (i.hasNext()) {
174             if (s.length() > 1)
175                 s.append(',');
176             s.append(bak.pcj.util.Display.display(i.next()));
177         }
178         s.append(']');
179         return s.toString();
180     }
181
182 }
Popular Tags