001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.util;
017:
018: import com.google.gwt.lang.Array;
019:
020: /**
021: * Skeletal implementation of the Collection interface. <a
022: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractCollection.html">[Sun
023: * docs]</a>
024: *
025: * @param <E> the element type.
026: *
027: */
028: public abstract class AbstractCollection<E> implements Collection<E> {
029:
030: protected AbstractCollection() {
031: }
032:
033: public boolean add(E o) {
034: throw new UnsupportedOperationException("add");
035: }
036:
037: public boolean addAll(Collection<? extends E> c) {
038: Iterator<? extends E> iter = c.iterator();
039: boolean changed = false;
040: while (iter.hasNext()) {
041: if (add(iter.next())) {
042: changed = true;
043: }
044: }
045: return changed;
046: }
047:
048: public void clear() {
049: Iterator<E> iter = iterator();
050: while (iter.hasNext()) {
051: iter.next();
052: iter.remove();
053: }
054: }
055:
056: public boolean contains(Object o) {
057: Iterator<E> iter = advanceToFind(iterator(), o);
058: return iter == null ? false : true;
059: }
060:
061: public boolean containsAll(Collection<?> c) {
062: Iterator<?> iter = c.iterator();
063: while (iter.hasNext()) {
064: if (!contains(iter.next())) {
065: return false;
066: }
067: }
068: return true;
069: }
070:
071: public boolean isEmpty() {
072: return size() == 0;
073: }
074:
075: public abstract Iterator<E> iterator();
076:
077: public boolean remove(Object o) {
078: Iterator<E> iter = advanceToFind(iterator(), o);
079: if (iter != null) {
080: iter.remove();
081: return true;
082: } else {
083: return false;
084: }
085: }
086:
087: public boolean removeAll(Collection<?> c) {
088: Iterator<?> iter = c.iterator();
089: boolean changed = false;
090: while (iter.hasNext()) {
091: if (remove(iter.next())) {
092: changed = true;
093: }
094: }
095: return changed;
096: }
097:
098: public boolean retainAll(Collection<?> c) {
099: Iterator<?> iter = iterator();
100: boolean changed = false;
101: while (iter.hasNext()) {
102: if (!c.contains(iter.next())) {
103: iter.remove();
104: changed = true;
105: }
106: }
107: return changed;
108: }
109:
110: public abstract int size();
111:
112: public Object[] toArray() {
113: return toArray(new Object[size()]);
114: }
115:
116: public <T> T[] toArray(T[] a) {
117: int size = size();
118: if (a.length < size) {
119: a = Array.clonify(a, size);
120: }
121: Object[] result = a;
122: Iterator<E> it = iterator();
123: for (int i = 0; i < size; ++i) {
124: result[i] = it.next();
125: }
126: if (a.length > size) {
127: a[size] = null;
128: }
129: return a;
130: }
131:
132: @Override
133: public String toString() {
134: StringBuffer sb = new StringBuffer();
135: String comma = null;
136: sb.append("[");
137: Iterator<E> iter = iterator();
138: while (iter.hasNext()) {
139: if (comma != null) {
140: sb.append(comma);
141: } else {
142: comma = ", ";
143: }
144: sb.append(String.valueOf(iter.next()));
145: }
146: sb.append("]");
147: return sb.toString();
148: }
149:
150: private <T> Iterator<T> advanceToFind(Iterator<T> iter, Object o) {
151: while (iter.hasNext()) {
152: T t = iter.next();
153: if (o == null ? t == null : o.equals(t)) {
154: return iter;
155: }
156: }
157: return null;
158: }
159: }
|