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

com.gs.collections.impl.map.immutable.primitive.ImmutableShortObjectSingletonMap Maven / Gradle / Ivy

/*
 * 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.map.immutable.primitive;

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

import com.gs.collections.api.LazyShortIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.ShortIterable;
import com.gs.collections.api.bag.MutableBag;
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.ShortObjectPredicate;
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.block.procedure.primitive.ShortObjectProcedure;
import com.gs.collections.api.block.procedure.primitive.ShortProcedure;
import com.gs.collections.api.collection.ImmutableCollection;
import com.gs.collections.api.collection.primitive.ImmutableBooleanCollection;
import com.gs.collections.api.collection.primitive.ImmutableByteCollection;
import com.gs.collections.api.collection.primitive.ImmutableCharCollection;
import com.gs.collections.api.collection.primitive.ImmutableDoubleCollection;
import com.gs.collections.api.collection.primitive.ImmutableFloatCollection;
import com.gs.collections.api.collection.primitive.ImmutableIntCollection;
import com.gs.collections.api.collection.primitive.ImmutableLongCollection;
import com.gs.collections.api.collection.primitive.ImmutableShortCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.map.MapIterable;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.map.primitive.ShortObjectMap;
import com.gs.collections.api.map.primitive.ImmutableShortObjectMap;
import com.gs.collections.api.map.sorted.MutableSortedMap;
import com.gs.collections.api.multimap.Multimap;
import com.gs.collections.api.multimap.MutableMultimap;
import com.gs.collections.api.partition.PartitionIterable;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.primitive.MutableShortSet;
import com.gs.collections.api.set.sorted.MutableSortedSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.api.tuple.primitive.ShortObjectPair;
import com.gs.collections.impl.UnmodifiableIteratorAdapter;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.PartitionProcedure;
import com.gs.collections.impl.block.procedure.PartitionPredicate2Procedure;
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.SortedMaps;
import com.gs.collections.impl.factory.SortedSets;
import com.gs.collections.impl.factory.primitive.BooleanLists;
import com.gs.collections.impl.factory.primitive.ByteLists;
import com.gs.collections.impl.factory.primitive.CharLists;
import com.gs.collections.impl.factory.primitive.DoubleLists;
import com.gs.collections.impl.factory.primitive.FloatLists;
import com.gs.collections.impl.factory.primitive.IntLists;
import com.gs.collections.impl.factory.primitive.LongLists;
import com.gs.collections.impl.factory.primitive.ShortLists;
import com.gs.collections.impl.lazy.LazyIterableAdapter;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.mutable.primitive.ShortObjectHashMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.primitive.ShortHashSet;
import com.gs.collections.impl.set.mutable.primitive.UnmodifiableShortSet;
import com.gs.collections.impl.tuple.Tuples;
import com.gs.collections.impl.tuple.primitive.PrimitiveTuples;
import com.gs.collections.impl.utility.Iterate;
import com.gs.collections.impl.utility.internal.IterableIterate;
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;

/**
 * ImmutableShortObjectSingletonMap is an optimization for {@link ImmutableShortObjectMap} of size 1.
 * This file was automatically generated from template file immutablePrimitiveObjectSingletonMap.stg.
 *
 * @since 4.0.
 */
final class ImmutableShortObjectSingletonMap extends AbstractImmutableShortObjectMap implements Serializable
{
    private static final long serialVersionUID = 1L;
    private final short key1;
    private final V value1;

    ImmutableShortObjectSingletonMap(short key1, V value1)
    {
        this.key1 = key1;
        this.value1 = value1;
    }

    public V get(short key)
    {
        return this.key1 == key ? this.value1 : null;
    }

    public V getIfAbsent(short key, Function0 ifAbsent)
    {
        return this.key1 == key ? this.value1 : ifAbsent.value();
    }

    public boolean containsKey(short key)
    {
        return this.key1 == key;
    }

    public boolean containsValue(Object value)
    {
        return nullSafeEquals(this.value1, value);
    }

    private static boolean nullSafeEquals(Object value, Object other)
    {
        if (value == null)
        {
            if (other == null)
            {
                return true;
            }
        }
        else if (other == value || value.equals(other))
        {
            return true;
        }
        return false;
    }

    public void forEachValue(Procedure procedure)
    {
        procedure.value(this.value1);
    }

    public void forEachKey(ShortProcedure procedure)
    {
        procedure.value(this.key1);
    }

    public void forEachKeyValue(ShortObjectProcedure procedure)
    {
        procedure.value(this.key1, this.value1);
    }

    public ImmutableShortObjectMap select(ShortObjectPredicate predicate)
    {
        return predicate.accept(this.key1, this.value1) ? ShortObjectHashMap.newWithKeysValues(this.key1, this.value1).toImmutable()
                : (ImmutableShortObjectMap) ImmutableShortObjectEmptyMap.INSTANCE;
    }

