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

com.gs.collections.impl.set.mutable.AbstractMutableSet 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.mutable;

import java.util.Set;

import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.Function2;
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.Procedure;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.api.partition.set.PartitionMutableSet;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
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.block.procedure.PartitionPredicate2Procedure;
import com.gs.collections.impl.block.procedure.PartitionProcedure;
import com.gs.collections.impl.block.procedure.SelectInstancesOfProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectBooleanProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectByteProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectCharProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectDoubleProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectFloatProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectIntProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectLongProcedure;
import com.gs.collections.impl.block.procedure.primitive.CollectShortProcedure;
import com.gs.collections.impl.collection.mutable.AbstractMutableCollection;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.multimap.set.UnifiedSetMultimap;
import com.gs.collections.impl.partition.set.PartitionUnifiedSet;
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.SetIterables;
import com.gs.collections.impl.utility.internal.SetIterate;

public abstract class AbstractMutableSet
        extends AbstractMutableCollection
        implements MutableSet
{
    @Override
    public MutableSet clone()
    {
        try
        {
            return (MutableSet) super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            throw new AssertionError(e);
        }
    }

    public MutableSet newEmpty()
    {
        return UnifiedSet.newSet();
    }

    protected  MutableSet newEmptySameSize()
    {
        return UnifiedSet.newSet(this.size());
    }

    public MutableSet tap(Procedure procedure)
    {
        this.forEach(procedure);
        return this;
    }

    public MutableSet select(Predicate predicate)
    {
        return this.select(predicate, this.newEmpty());
    }

    public 

MutableSet selectWith(Predicate2 predicate, P parameter) { return this.selectWith(predicate, parameter, this.newEmptySameSize()); } public MutableSet reject(Predicate predicate) { return this.reject(predicate, this.newEmptySameSize()); } public

MutableSet rejectWith(Predicate2 predicate, P parameter) { return this.rejectWith(predicate, parameter, this.newEmptySameSize()); } public PartitionMutableSet partition(Predicate predicate) { PartitionMutableSet partitionMutableSet = new PartitionUnifiedSet(); this.forEach(new PartitionProcedure(predicate, partitionMutableSet)); return partitionMutableSet; } public

PartitionMutableSet partitionWith(Predicate2 predicate, P parameter) { PartitionMutableSet partitionMutableSet = new PartitionUnifiedSet(); this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionMutableSet)); return partitionMutableSet; } public MutableSet selectInstancesOf(Class clazz) { MutableSet result = (MutableSet) this.newEmpty(); this.forEach(new SelectInstancesOfProcedure(clazz, result)); return result; } public MutableSet collect(Function function) { return this.collect(function, this.newEmptySameSize()); } public MutableBooleanSet collectBoolean(BooleanFunction booleanFunction) { MutableBooleanSet result = new BooleanHashSet(); this.forEach(new CollectBooleanProcedure(booleanFunction, result)); return result; } public MutableByteSet collectByte(ByteFunction byteFunction) { MutableByteSet result = new ByteHashSet(); this.forEach(new CollectByteProcedure(byteFunction, result)); return result; } public MutableCharSet collectChar(CharFunction charFunction) { MutableCharSet result = new CharHashSet(this.size()); this.forEach(new CollectCharProcedure(charFunction, result)); return result; } public MutableDoubleSet collectDouble(DoubleFunction doubleFunction) { MutableDoubleSet result = new DoubleHashSet(this.size()); this.forEach(new CollectDoubleProcedure(doubleFunction, result)); return result; } public MutableFloatSet collectFloat(FloatFunction floatFunction) { MutableFloatSet result = new FloatHashSet(this.size()); this.forEach(new CollectFloatProcedure(floatFunction, result)); return result; } public MutableIntSet collectInt(IntFunction intFunction) { MutableIntSet result = new IntHashSet(this.size()); this.forEach(new CollectIntProcedure(intFunction, result)); return result; } public MutableLongSet collectLong(LongFunction longFunction) { MutableLongSet result = new LongHashSet(this.size()); this.forEach(new CollectLongProcedure(longFunction, result)); return result; } public MutableShortSet collectShort(ShortFunction shortFunction) { MutableShortSet result = new ShortHashSet(this.size()); this.forEach(new CollectShortProcedure(shortFunction, result)); return result; } public MutableSet flatCollect(Function> function) { return this.flatCollect(function, this.newEmptySameSize()); } public MutableSet collectWith(Function2 function, P parameter) { return this.collectWith(function, parameter, this.newEmptySameSize()); } public MutableSet collectIf(Predicate predicate, Function function) { return this.collectIf(predicate, function, this.newEmptySameSize()); } public UnifiedSetMultimap groupBy(Function function) { return this.groupBy(function, UnifiedSetMultimap.newMultimap()); } public UnifiedSetMultimap groupByEach(Function> function) { return this.groupByEach(function, UnifiedSetMultimap.newMultimap()); } public MutableSet asUnmodifiable() { return UnmodifiableMutableSet.of(this); } public MutableSet asSynchronized() { return SynchronizedMutableSet.of(this); } public ImmutableSet toImmutable() { return Sets.immutable.withAll(this); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zip(Iterable)} instead. */ @Deprecated public MutableSet> zip(Iterable that) { return this.zip(that, this.>newEmptySameSize()); } /** * @deprecated in 6.0. Use {@link OrderedIterable#zipWithIndex()} instead. */ @Deprecated public MutableSet> zipWithIndex() { return this.zipWithIndex(this.>newEmptySameSize()); } @Override public boolean removeAllIterable(Iterable iterable) { return SetIterate.removeAllIterable(this, iterable); } public MutableSet union(SetIterable set) { return SetIterables.union(this, set); } public > R unionInto(SetIterable set, R targetSet) { return SetIterables.unionInto(this, set, targetSet); } public MutableSet intersect(SetIterable set) { return SetIterables.intersect(this, set); } public > R intersectInto(SetIterable set, R targetSet) { return SetIterables.intersectInto(this, set, targetSet); } public MutableSet difference(SetIterable subtrahendSet) { return SetIterables.difference(this, subtrahendSet); } public > R differenceInto(SetIterable subtrahendSet, R targetSet) { return SetIterables.differenceInto(this, subtrahendSet, targetSet); } public MutableSet symmetricDifference(SetIterable setB) { return SetIterables.symmetricDifference(this, setB); } 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); } }