Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractShortCollection |
|
| 2.357142857142857;2.357 |
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 ShortCollection}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 ShortIterator#remove ShortIterator.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 AbstractShortCollection implements ShortCollection { | |
36 | public abstract ShortIterator iterator(); | |
37 | public abstract int size(); | |
38 | ||
39 | 1341 | protected AbstractShortCollection() { } |
40 | ||
41 | /** Unsupported in this base implementation. */ | |
42 | public boolean add(short element) { | |
43 | 1 | throw new UnsupportedOperationException("add(short) is not supported."); |
44 | } | |
45 | ||
46 | public boolean addAll(ShortCollection c) { | |
47 | 289 | boolean modified = false; |
48 | 289 | for(ShortIterator iter = c.iterator(); iter.hasNext(); ) { |
49 | 5431 | modified |= add(iter.next()); |
50 | } | |
51 | 288 | return modified; |
52 | } | |
53 | ||
54 | public void clear() { | |
55 | 4 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
56 | 26 | iter.next(); |
57 | 26 | iter.remove(); |
58 | } | |
59 | 4 | } |
60 | ||
61 | public boolean contains(short element) { | |
62 | 1002 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
63 | 8711 | if(iter.next() == element) { |
64 | 755 | return true; |
65 | } | |
66 | } | |
67 | 247 | return false; |
68 | } | |
69 | ||
70 | public boolean containsAll(ShortCollection c) { | |
71 | 35 | for(ShortIterator iter = c.iterator(); iter.hasNext();) { |
72 | 321 | if(!contains(iter.next())) { |
73 | 10 | return false; |
74 | } | |
75 | } | |
76 | 25 | return true; |
77 | } | |
78 | ||
79 | public boolean isEmpty() { | |
80 | 2126 | return (0 == size()); |
81 | } | |
82 | ||
83 | public boolean removeElement(short element) { | |
84 | 415 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
85 | 3289 | if(iter.next() == element) { |
86 | 193 | iter.remove(); |
87 | 193 | return true; |
88 | } | |
89 | } | |
90 | 222 | return false; |
91 | } | |
92 | ||
93 | public boolean removeAll(ShortCollection c) { | |
94 | 30 | boolean modified = false; |
95 | 30 | for(ShortIterator iter = c.iterator(); iter.hasNext(); ) { |
96 | 163 | modified |= removeElement(iter.next()); |
97 | } | |
98 | 30 | return modified; |
99 | } | |
100 | ||
101 | public boolean retainAll(ShortCollection c) { | |
102 | 35 | boolean modified = false; |
103 | 35 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
104 | 370 | if(!c.contains(iter.next())) { |
105 | 207 | iter.remove(); |
106 | 207 | modified = true; |
107 | } | |
108 | } | |
109 | 35 | return modified; |
110 | } | |
111 | ||
112 | public short[] toArray() { | |
113 | 2114 | short[] array = new short[size()]; |
114 | 2114 | int i = 0; |
115 | 2114 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
116 | 29771 | array[i] = iter.next(); |
117 | 29771 | i++; |
118 | } | |
119 | 2114 | return array; |
120 | } | |
121 | ||
122 | public short[] toArray(short[] a) { | |
123 | 6 | if(a.length < size()) { |
124 | 2 | return toArray(); |
125 | } else { | |
126 | 4 | int i = 0; |
127 | 4 | for(ShortIterator iter = iterator(); iter.hasNext();) { |
128 | 76 | a[i] = iter.next(); |
129 | 76 | i++; |
130 | } | |
131 | 4 | return a; |
132 | } | |
133 | } | |
134 | } |