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 bak.pcj;
020:
021: import bak.pcj.util.Exceptions;
022:
023: /**
024: * This class represents an abstract base for implementing
025: * collections of double 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 AbstractDoubleCollection implements
040: DoubleCollection {
041:
042: /** Default constructor to be invoked by sub-classes. */
043: protected AbstractDoubleCollection() {
044: }
045:
046: /**
047: * Throws <tt>UnsupportedOperationException</tt>.
048: *
049: * @throws UnsupportedOperationException
050: * unconditionally.
051: */
052: public boolean add(double v) {
053: Exceptions.unsupported("add");
054: return false;
055: }
056:
057: public boolean addAll(DoubleCollection c) {
058: DoubleIterator i = c.iterator(); // Throws NullPointerException
059: boolean result = false;
060: while (i.hasNext())
061: result = result | add(i.next());
062: return result;
063: }
064:
065: public void clear() {
066: DoubleIterator i = iterator();
067: while (i.hasNext()) {
068: i.next();
069: i.remove();
070: }
071: }
072:
073: public boolean contains(double v) {
074: DoubleIterator i = iterator();
075: while (i.hasNext())
076: if (i.next() == v)
077: return true;
078: return false;
079: }
080:
081: public boolean containsAll(DoubleCollection c) {
082: DoubleIterator i = c.iterator(); // Throws NullPointerException
083: while (i.hasNext())
084: if (!contains(i.next()))
085: return false;
086: return true;
087: }
088:
089: public boolean isEmpty() {
090: return size() == 0;
091: }
092:
093: public boolean remove(double v) {
094: DoubleIterator i = iterator();
095: boolean result = false;
096: while (i.hasNext()) {
097: if (i.next() == v) {
098: i.remove();
099: result = true;
100: break;
101: }
102: }
103: return result;
104: }
105:
106: public boolean removeAll(DoubleCollection c) {
107: if (c == null)
108: Exceptions.nullArgument("collection");
109: DoubleIterator i = iterator();
110: boolean result = false;
111: while (i.hasNext()) {
112: if (c.contains(i.next())) {
113: i.remove();
114: result = true;
115: }
116: }
117: return result;
118: }
119:
120: public boolean retainAll(DoubleCollection c) {
121: if (c == null)
122: Exceptions.nullArgument("collection");
123: DoubleIterator i = iterator();
124: boolean result = false;
125: while (i.hasNext()) {
126: if (!c.contains(i.next())) {
127: i.remove();
128: result = true;
129: }
130: }
131: return result;
132: }
133:
134: public int size() {
135: DoubleIterator i = iterator();
136: int size = 0;
137: while (i.hasNext()) {
138: i.next();
139: size++;
140: }
141: return size;
142: }
143:
144: public double[] toArray() {
145: return toArray(null);
146: }
147:
148: public double[] toArray(double[] a) {
149: int size = size();
150: if (a == null || a.length < size)
151: a = new double[size];
152: DoubleIterator i = iterator();
153: int index = 0;
154: while (i.hasNext()) {
155: a[index] = i.next();
156: index++;
157: }
158: return a;
159: }
160:
161: /**
162: * Does nothing. Sub-classes may provide an implementation to
163: * minimize memory usage, but this is not required since many
164: * implementations will always have minimal memory usage.
165: */
166: public void trimToSize() {
167: }
168:
169: /**
170: * Returns a string representation of this collection.
171: *
172: * @return a string representation of this collection.
173: */
174: public String toString() {
175: StringBuffer s = new StringBuffer();
176: s.append('[');
177: DoubleIterator i = iterator();
178: while (i.hasNext()) {
179: if (s.length() > 1)
180: s.append(',');
181: s.append(bak.pcj.util.Display.display(i.next()));
182: }
183: s.append(']');
184: return s.toString();
185: }
186:
187: }
|