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

com.gs.collections.impl.bag.immutable.ImmutableEmptyBag 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 2014 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.bag.immutable;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.Bag;
import com.gs.collections.api.bag.ImmutableBag;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.primitive.ImmutableBooleanBag;
import com.gs.collections.api.bag.primitive.ImmutableByteBag;
import com.gs.collections.api.bag.primitive.ImmutableCharBag;
import com.gs.collections.api.bag.primitive.ImmutableDoubleBag;
import com.gs.collections.api.bag.primitive.ImmutableFloatBag;
import com.gs.collections.api.bag.primitive.ImmutableIntBag;
import com.gs.collections.api.bag.primitive.ImmutableLongBag;
import com.gs.collections.api.bag.primitive.ImmutableShortBag;
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.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.DoubleObjectToDoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.FloatObjectToFloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.IntObjectToIntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.LongObjectToLongFunction;
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.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.collection.primitive.MutableByteCollection;
import com.gs.collections.api.collection.primitive.MutableCharCollection;
import com.gs.collections.api.collection.primitive.MutableDoubleCollection;
import com.gs.collections.api.collection.primitive.MutableFloatCollection;
import com.gs.collections.api.collection.primitive.MutableIntCollection;
import com.gs.collections.api.collection.primitive.MutableLongCollection;
import com.gs.collections.api.collection.primitive.MutableShortCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.map.sorted.MutableSortedMap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.multimap.bag.ImmutableBagMultimap;
import com.gs.collections.api.partition.bag.PartitionImmutableBag;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.sorted.MutableSortedSet;
import com.gs.collections.api.stack.MutableStack;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.EmptyIterator;
import com.gs.collections.impl.bag.mutable.HashBag;
import com.gs.collections.impl.block.factory.Comparators;
import com.gs.collections.impl.factory.Bags;
import com.gs.collections.impl.factory.Lists;
import com.gs.collections.impl.factory.Maps;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.factory.Stacks;
import com.gs.collections.impl.factory.primitive.BooleanBags;
import com.gs.collections.impl.factory.primitive.ByteBags;
import com.gs.collections.impl.factory.primitive.CharBags;
import com.gs.collections.impl.factory.primitive.DoubleBags;
import com.gs.collections.impl.factory.primitive.FloatBags;
import com.gs.collections.impl.factory.primitive.IntBags;
import com.gs.collections.impl.factory.primitive.LongBags;
import com.gs.collections.impl.factory.primitive.ShortBags;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.sorted.mutable.TreeSortedMap;
import com.gs.collections.impl.multimap.bag.HashBagMultimap;
import com.gs.collections.impl.partition.bag.PartitionHashBag;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.sorted.mutable.TreeSortedSet;
import com.gs.collections.impl.utility.ArrayIterate;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.LazyIterate;
import net.jcip.annotations.Immutable;

/**
 * This is a zero element {@link ImmutableBag} which is created by calling the Bags.immutable.of().
 *
 * @since 1.0
 */
@Immutable
final class ImmutableEmptyBag
        implements ImmutableBag, Serializable
{
    static final ImmutableBag INSTANCE = new ImmutableEmptyBag();

    private static final long serialVersionUID = 1L;

    private static final LazyIterable LAZY_ITERABLE = LazyIterate.adapt(INSTANCE);
    private static final Object[] TO_ARRAY = new Object[0];
    private static final PartitionImmutableBag IMMUTABLE_EMPTY_PARTITION = new PartitionHashBag().toImmutable();

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }
        return obj instanceof Bag && ((Bag) obj).isEmpty();
    }

    public int sizeDistinct()
    {
        return 0;
    }

    public String toStringOfItemToCount()
    {
        return "";
    }

    public int occurrencesOf(Object item)
    {
        return 0;
    }

    public void forEachWithOccurrences(ObjectIntProcedure objectIntProcedure)
    {
    }

    public MutableMap toMapOfItemToCount()
    {
        return Maps.mutable.of();
    }

    public ImmutableBag toImmutable()
    {
        return this;
    }

    @Override
    public int hashCode()
    {
        return 0;
    }

    public int size()
    {
        return 0;
    }

    public boolean isEmpty()
    {
        return true;
    }

    public boolean notEmpty()
    {
        return false;
    }

    public boolean contains(Object object)
    {
        return false;
    }

    public boolean containsAll(Collection source)
    {
        return this.containsAllIterable(source);
    }

    public boolean containsAllIterable(Iterable source)
    {
        return Iterate.isEmpty(source);
    }

    public boolean containsAllArguments(Object... elements)
    {
        return ArrayIterate.isEmpty(elements);
    }

    public void forEach(Procedure procedure)
    {
    }

    public void forEachWithIndex(ObjectIntProcedure objectIntProcedure)
    {
    }

    public 

