| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AbstractCharCollection |
|
| 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 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 | 803 | protected AbstractCharCollection() { } |
| 40 | ||
| 41 | /** Unsupported in this base implementation. */ | |
| 42 | public boolean add(char element) { | |
| 43 | 1 | throw new UnsupportedOperationException("add(char) is not supported."); |
| 44 | } | |
| 45 | ||
| 46 | public boolean addAll(CharCollection c) { | |
| 47 | 4 | boolean modified = false; |
| 48 | 4 | for(CharIterator iter = c.iterator(); iter.hasNext(); ) { |
| 49 | 43 | modified |= add(iter.next()); |
| 50 | } | |
| 51 | 3 | return modified; |
| 52 | } | |
| 53 | ||
| 54 | public void clear() { | |
| 55 | 2 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 56 | 13 | iter.next(); |
| 57 | 13 | iter.remove(); |
| 58 | } | |
| 59 | 2 | } |
| 60 | ||
| 61 | public boolean contains(char element) { | |
| 62 | 570 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 63 | 4752 | if(iter.next() == element) { |
| 64 | 429 | return true; |
| 65 | } | |
| 66 | } | |
| 67 | 141 | return false; |
| 68 | } | |
| 69 | ||
| 70 | public boolean containsAll(CharCollection c) { | |
| 71 | 21 | for(CharIterator iter = c.iterator(); iter.hasNext();) { |
| 72 | 183 | if(!contains(iter.next())) { |
| 73 | 6 | return false; |
| 74 | } | |
| 75 | } | |
| 76 | 15 | return true; |
| 77 | } | |
| 78 | ||
| 79 | public boolean isEmpty() { | |
| 80 | 1193 | return (0 == size()); |
| 81 | } | |
| 82 | ||
| 83 | public boolean removeElement(char element) { | |
| 84 | 234 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 85 | 1675 | if(iter.next() == element) { |
| 86 | 108 | iter.remove(); |
| 87 | 108 | return true; |
| 88 | } | |
| 89 | } | |
| 90 | 126 | return false; |
| 91 | } | |
| 92 | ||
| 93 | public boolean removeAll(CharCollection c) { | |
| 94 | 18 | boolean modified = false; |
| 95 | 18 | for(CharIterator iter = c.iterator(); iter.hasNext(); ) { |
| 96 | 93 | modified |= removeElement(iter.next()); |
| 97 | } | |
| 98 | 18 | return modified; |
| 99 | } | |
| 100 | ||
| 101 | public boolean retainAll(CharCollection c) { | |
| 102 | 21 | boolean modified = false; |
| 103 | 21 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 104 | 210 | if(!c.contains(iter.next())) { |
| 105 | 117 | iter.remove(); |
| 106 | 117 | modified = true; |
| 107 | } | |
| 108 | } | |
| 109 | 21 | return modified; |
| 110 | } | |
| 111 | ||
| 112 | public char[] toArray() { | |
| 113 | 1179 | char[] array = new char[size()]; |
| 114 | 1179 | int i = 0; |
| 115 | 1179 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 116 | 15859 | array[i] = iter.next(); |
| 117 | 15859 | i++; |
| 118 | } | |
| 119 | 1179 | return array; |
| 120 | } | |
| 121 | ||
| 122 | public char[] toArray(char[] a) { | |
| 123 | 3 | if(a.length < size()) { |
| 124 | 1 | return toArray(); |
| 125 | } else { | |
| 126 | 2 | int i = 0; |
| 127 | 2 | for(CharIterator iter = iterator(); iter.hasNext();) { |
| 128 | 38 | a[i] = iter.next(); |
| 129 | 38 | i++; |
| 130 | } | |
| 131 | 2 | return a; |
| 132 | } | |
| 133 | } | |
| 134 | } |