KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > AbstractCollection


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package java.util;
17
18 /**
19  * Abstract base class for collection implementations.
20  */

21 public abstract class AbstractCollection implements Collection JavaDoc {
22
23   public boolean add(Object JavaDoc o) {
24     throw new UnsupportedOperationException JavaDoc("add");
25   }
26
27   public boolean addAll(Collection JavaDoc c) {
28     Iterator JavaDoc iter = c.iterator();
29     boolean changed = false;
30     while (iter.hasNext()) {
31       if (add(iter.next())) {
32         changed = true;
33       }
34     }
35     return changed;
36   }
37
38   public void clear() {
39     Iterator JavaDoc iter = iterator();
40     while (iter.hasNext()) {
41       iter.next();
42       iter.remove();
43     }
44   }
45
46   public boolean contains(Object JavaDoc o) {
47     Iterator JavaDoc iter = advanceToFind(iterator(), o);
48     return iter == null ? false : true;
49   }
50
51   public boolean containsAll(Collection JavaDoc c) {
52     Iterator JavaDoc iter = c.iterator();
53     while (iter.hasNext()) {
54       if (!contains(iter.next())) {
55         return false;
56       }
57     }
58     return true;
59   }
60
61   public boolean isEmpty() {
62     return size() == 0;
63   }
64
65   public abstract Iterator JavaDoc iterator();
66
67   public boolean remove(Object JavaDoc o) {
68     Iterator JavaDoc iter = advanceToFind(iterator(), o);
69     if (iter != null) {
70       iter.remove();
71       return true;
72     } else {
73       return false;
74     }
75   }
76
77   public boolean removeAll(Collection JavaDoc c) {
78     Iterator JavaDoc iter = c.iterator();
79     boolean changed = false;
80     while (iter.hasNext()) {
81       if (remove(iter.next())) {
82         changed = true;
83       }
84     }
85     return changed;
86   }
87
88   public boolean retainAll(Collection JavaDoc c) {
89     Iterator JavaDoc iter = iterator();
90     boolean changed = false;
91     while (iter.hasNext()) {
92       if (!c.contains(iter.next())) {
93         iter.remove();
94         changed = true;
95       }
96     }
97     return changed;
98   }
99
100   public abstract int size();
101
102   public Object JavaDoc[] toArray() {
103     int n = size();
104     int i = 0;
105     Object JavaDoc[] array = new Object JavaDoc[n];
106     for (Iterator JavaDoc iter = iterator(); iter.hasNext();) {
107       Object JavaDoc o = iter.next();
108       array[i++] = o;
109     }
110     return array;
111   }
112
113   public String JavaDoc toString() {
114     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
115     String JavaDoc comma = null;
116     sb.append("[");
117     Iterator JavaDoc iter = iterator();
118     while (iter.hasNext()) {
119       if (comma != null) {
120         sb.append(comma);
121       } else {
122         comma = ", ";
123       }
124       sb.append(String.valueOf(iter.next()));
125     }
126     sb.append("]");
127     return sb.toString();
128   }
129
130   private Iterator JavaDoc advanceToFind(Iterator JavaDoc iter, Object JavaDoc o) {
131     while (iter.hasNext()) {
132       Object JavaDoc t = iter.next();
133       if (o == null ? t == null : o.equals(t)) {
134         return iter;
135       }
136     }
137     return null;
138   }
139 }
140
Popular Tags