void forEachWith(Procedure2 procedure, P parameter) { } public T getFirst() { return null; } public T getLast() { return null; } public Iterator iterator() { return EmptyIterator.getInstance(); } public ImmutableBag newWith(T element) { return Bags.immutable.of(element); } public ImmutableBag newWithout(T element) { return this; } public ImmutableBag newWithAll(Iterable elements) { return HashBag.newBag(elements).toImmutable(); } public ImmutableBag newWithoutAll(Iterable elements) { return this; } public ImmutableBag selectByOccurrences(IntPredicate predicate) { return this; } public ImmutableBag select(Predicate predicate) { return this; } public > R select(Predicate predicate, R target) { return target; } public

ImmutableBag selectWith(Predicate2 predicate, P parameter) { return this; } public > R selectWith( Predicate2 predicate, P parameter, R targetCollection) { return targetCollection; } public ImmutableBag reject(Predicate predicate) { return this; } public > R reject(Predicate predicate, R target) { return target; } public

ImmutableBag rejectWith(Predicate2 predicate, P parameter) { return this; } public > R rejectWith( Predicate2 predicate, P parameter, R targetCollection) { return targetCollection; } public PartitionImmutableBag partition(Predicate predicate) { return (PartitionImmutableBag) IMMUTABLE_EMPTY_PARTITION; } public