    public ImmutableShortObjectMap reject(ShortObjectPredicate predicate)
    {
        return predicate.accept(this.key1, this.value1) ? (ImmutableShortObjectMap) ImmutableShortObjectEmptyMap.INSTANCE
                : ShortObjectHashMap.newWithKeysValues(this.key1, this.value1).toImmutable();
    }

    public ImmutableShortObjectMap toImmutable()
    {
        return this;
    }

    public int size()
    {
        return 1;
    }

    public MutableShortSet keySet()
    {
        return UnmodifiableShortSet.of(ShortHashSet.newSetWith(this.key1));
    }

    public Collection values()
    {
        return Lists.immutable.of(this.value1).castToList();
    }

    public LazyShortIterable keysView()
    {
        return ShortLists.immutable.of(this.key1).asLazy();
    }

    public RichIterable> keyValuesView()
    {
        return Lists.immutable.of(PrimitiveTuples.pair(this.key1, this.value1)).asLazy();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this)
        {
            return true;
        }
        if (!(obj instanceof ShortObjectMap))
        {
            return false;
        }
        ShortObjectMap map = (ShortObjectMap) obj;
        if (map.size() != 1)
        {
            return false;
        }
        return map.containsKey(this.key1) && nullSafeEquals(this.value1, map.get(this.key1));
    }

    @Override
    public int hashCode()
    {
        return (int) this.key1 ^ (this.value1 == null ? 0 : this.value1.hashCode());
    }

    @Override
    public String toString()
    {
        return "{" + this.key1 + "=" + this.value1 + "}";
    }

    public boolean isEmpty()
    {
        return false;
    }

    public boolean notEmpty()
    {
        return true;
    }

    public V getFirst()
    {
        return this.value1;
    }

    public V getLast()
    {
        return this.value1;
    }

    public boolean contains(Object object)
    {
        return this.containsValue(object);
    }

    public boolean containsAllIterable(Iterable source)
    {
        for (Iterator iterator = source.iterator(); iterator.hasNext(); )
        {
            if (!nullSafeEquals(this.value1, iterator.next()))
            {
                return false;
            }
        }
        return true;
    }

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

    public boolean containsAllArguments(Object... elements)
    {
        for (Object item : elements)
        {
            if (!this.contains(item))
            {
                return false;
            }
        }
        return true;
    }

    public ImmutableCollection select(Predicate predicate)
    {
        return predicate.accept(this.value1) ? Lists.immutable.with(this.value1) : Lists.immutable.with();
    }

    public > R select(Predicate predicate, R target)
    {
        if (predicate.accept(this.value1))
        {
            target.add(this.value1);
        }
        return target;
    }

    public 

ImmutableCollection selectWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter) ? Lists.immutable.with(this.value1) : Lists.immutable.with(); } public > R selectWith(Predicate2 predicate, P parameter, R targetCollection) { if (predicate.accept(this.value1, parameter)) { targetCollection.add(this.value1); } return targetCollection; } public ImmutableCollection reject(Predicate predicate) { return predicate.accept(this.value1) ? Lists.immutable.with() : Lists.immutable.with(this.value1); } public > R reject(Predicate predicate, R target) { if (!predicate.accept(this.value1)) { target.add(this.value1); } return target; } public

ImmutableCollection rejectWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter) ? Lists.immutable.with() : Lists.immutable.with(this.value1); } public > R rejectWith(Predicate2 predicate, P parameter, R targetCollection) { if (!predicate.accept(this.value1, parameter)) { targetCollection.add(this.value1); } return targetCollection; } public PartitionIterable partition(Predicate predicate) { PartitionMutableList partitionMutableList = new PartitionFastList(); this.forEach(new PartitionProcedure(predicate, partitionMutableList)); return partitionMutableList; } public

