All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.collections.impl.set.AbstractUnifiedSet Maven / Gradle / Ivy

Go to download

GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map implementations with a rich API and set of utility classes that work with any JDK compatible Collections, Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.

There is a newer version: 7.0.3
Show newest version
/*
 * Copyright 2015 Goldman Sachs.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gs.collections.impl.set;

import java.util.Collection;
import java.util.Set;

import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function0;
import com.gs.collections.api.block.function.Function2;
import com.gs.collections.api.block.function.Function3;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.Pool;
import com.gs.collections.api.set.SetIterable;
import com.gs.collections.api.set.UnsortedSetIterable;
import com.gs.collections.api.set.primitive.MutableBooleanSet;
import com.gs.collections.api.set.primitive.MutableByteSet;
import com.gs.collections.api.set.primitive.MutableCharSet;
import com.gs.collections.api.set.primitive.MutableDoubleSet;
import com.gs.collections.api.set.primitive.MutableFloatSet;
import com.gs.collections.api.set.primitive.MutableIntSet;
import com.gs.collections.api.set.primitive.MutableLongSet;
import com.gs.collections.api.set.primitive.MutableShortSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.AbstractRichIterable;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.multimap.set.UnifiedSetMultimap;
import com.gs.collections.impl.parallel.BatchIterable;
import com.gs.collections.impl.set.mutable.SynchronizedMutableSet;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.mutable.UnmodifiableMutableSet;
import com.gs.collections.impl.set.mutable.primitive.BooleanHashSet;
import com.gs.collections.impl.set.mutable.primitive.ByteHashSet;
import com.gs.collections.impl.set.mutable.primitive.CharHashSet;
import com.gs.collections.impl.set.mutable.primitive.DoubleHashSet;
import com.gs.collections.impl.set.mutable.primitive.FloatHashSet;
import com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import com.gs.collections.impl.set.mutable.primitive.LongHashSet;
import com.gs.collections.impl.set.mutable.primitive.ShortHashSet;
import com.gs.collections.impl.utility.internal.IterableIterate;
import com.gs.collections.impl.utility.internal.MutableCollectionIterate;
import com.gs.collections.impl.utility.internal.SetIterables;

public abstract class AbstractUnifiedSet
        extends AbstractRichIterable
        implements MutableSet, Pool, BatchIterable
{
    protected static final float DEFAULT_LOAD_FACTOR = 0.75f;

    protected static final int DEFAULT_INITIAL_CAPACITY = 8;

    protected float loadFactor = DEFAULT_LOAD_FACTOR;

    protected int maxSize;

    protected abstract Object[] getTable();

    protected abstract void allocateTable(int sizeToAllocate);

    protected abstract void rehash(int newCapacity);

    protected abstract T detect(Predicate predicate, int start, int end);

    @SuppressWarnings("AbstractMethodOverridesAbstractMethod")
    public abstract MutableSet clone();

    protected abstract boolean shortCircuit(
            Predicate predicate,
            boolean expected,
            boolean onShortCircuit,
            boolean atEnd,
            int start,
            int end);

    protected abstract 

boolean shortCircuitWith( Predicate2 predicate2, P parameter, boolean expected, boolean onShortCircuit, boolean atEnd); protected int init(int initialCapacity) { int capacity = 1; while (capacity < initialCapacity) { capacity <<= 1; } return this.allocate(capacity); } protected int allocate(int capacity) { this.allocateTable(capacity); this.computeMaxSize(capacity); return capacity; } protected void computeMaxSize(int capacity) { // need at least one free slot for open addressing this.maxSize = Math.min(capacity - 1, (int) (capacity * this.loadFactor)); } protected void rehash() { this.rehash(this.getTable().length << 1); } protected boolean shortCircuit( Predicate predicate, boolean expected, boolean onShortCircuit, boolean atEnd) { return this.shortCircuit(predicate, expected, onShortCircuit, atEnd, 0, this.getTable().length); } public int getBatchCount(int batchSize) { return Math.max(1, this.getTable().length / batchSize); } public boolean removeIf(Predicate predicate) { return IterableIterate.removeIf(this, predicate); } public

boolean removeIfWith(Predicate2 predicate, P parameter) { return IterableIterate.removeIfWith(this, predicate, parameter); } public UnifiedSet collect(Function function) { return this.collect(function, UnifiedSet.newSet()); } public MutableBooleanSet collectBoolean(BooleanFunction booleanFunction) { return this.collectBoolean(booleanFunction, new BooleanHashSet()); } public MutableByteSet collectByte(ByteFunction byteFunction) { return this.collectByte(byteFunction, new ByteHashSet()); } public MutableCharSet collectChar(CharFunction charFunction) { return this.collectChar(charFunction, new CharHashSet()); } public MutableDoubleSet collectDouble(DoubleFunction doubleFunction) { return this.collectDouble(doubleFunction, new DoubleHashSet()); } public MutableFloatSet collectFloat(FloatFunction floatFunction) { return this.collectFloat(floatFunction, new FloatHashSet()); } public MutableIntSet collectInt(IntFunction intFunction) { return this.collectInt(intFunction, new IntHashSet()); } public MutableLongSet collectLong(LongFunction longFunction) { return this.collectLong(longFunction, new LongHashSet()); } public MutableShortSet collectShort(ShortFunction shortFunction) { return this.collectShort(shortFunction, new ShortHashSet()); } public UnifiedSet flatCollect(Function> function) { return this.flatCollect(function, UnifiedSet.newSet()); } public UnifiedSet collectWith(Function2 function, P parameter) { return this.collectWith(function, parameter, UnifiedSet.newSet()); } public UnifiedSet collectIf( Predicate predicate, Function function) { return this.collectIf(predicate, function, UnifiedSet.newSet()); } @Override public T detect(Predicate predicate) { return this.detect(predicate, 0, this.getTable().length); } @Override public boolean anySatisfy(Predicate predicate) { return this.shortCircuit(predicate, true, true, false); } @Override public

boolean anySatisfyWith( Predicate2 predicate, P parameter) { return this.shortCircuitWith(predicate, parameter, true, true, false); } @Override public boolean allSatisfy(Predicate predicate) { return this.shortCircuit(predicate, false, false, true); } @Override public

boolean allSatisfyWith( Predicate2 predicate, P parameter) { return this.shortCircuitWith(predicate, parameter, false, false, true); } @Override public boolean noneSatisfy(Predicate predicate) { return this.shortCircuit(predicate, true, false, true); } @Override public

boolean noneSatisfyWith( Predicate2 predicate, P parameter) { return this.shortCircuitWith(predicate, parameter, true, false, true); } public IV injectIntoWith( IV injectValue, final Function3 function, final P parameter) { return this.injectInto(injectValue, new Function2() { public IV value(IV argument1, T argument2) { return function.value(argument1, argument2, parameter); } }); } public MutableSet asUnmodifiable() { return UnmodifiableMutableSet.of(this); } public MutableSet asSynchronized() { return SynchronizedMutableSet.of(this); } public boolean addAll(Collection collection) { return this.addAllIterable(collection); } public boolean removeAll(Collection collection) { return this.removeAllIterable(collection); } public boolean removeAllIterable(Iterable iterable) { boolean changed = false; for (Object each : iterable) { changed |= this.remove(each); } return changed; } public boolean retainAll(Collection collection) { return this.retainAllIterable(collection); } public UnifiedSetMultimap groupByEach( Function> function) { return this.groupByEach(function, UnifiedSetMultimap.newMultimap()); } public MutableMap groupByUniqueKey( Function function) { return this.groupByUniqueKey(function, UnifiedMap.newMap()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Deprecated public MutableSet> zip(Iterable that) { return this.zip(that, UnifiedSet.>newSet()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Deprecated public MutableSet> zipWithIndex() { return this.zipWithIndex(UnifiedSet.>newSet()); } public RichIterable> chunk(int size) { return MutableCollectionIterate.chunk(this, size); } public MutableSet union(SetIterable set) { return SetIterables.unionInto(this, set, this.newEmpty()); } public > R unionInto(SetIterable set, R targetSet) { return SetIterables.unionInto(this, set, targetSet); } public MutableSet intersect(SetIterable set) { return SetIterables.intersectInto(this, set, this.newEmpty()); } public > R intersectInto(SetIterable set, R targetSet) { return SetIterables.intersectInto(this, set, targetSet); } public MutableSet difference(SetIterable subtrahendSet) { return SetIterables.differenceInto(this, subtrahendSet, this.newEmpty()); } public > R differenceInto(SetIterable subtrahendSet, R targetSet) { return SetIterables.differenceInto(this, subtrahendSet, targetSet); } public MutableSet symmetricDifference(SetIterable setB) { return SetIterables.symmetricDifferenceInto(this, setB, this.newEmpty()); } public > R symmetricDifferenceInto(SetIterable set, R targetSet) { return SetIterables.symmetricDifferenceInto(this, set, targetSet); } public boolean isSubsetOf(SetIterable candidateSuperset) { return SetIterables.isSubsetOf(this, candidateSuperset); } public boolean isProperSubsetOf(SetIterable candidateSuperset) { return SetIterables.isProperSubsetOf(this, candidateSuperset); } public MutableSet> powerSet() { return (MutableSet>) (MutableSet) SetIterables.powerSet(this); } public LazyIterable> cartesianProduct(SetIterable set) { return SetIterables.cartesianProduct(this, set); } public MutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public MutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } }