| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package jaggregate; |
| 7 | |
|
| 8 | |
import static jaggregate.internal.ArgumentChecks.*; |
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | 0 | public abstract class AbstractExtensibleCollection<E> extends AbstractCollection<E> |
| 21 | |
implements ExtensibleCollection<E> { |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 11607 | protected AbstractExtensibleCollection() { |
| 27 | |
|
| 28 | 11607 | } |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
@Override |
| 34 | |
public <R> ExtensibleCollection<R> collect( |
| 35 | |
UnaryFunctor<? super E, ? extends R> transformer ) { |
| 36 | |
|
| 37 | 38 | return (ExtensibleCollection<R>) super.collect( transformer ); |
| 38 | |
} |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
@Override |
| 44 | |
public ExtensibleCollection<E> reject( UnaryCondition<? super E> discriminator ) { |
| 45 | 57 | return (ExtensibleCollection<E>) super.reject( discriminator ); |
| 46 | |
} |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@Override |
| 52 | |
public ExtensibleCollection<E> select( UnaryCondition<? super E> discriminator ) { |
| 53 | 92 | return (ExtensibleCollection<E>) super.select( discriminator ); |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
public void addAll( Collection<? extends E> newElements ) { |
| 60 | 873 | newElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 61 | 3455 | public Void evaluate( E argument ) { |
| 62 | 2582 | add( argument ); |
| 63 | 2580 | return null; |
| 64 | |
} |
| 65 | |
} ); |
| 66 | 847 | } |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public void addAll( E[] newElements ) { |
| 72 | 13521 | for ( E each : newElements ) |
| 73 | 9883 | add( each ); |
| 74 | 3615 | } |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public void addAll( E newElement, E... restOfNewElements ) { |
| 80 | 242 | add( newElement ); |
| 81 | 242 | addAll( restOfNewElements ); |
| 82 | 242 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public void addAll( Iterable<? extends E> newElements ) { |
| 88 | 722 | for ( E each : newElements ) |
| 89 | 2416 | add( each ); |
| 90 | 695 | } |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public boolean removeAll( Collection<? extends E> oldElements ) { |
| 96 | 11 | final boolean[] removalsOccurred = new boolean[] { false }; |
| 97 | |
|
| 98 | 11 | oldElements.forEachDo( new UnaryFunctor<E, Void>() { |
| 99 | 25 | public Void evaluate( E argument ) { |
| 100 | 14 | removalsOccurred[ 0 ] |= remove( argument ); |
| 101 | 14 | return null; |
| 102 | |
} |
| 103 | |
} ); |
| 104 | |
|
| 105 | 4 | return removalsOccurred[ 0 ]; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public boolean removeAll( E[] oldElements ) { |
| 112 | 108 | boolean removalsOccurred = false; |
| 113 | |
|
| 114 | 245 | for ( E each : oldElements ) |
| 115 | 137 | removalsOccurred |= remove( each ); |
| 116 | |
|
| 117 | 101 | return removalsOccurred; |
| 118 | |
} |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public boolean removeAll( E oldElement, E... restOfOldElements ) { |
| 124 | 101 | boolean removalsOccurred = remove( oldElement ); |
| 125 | 101 | removalsOccurred |= removeAll( restOfOldElements ); |
| 126 | |
|
| 127 | 101 | return removalsOccurred; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public boolean removeAll( Iterable<? extends E> oldElements ) { |
| 134 | 106 | boolean removalsOccurred = false; |
| 135 | |
|
| 136 | 106 | for ( E each : oldElements ) |
| 137 | 232 | removalsOccurred |= remove( each ); |
| 138 | |
|
| 139 | 99 | return removalsOccurred; |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public boolean removeIf( final UnaryCondition<? super E> discriminator ) { |
| 146 | 187 | ensureNotNull( discriminator, DISCRIMINATOR ); |
| 147 | |
|
| 148 | 181 | final boolean[] removalOccurred = new boolean[] { false }; |
| 149 | |
|
| 150 | 181 | forEachDo( new UnaryFunctor<E, Void>() { |
| 151 | 1156 | public Void evaluate( E argument ) { |
| 152 | 975 | if ( discriminator.matches( argument ) ) { |
| 153 | 422 | removalOccurred[ 0 ] = true; |
| 154 | 422 | remove( argument ); |
| 155 | |
} |
| 156 | |
|
| 157 | 975 | return null; |
| 158 | |
} |
| 159 | |
} ); |
| 160 | |
|
| 161 | 181 | return removalOccurred[ 0 ]; |
| 162 | |
} |
| 163 | |
|
| 164 | |
private boolean retainAllOf( final Collection<Object> keepers ) { |
| 165 | 156 | return removeIf( new UnaryCondition<E>() { |
| 166 | 156 | public boolean matches( E target ) { |
| 167 | 718 | return !keepers.includes( target ); |
| 168 | |
} |
| 169 | |
} ); |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public boolean retainAll( Collection<? extends E> keepers ) { |
| 176 | 12 | AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet(); |
| 177 | 12 | setOfKeepers.addAll( keepers ); |
| 178 | |
|
| 179 | 6 | return retainAllOf( setOfKeepers ); |
| 180 | |
} |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public boolean retainAll( E[] keepers ) { |
| 186 | 11 | AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet(); |
| 187 | 11 | setOfKeepers.addAll( keepers ); |
| 188 | |
|
| 189 | 4 | return retainAllOf( setOfKeepers ); |
| 190 | |
} |
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
public boolean retainAll( E keeper, E... restOfKeepers ) { |
| 196 | 75 | AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet(); |
| 197 | 75 | setOfKeepers.addAll( keeper, restOfKeepers ); |
| 198 | |
|
| 199 | 75 | return retainAllOf( setOfKeepers ); |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public boolean retainAll( Iterable<? extends E> keepers ) { |
| 206 | 78 | AbstractExtensibleCollection<Object> setOfKeepers = newEmptySet(); |
| 207 | 78 | setOfKeepers.addAll( keepers ); |
| 208 | |
|
| 209 | 71 | return retainAllOf( setOfKeepers ); |
| 210 | |
} |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public boolean retainIf( final UnaryCondition<? super E> discriminator ) { |
| 216 | 52 | ensureNotNull( discriminator, DISCRIMINATOR ); |
| 217 | |
|
| 218 | 46 | final boolean[] removalOccurred = new boolean[] { false }; |
| 219 | |
|
| 220 | 46 | forEachDo( new UnaryFunctor<E, Void>() { |
| 221 | 431 | public Void evaluate( E argument ) { |
| 222 | 385 | if ( !discriminator.matches( argument ) ) { |
| 223 | 207 | removalOccurred[ 0 ] = true; |
| 224 | 207 | remove( argument ); |
| 225 | |
} |
| 226 | |
|
| 227 | 385 | return null; |
| 228 | |
} |
| 229 | |
} ); |
| 230 | |
|
| 231 | 46 | return removalOccurred[ 0 ]; |
| 232 | |
} |
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
protected abstract <T> AbstractSet<T> newEmptySet(); |
| 241 | |
} |