PartitionIterable partitionWith(Predicate2 predicate, P parameter) { PartitionMutableList partitionMutableList = new PartitionFastList(); this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionMutableList)); return partitionMutableList; } public RichIterable selectInstancesOf(Class clazz) { return clazz.isInstance(this.value1) ? Lists.immutable.with(clazz.cast(this.value1)) : Lists.immutable.with(); } public ImmutableCollection collect(Function function) { return Lists.immutable.with(function.valueOf(this.value1)); } public ImmutableBooleanCollection collectBoolean(BooleanFunction booleanFunction) { return BooleanLists.immutable.with(booleanFunction.booleanValueOf(this.value1)); } public R collectBoolean(BooleanFunction booleanFunction, R target) { target.add(booleanFunction.booleanValueOf(this.value1)); return target; } public ImmutableByteCollection collectByte(ByteFunction byteFunction) { return ByteLists.immutable.with(byteFunction.byteValueOf(this.value1)); } public R collectByte(ByteFunction byteFunction, R target) { target.add(byteFunction.byteValueOf(this.value1)); return target; } public ImmutableCharCollection collectChar(CharFunction charFunction) { return CharLists.immutable.with(charFunction.charValueOf(this.value1)); } public R collectChar(CharFunction charFunction, R target) { target.add(charFunction.charValueOf(this.value1)); return target; } public ImmutableDoubleCollection collectDouble(DoubleFunction doubleFunction) { return DoubleLists.immutable.with(doubleFunction.doubleValueOf(this.value1)); } public R collectDouble(DoubleFunction doubleFunction, R target) { target.add(doubleFunction.doubleValueOf(this.value1)); return target; } public ImmutableFloatCollection collectFloat(FloatFunction floatFunction) { return FloatLists.immutable.with(floatFunction.floatValueOf(this.value1)); } public R collectFloat(FloatFunction floatFunction, R target) { target.add(floatFunction.floatValueOf(this.value1)); return target; } public ImmutableIntCollection collectInt(IntFunction intFunction) { return IntLists.immutable.with(intFunction.intValueOf(this.value1)); } public R collectInt(IntFunction intFunction, R target) { target.add(intFunction.intValueOf(this.value1)); return target; } public ImmutableLongCollection collectLong(LongFunction longFunction) { return LongLists.immutable.with(longFunction.longValueOf(this.value1)); } public R collectLong(LongFunction longFunction, R target) { target.add(longFunction.longValueOf(this.value1)); return target; } public ImmutableShortCollection collectShort(ShortFunction shortFunction) { return ShortLists.immutable.with(shortFunction.shortValueOf(this.value1)); } public R collectShort(ShortFunction shortFunction, R target) { target.add(shortFunction.shortValueOf(this.value1)); return target; } public RichIterable collectIf(Predicate predicate, Function function) { return predicate.accept(this.value1) ? Lists.immutable.with(function.valueOf(this.value1)) : Lists.immutable.with(); } public RichIterable flatCollect(Function> function) { return Lists.immutable.withAll(function.valueOf(this.value1)); } public V detect(Predicate predicate) { return predicate.accept(this.value1) ? this.value1 : null; } public

V detectWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter) ? this.value1 : null; } public V detectIfNone(Predicate predicate, Function0 function) { return predicate.accept(this.value1) ? this.value1 : function.value(); } public

V detectWithIfNone(Predicate2 predicate, P parameter, Function0 function) { return predicate.accept(this.value1, parameter) ? this.value1 : function.value(); } public int count(Predicate predicate) { return predicate.accept(this.value1) ? 1 : 0; } public

int countWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter) ? 1 : 0; } public boolean anySatisfy(Predicate predicate) { return predicate.accept(this.value1); } public

boolean anySatisfyWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter); } public boolean allSatisfy(Predicate predicate) { return predicate.accept(this.value1); } public

boolean allSatisfyWith(Predicate2 predicate, P parameter) { return predicate.accept(this.value1, parameter); } public boolean noneSatisfy(Predicate predicate) { return !predicate.accept(this.value1); } public

