View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections.primitives;
18  
19  /**
20   * Abstract base class for {@link CharCollection}s.
21   * <p />
22   * Read-only subclasses must override {@link #iterator}
23   * and {@link #size}.  Mutable subclasses
24   * should also override {@link #add} and 
25   * {@link CharIterator#remove CharIterator.remove}.
26   * All other methods have at least some base implementation 
27   * derived from these.  Subclasses may choose to override 
28   * these methods to provide a more efficient implementation. 
29   * 
30   * @since Commons Primitives 1.0
31   * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $
32   * 
33   * @author Rodney Waldhoff 
34   */
35  public abstract class AbstractCharCollection implements CharCollection {
36      public abstract CharIterator iterator();
37      public abstract int size();
38            
39      protected AbstractCharCollection() { }
40                
41      /** Unsupported in this base implementation. */
42      public boolean add(char element) {
43          throw new UnsupportedOperationException("add(char) is not supported.");
44      }
45  
46      public boolean addAll(CharCollection c) {
47          boolean modified = false;
48          for(CharIterator iter = c.iterator(); iter.hasNext(); ) {
49              modified  |= add(iter.next());
50          }
51          return modified;
52      }
53      
54      public void clear() {
55          for(CharIterator iter = iterator(); iter.hasNext();) {
56              iter.next();
57              iter.remove();
58          }
59      }        
60  
61      public boolean contains(char element) {
62          for(CharIterator iter = iterator(); iter.hasNext();) {
63              if(iter.next() == element) {
64                  return true;
65              }
66          }
67          return false;
68      }
69          
70      public boolean containsAll(CharCollection c) {
71          for(CharIterator iter = c.iterator(); iter.hasNext();) {
72              if(!contains(iter.next())) {
73                  return false;
74              }
75          }
76          return true;
77      }
78      
79      public boolean isEmpty() {
80          return (0 == size());
81      }
82         
83      public boolean removeElement(char element) {
84          for(CharIterator iter = iterator(); iter.hasNext();) {
85              if(iter.next() == element) {
86                  iter.remove();
87                  return true;
88              }
89          }
90          return false;
91      }        
92      
93      public boolean removeAll(CharCollection c) {
94          boolean modified = false;
95          for(CharIterator iter = c.iterator(); iter.hasNext(); ) {
96              modified  |= removeElement(iter.next());
97          }
98          return modified;
99      }       
100     
101     public boolean retainAll(CharCollection c) {
102         boolean modified = false;
103         for(CharIterator iter = iterator(); iter.hasNext();) {
104             if(!c.contains(iter.next())) {
105                 iter.remove();
106                 modified = true;
107             }
108         }
109         return modified;
110     }
111     
112     public char[] toArray() {
113         char[] array = new char[size()];
114         int i = 0;
115         for(CharIterator iter = iterator(); iter.hasNext();) {
116             array[i] = iter.next();
117             i++;
118         }
119         return array;
120     }
121         
122     public char[] toArray(char[] a) {
123         if(a.length < size()) {
124             return toArray();
125         } else {
126             int i = 0;
127             for(CharIterator iter = iterator(); iter.hasNext();) {
128                 a[i] = iter.next();
129                 i++;
130             }
131             return a;
132         }            
133     }
134 }