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 FloatCollection}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 FloatIterator#remove FloatIterator.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 AbstractFloatCollection implements FloatCollection {
36      public abstract FloatIterator iterator();
37      public abstract int size();
38            
39      protected AbstractFloatCollection() { }
40                
41      /** Unsupported in this base implementation. */
42      public boolean add(float element) {
43          throw new UnsupportedOperationException("add(float) is not supported.");
44      }
45  
46      public boolean addAll(FloatCollection c) {
47          boolean modified = false;
48          for(FloatIterator iter = c.iterator(); iter.hasNext(); ) {
49              modified  |= add(iter.next());
50          }
51          return modified;
52      }
53      
54      public void clear() {
55          for(FloatIterator iter = iterator(); iter.hasNext();) {
56              iter.next();
57              iter.remove();
58          }
59      }        
60  
61      public boolean contains(float element) {
62          for(FloatIterator iter = iterator(); iter.hasNext();) {
63              if(iter.next() == element) {
64                  return true;
65              }
66          }
67          return false;
68      }
69          
70      public boolean containsAll(FloatCollection c) {
71          for(FloatIterator 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(float element) {
84          for(FloatIterator 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(FloatCollection c) {
94          boolean modified = false;
95          for(FloatIterator iter = c.iterator(); iter.hasNext(); ) {
96              modified  |= removeElement(iter.next());
97          }
98          return modified;
99      }       
100     
101     public boolean retainAll(FloatCollection c) {
102         boolean modified = false;
103         for(FloatIterator 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 float[] toArray() {
113         float[] array = new float[size()];
114         int i = 0;
115         for(FloatIterator iter = iterator(); iter.hasNext();) {
116             array[i] = iter.next();
117             i++;
118         }
119         return array;
120     }
121         
122     public float[] toArray(float[] a) {
123         if(a.length < size()) {
124             return toArray();
125         } else {
126             int i = 0;
127             for(FloatIterator iter = iterator(); iter.hasNext();) {
128                 a[i] = iter.next();
129                 i++;
130             }
131             return a;
132         }            
133     }
134 }