boolean noneSatisfyWith(Predicate2 predicate, P parameter) { return !predicate.accept(this.value1, parameter); } public IV injectInto(IV injectedValue, Function2 function) { return function.value(injectedValue, this.value1); } public int injectInto(int injectedValue, IntObjectToIntFunction function) { return function.intValueOf(injectedValue, this.value1); } public long injectInto(long injectedValue, LongObjectToLongFunction function) { return function.longValueOf(injectedValue, this.value1); } public float injectInto(float injectedValue, FloatObjectToFloatFunction function) { return function.floatValueOf(injectedValue, this.value1); } public double injectInto(double injectedValue, DoubleObjectToDoubleFunction function) { return function.doubleValueOf(injectedValue, this.value1); } public MutableList toList() { return Lists.mutable.with(this.value1); } public MutableList toSortedList() { return Lists.mutable.with(this.value1); } public MutableList toSortedList(Comparator comparator) { return Lists.mutable.with(this.value1); } public MutableSet toSet() { return Sets.mutable.with(this.value1); } public MutableSortedSet toSortedSet() { return SortedSets.mutable.with(this.value1); } public MutableSortedSet toSortedSet(Comparator comparator) { return SortedSets.mutable.with(this.value1); } public MutableBag toBag() { return Bags.mutable.with(this.value1); } public MutableMap toMap(Function keyFunction, Function valueFunction) { return Maps.mutable.with(keyFunction.valueOf(this.value1), valueFunction.valueOf(this.value1)); } public MutableSortedMap toSortedMap(Function keyFunction, Function valueFunction) { return SortedMaps.mutable.with(keyFunction.valueOf(this.value1), valueFunction.valueOf(this.value1)); } public MutableSortedMap toSortedMap(Comparator comparator, Function keyFunction, Function valueFunction) { return SortedMaps.mutable.with(keyFunction.valueOf(this.value1), valueFunction.valueOf(this.value1)); } public LazyIterable asLazy() { return new LazyIterableAdapter(FastList.newListWith(this.value1)); } public Object[] toArray() { return new Object[]{this.value1}; } public T[] toArray(T[] a) { return (T[]) new Object[]{this.value1}; } public V min(Comparator comparator) { return this.value1; } public V max(Comparator comparator) { return this.value1; } public V min() { return this.value1; } public V max() { return this.value1; } public long sumOfInt(IntFunction function) { return function.intValueOf(this.value1); } public double sumOfFloat(FloatFunction function) { return function.floatValueOf(this.value1); } public long sumOfLong(LongFunction function) { return function.longValueOf(this.value1); } public double sumOfDouble(DoubleFunction function) { return function.doubleValueOf(this.value1); } public String makeString() { return this.makeString(", "); } public String makeString(String separator) { return this.makeString("", separator, ""); } public String makeString(String start, String separator, String end) { Appendable stringBuilder = new StringBuilder(); this.appendString(stringBuilder, start, separator, end); return stringBuilder.toString(); } public void appendString(Appendable appendable) { this.appendString(appendable, ", "); } public void appendString(Appendable appendable, String separator) { this.appendString(appendable, "", separator, ""); } public void appendString(Appendable appendable, String start, String separator, String end) { try { appendable.append(start); appendable.append(String.valueOf(this.value1)); appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } } public Multimap groupBy(Function function) { return new FastListMultimap(Tuples.pair(function.valueOf(this.value1), this.value1)); } public Multimap groupByEach(Function> function) { return this.groupByEach(function, FastListMultimap.newMultimap()); } public MapIterable groupByUniqueKey(Function function) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet"); } public RichIterable> zip(Iterable that) { return this.zip(that, FastList.>newList()); } public >> R zip(Iterable that, R target) { return IterableIterate.zip(this, that, target); } public RichIterable> zipWithIndex() { return this.zipWithIndex(FastList.>newList()); } public >> R zipWithIndex(R target) { return IterableIterate.zipWithIndex(this, target); } public RichIterable> chunk(int size) { if (size <= 0) { throw new IllegalArgumentException("Size for groups must be positive but was: " + size); } MutableList> result = Lists.mutable.of(); result.add(FastList.newListWith(this.value1)); return result; } public MapIterable aggregateInPlaceBy(Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public MapIterable aggregateBy(Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } public > R groupByEach(Function> function, R target) { return IterableIterate.groupByEach(this, function, target); } public > R groupBy(Function function, R target) { target.put(function.valueOf(this.value1), this.value1); return target; } public > V maxBy(Function function) { return this.value1; } public > V minBy(Function function) { return this.value1; } public > MutableSortedSet toSortedSetBy(Function function) { return SortedSets.mutable.with(this.value1); } public > MutableList toSortedListBy(Function function) { return Lists.mutable.of(this.value1); } public > R flatCollect(Function> function, R target) { Iterate.addAllTo(function.valueOf(this.value1), target); return target; } public > R collectIf(Predicate predicate, Function function, R target) { if (predicate.accept(this.value1)) { target.add(function.valueOf(this.value1)); } return target; } public ImmutableCollection collectWith(Function2 function, P parameter) { return Lists.immutable.with(function.value(this.value1, parameter)); } public > R collectWith(Function2 function, P parameter, R targetCollection) { targetCollection.add(function.value(this.value1, parameter)); return targetCollection; } public > R collect(Function function, R target) { target.add(function.valueOf(this.value1)); return target; } public ImmutableShortObjectMap newWithKeyValue(short key, V value) { return ShortObjectHashMap.newWithKeysValues(this.key1, this.value1, key, value).toImmutable(); } public ImmutableShortObjectMap newWithoutKey(short key) { return this.key1 == key ? (ImmutableShortObjectMap) ImmutableShortObjectEmptyMap.INSTANCE : this; } public ImmutableShortObjectMap newWithoutAllKeys(ShortIterable keys) { return keys.contains(this.key1) ? (ImmutableShortObjectMap) ImmutableShortObjectEmptyMap.INSTANCE : this; } public void forEach(Procedure procedure) { procedure.value(this.value1); } public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { objectIntProcedure.value(this.value1, 0); } public

void forEachWith(Procedure2 procedure, P parameter) { procedure.value(this.value1, parameter); } public Iterator iterator() { return new UnmodifiableIteratorAdapter(ShortObjectHashMap.newWithKeysValues(this.key1, this.value1).iterator()); } private Object writeReplace() { return new ImmutableShortObjectMapSerializationProxy(this); } }