001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2002, 2003 S�ren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package com.uwyn.rife.pcj;
020:
021: import com.uwyn.rife.pcj.util.Exceptions;
022:
023: /**
024: * This class represents an abstract base for implementing
025: * collections of int values. All operations that can be implemented
026: * using iterators are implemented as such. In most cases, this is
027: * hardly an efficient solution, and at least some of those
028: * methods should be overridden by sub-classes.
029: *
030: * <p>In this implementation, <tt>size()</tt> is calculated by
031: * iterating over the collection. Make sure that <tt>size()</tt>
032: * is overwritten or that iterators do not depend on the
033: * <tt>size()</tt> method.
034: *
035: * @author Søren Bak
036: * @version 1.3 21-08-2003 20:16
037: * @since 1.0
038: */
039: public abstract class AbstractIntCollection implements IntCollection {
040:
041: /** Default constructor to be invoked by sub-classes. */
042: protected AbstractIntCollection() {
043: }
044:
045: /**
046: * Throws <tt>UnsupportedOperationException</tt>.
047: *
048: * @throws UnsupportedOperationException
049: * unconditionally.
050: */
051: public boolean add(int v) {
052: Exceptions.unsupported("add");
053: return false;
054: }
055:
056: public boolean addAll(IntCollection c) {
057: IntIterator i = c.iterator(); // Throws NullPointerException
058: boolean result = false;
059: while (i.hasNext())
060: result = result | add(i.next());
061: return result;
062: }
063:
064: public void clear() {
065: IntIterator i = iterator();
066: while (i.hasNext()) {
067: i.next();
068: i.remove();
069: }
070: }
071:
072: public boolean contains(int v) {
073: IntIterator i = iterator();
074: while (i.hasNext())
075: if (i.next() == v)
076: return true;
077: return false;
078: }
079:
080: public boolean containsAll(IntCollection c) {
081: IntIterator i = c.iterator(); // Throws NullPointerException
082: while (i.hasNext())
083: if (!contains(i.next()))
084: return false;
085: return true;
086: }
087:
088: public boolean isEmpty() {
089: return size() == 0;
090: }
091:
092: public boolean remove(int v) {
093: IntIterator i = iterator();
094: boolean result = false;
095: while (i.hasNext()) {
096: if (i.next() == v) {
097: i.remove();
098: result = true;
099: break;
100: }
101: }
102: return result;
103: }
104:
105: public boolean removeAll(IntCollection c) {
106: if (c == null)
107: Exceptions.nullArgument("collection");
108: IntIterator i = iterator();
109: boolean result = false;
110: while (i.hasNext()) {
111: if (c.contains(i.next())) {
112: i.remove();
113: result = true;
114: }
115: }
116: return result;
117: }
118:
119: public boolean retainAll(IntCollection c) {
120: if (c == null)
121: Exceptions.nullArgument("collection");
122: IntIterator i = iterator();
123: boolean result = false;
124: while (i.hasNext()) {
125: if (!c.contains(i.next())) {
126: i.remove();
127: result = true;
128: }
129: }
130: return result;
131: }
132:
133: public int size() {
134: IntIterator i = iterator();
135: int size = 0;
136: while (i.hasNext()) {
137: i.next();
138: size++;
139: }
140: return size;
141: }
142:
143: public int[] toArray() {
144: return toArray(null);
145: }
146:
147: public int[] toArray(int[] a) {
148: int size = size();
149: if (a == null || a.length < size)
150: a = new int[size];
151: IntIterator i = iterator();
152: int index = 0;
153: while (i.hasNext()) {
154: a[index] = i.next();
155: index++;
156: }
157: return a;
158: }
159:
160: /**
161: * Does nothing. Sub-classes may provide an implementation to
162: * minimize memory usage, but this is not required since many
163: * implementations will always have minimal memory usage.
164: */
165: public void trimToSize() {
166: }
167:
168: /**
169: * Returns a string representation of this collection.
170: *
171: * @return a string representation of this collection.
172: */
173: public String toString() {
174: StringBuffer s = new StringBuffer();
175: s.append('[');
176: IntIterator i = iterator();
177: while (i.hasNext()) {
178: if (s.length() > 1)
179: s.append(',');
180: s.append(com.uwyn.rife.pcj.util.Display.display(i.next()));
181: }
182: s.append(']');
183: return s.toString();
184: }
185:
186: }
|