PartitionImmutableBag partitionWith(Predicate2 predicate, P parameter) { return (PartitionImmutableBag) IMMUTABLE_EMPTY_PARTITION; } public ImmutableBag selectInstancesOf(Class clazz) { return (ImmutableBag) INSTANCE; } public ImmutableBag collect(Function function) { return (ImmutableBag) INSTANCE; } public ImmutableBooleanBag collectBoolean(BooleanFunction booleanFunction) { return BooleanBags.immutable.of(); } public R collectBoolean(BooleanFunction booleanFunction, R target) { return target; } public ImmutableByteBag collectByte(ByteFunction byteFunction) { return ByteBags.immutable.of(); } public R collectByte(ByteFunction byteFunction, R target) { return target; } public ImmutableCharBag collectChar(CharFunction charFunction) { return CharBags.immutable.of(); } public R collectChar(CharFunction charFunction, R target) { return target; } public ImmutableDoubleBag collectDouble(DoubleFunction doubleFunction) { return DoubleBags.immutable.of(); } public R collectDouble(DoubleFunction doubleFunction, R target) { return target; } public ImmutableFloatBag collectFloat(FloatFunction floatFunction) { return FloatBags.immutable.of(); } public R collectFloat(FloatFunction floatFunction, R target) { return target; } public ImmutableIntBag collectInt(IntFunction intFunction) { return IntBags.immutable.of(); } public R collectInt(IntFunction intFunction, R target) { return target; } public ImmutableLongBag collectLong(LongFunction longFunction) { return LongBags.immutable.of(); } public R collectLong(LongFunction longFunction, R target) { return target; } public ImmutableShortBag collectShort(ShortFunction shortFunction) { return ShortBags.immutable.of(); } public R collectShort(ShortFunction shortFunction, R target) { return target; } public > R collect(Function function, R target) { return target; } public ImmutableBag collectWith(Function2 function, P parameter) { return (ImmutableBag) INSTANCE; } public > R collectWith( Function2 function, P parameter, R targetCollection) { return targetCollection; } public ImmutableBag collectIf( Predicate predicate, Function function) { return (ImmutableBag) INSTANCE; } public > R collectIf( Predicate predicate, Function function, R target) { return target; } public ImmutableBag flatCollect(Function> function) { return (ImmutableBag) INSTANCE; } public > R flatCollect( Function> function, R target) { return target; } public ImmutableBagMultimap groupBy(Function function) { // TODO: Create a Singleton ImmutableEmptyBagMultimap for efficiency return HashBagMultimap.newMultimap().toImmutable(); } public ImmutableBagMultimap groupByEach(Function> function) { // TODO: Create a Singleton ImmutableEmptyBagMultimap for efficiency return HashBagMultimap.newMultimap().toImmutable(); } public > R groupBy( Function function, R target) { return target; } public > R groupByEach( Function> function, R target) { return target; } public ImmutableMap groupByUniqueKey(Function function) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet"); } public T detect(Predicate predicate) { return null; } public

T detectWith(Predicate2 predicate, P parameter) { return null; } public T detectIfNone(Predicate predicate, Function0 function) { return function.value(); } public

T detectWithIfNone(Predicate2 predicate, P parameter, Function0 function) { return function.value(); } public int count(Predicate predicate) { return 0; } public

int countWith(Predicate2 predicate, P parameter) { return 0; } public boolean anySatisfy(Predicate predicate) { return false; } public boolean allSatisfy(Predicate predicate) { return true; } public boolean noneSatisfy(Predicate predicate) { return true; } public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return false; } public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return true; } public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return true; } public IV injectInto(IV injectedValue, Function2 function) { return injectedValue; } public int injectInto(int injectedValue, IntObjectToIntFunction function) { return injectedValue; } public long injectInto(long injectedValue, LongObjectToLongFunction function) { return injectedValue; } public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { return injectedValue; } public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return injectedValue; } public long sumOfInt(IntFunction function) { return 0; } public double sumOfFloat(FloatFunction function) { return 0.0F; } public long sumOfLong(LongFunction function) { return 0L; } public double sumOfDouble(DoubleFunction function) { return 0.0d; } public MutableList toList() { return Lists.mutable.of(); } public MutableList toSortedList() { return Lists.mutable.of(); } public MutableList toSortedList(Comparator comparator) { return Lists.mutable.of(); } public > MutableList toSortedListBy(Function function) { return Lists.mutable.of(); } public MutableSortedSet toSortedSet() { return TreeSortedSet.newSet(); } public MutableSortedSet toSortedSet(Comparator comparator) { return TreeSortedSet.newSet(comparator); } public > MutableSortedSet toSortedSetBy(Function function) { return TreeSortedSet.newSet(Comparators.byFunction(function)); } public MutableSet toSet() { return UnifiedSet.newSet(); } public MutableBag toBag() { return Bags.mutable.of(); } public MutableStack toStack() { return Stacks.mutable.of(); } public MutableMap toMap( Function keyFunction, Function valueFunction) { return UnifiedMap.newMap(); } public MutableSortedMap toSortedMap( Function keyFunction, Function valueFunction) { return TreeSortedMap.newMap(); } public MutableSortedMap toSortedMap(Comparator comparator, Function keyFunction, Function valueFunction) { return TreeSortedMap.newMap(comparator); } public LazyIterable asLazy() { return (LazyIterable) LAZY_ITERABLE; } public Object[] toArray() { return TO_ARRAY; } public T[] toArray(T[] a) { if (a.length > 0) { a[0] = null; } return a; } public T min(Comparator comparator) { throw new NoSuchElementException(); } public T max(Comparator comparator) { throw new NoSuchElementException(); } public T min() { throw new NoSuchElementException(); } public T max() { throw new NoSuchElementException(); } public > T minBy(Function function) { throw new NoSuchElementException(); } public > T maxBy(Function function) { throw new NoSuchElementException(); } @Override public String toString() { return "[]"; } public String makeString() { return ""; } public String makeString(String separator) { return ""; } public String makeString(String start, String separator, String end) { return start + end; } public void appendString(Appendable appendable) { } public void appendString(Appendable appendable, String separator) { } public void appendString(Appendable appendable, String start, String separator, String end) { try { appendable.append(start); appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } } public ImmutableBag> zip(Iterable that) { return Bags.immutable.of(); } public >> R zip(Iterable that, R target) { return target; } public ImmutableSet> zipWithIndex() { return Sets.immutable.of(); } public >> R zipWithIndex(R target) { return target; } public RichIterable> chunk(int size) { if (size <= 0) { throw new IllegalArgumentException("Size for groups must be positive but was: " + size); } return Bags.immutable.of(); } private Object writeReplace() { return new ImmutableBagSerializationProxy(this); } public ImmutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { return Maps.immutable.of(); } public ImmutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { return Maps.immutable.of(); } }