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

com.gs.collections.impl.map.sorted.immutable.AbstractImmutableSortedMap 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.sorted.immutable;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;

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.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.list.ImmutableList;
import com.gs.collections.api.list.primitive.ImmutableBooleanList;
import com.gs.collections.api.list.primitive.ImmutableByteList;
import com.gs.collections.api.list.primitive.ImmutableCharList;
import com.gs.collections.api.list.primitive.ImmutableDoubleList;
import com.gs.collections.api.list.primitive.ImmutableFloatList;
import com.gs.collections.api.list.primitive.ImmutableIntList;
import com.gs.collections.api.list.primitive.ImmutableLongList;
import com.gs.collections.api.list.primitive.ImmutableShortList;
import com.gs.collections.api.map.ImmutableMap;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.map.sorted.ImmutableSortedMap;
import com.gs.collections.api.map.sorted.MutableSortedMap;
import com.gs.collections.api.multimap.list.ImmutableListMultimap;
import com.gs.collections.api.multimap.sortedset.ImmutableSortedSetMultimap;
import com.gs.collections.api.partition.PartitionIterable;
import com.gs.collections.api.partition.list.PartitionImmutableList;
import com.gs.collections.api.partition.list.PartitionMutableList;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.tuple.Pair;
import com.gs.collections.impl.block.factory.Functions;
import com.gs.collections.impl.block.factory.Predicates;
import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure;
import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure;
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.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.ByteArrayList;
import com.gs.collections.impl.list.mutable.primitive.CharArrayList;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.impl.list.mutable.primitive.LongArrayList;
import com.gs.collections.impl.list.mutable.primitive.ShortArrayList;
import com.gs.collections.impl.map.AbstractMapIterable;
import com.gs.collections.impl.map.mutable.UnifiedMap;
import com.gs.collections.impl.map.sorted.mutable.TreeSortedMap;
import com.gs.collections.impl.multimap.list.FastListMultimap;
import com.gs.collections.impl.partition.list.PartitionFastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.tuple.ImmutableEntry;
import com.gs.collections.impl.utility.MapIterate;
import net.jcip.annotations.Immutable;

@Immutable
public abstract class AbstractImmutableSortedMap
        extends AbstractMapIterable
        implements ImmutableSortedMap, SortedMap
{
    public SortedMap castToSortedMap()
    {
        return this;
    }

    public MutableSortedMap toSortedMap()
    {
        return TreeSortedMap.newMap(this);
    }

    public ImmutableSortedMap toImmutable()
    {
        return this;
    }

    public Iterator iterator()
    {
        return this.valuesView().iterator();
    }

    public void putAll(Map map)
    {
        throw new UnsupportedOperationException("Cannot call putAll() on " + this.getClass().getSimpleName());
    }

    public void clear()
    {
        throw new UnsupportedOperationException("Cannot call clear() on " + this.getClass().getSimpleName());
    }

    public Set> entrySet()
    {
        final MutableSet> set = UnifiedSet.newSet(this.size());
        this.forEachKeyValue(new Procedure2()
        {
            public void value(K key, V value)
            {
                set.add(ImmutableEntry.of(key, value));
            }
        });
        return set.toImmutable().castToSet();
    }

    public ImmutableSortedMap newWithKeyValue(K key, V value)
    {
        TreeSortedMap sortedMap = TreeSortedMap.newMap(this);
        sortedMap.put(key, value);
        return sortedMap.toImmutable();
    }

    public ImmutableSortedMap newWithAllKeyValues(Iterable> keyValues)
    {
        TreeSortedMap sortedMap = TreeSortedMap.newMap(this);
        for (Pair keyValuePair : keyValues)
        {
            sortedMap.put(keyValuePair.getOne(), keyValuePair.getTwo());
        }
        return sortedMap.toImmutable();
    }

    public ImmutableSortedMap newWithAllKeyValueArguments(Pair... keyValuePairs)
    {
        TreeSortedMap sortedMap = TreeSortedMap.newMap(this);
        for (Pair keyValuePair : keyValuePairs)
        {
            sortedMap.put(keyValuePair.getOne(), keyValuePair.getTwo());
        }
        return sortedMap.toImmutable();
    }

    public ImmutableSortedMap newWithoutKey(K key)
    {
        TreeSortedMap sortedMap = TreeSortedMap.newMap(this);
        sortedMap.removeKey(key);
        return sortedMap.toImmutable();
    }

    public ImmutableSortedMap newWithoutAllKeys(Iterable keys)
    {
        TreeSortedMap sortedMap = TreeSortedMap.newMap(this);
        for (K key : keys)
        {
            sortedMap.removeKey(key);
        }
        return sortedMap.toImmutable();
    }

    public V put(K key, V value)
    {
        throw new UnsupportedOperationException("Cannot call put() on " + this.getClass().getSimpleName());
    }

    public V remove(Object key)
    {
        throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName());
    }

    public ImmutableSortedSetMultimap flip()
    {
        return MapIterate.flip(this).toImmutable();
    }

    public ImmutableList select(Predicate predicate)
    {
        return this.select(predicate, FastList.newList(this.size())).toImmutable();
    }

    @Override
    public 

ImmutableList selectWith(Predicate2 predicate, P parameter) { return this.select(Predicates.bind(predicate, parameter)); } public ImmutableSortedMap select(Predicate2 predicate) { return MapIterate.selectMapOnEntry(this, predicate, TreeSortedMap.newMap(this.comparator())).toImmutable(); } public ImmutableList reject(Predicate predicate) { return this.reject(predicate, FastList.newList(this.size())).toImmutable(); } @Override public

ImmutableList rejectWith(Predicate2 predicate, P parameter) { return this.reject(Predicates.bind(predicate, parameter)); } public ImmutableSortedMap reject(Predicate2 predicate) { return MapIterate.rejectMapOnEntry(this, predicate, TreeSortedMap.newMap(this.comparator())).toImmutable(); } public PartitionImmutableList partition(Predicate predicate) { PartitionMutableList partitionFastList = new PartitionFastList(); this.forEach(new PartitionProcedure(predicate, partitionFastList)); return partitionFastList.toImmutable(); } public

PartitionIterable partitionWith(Predicate2 predicate, P parameter) { PartitionMutableList partitionFastList = new PartitionFastList(); this.forEach(new PartitionPredicate2Procedure(predicate, parameter, partitionFastList)); return partitionFastList.toImmutable(); } public ImmutableList selectInstancesOf(Class clazz) { FastList result = FastList.newList(this.size()); this.forEach(new SelectInstancesOfProcedure(clazz, result)); return result.toImmutable(); } @Override public ImmutableList collect(Function function) { return this.collect(function, FastList.newList(this.size())).toImmutable(); } @Override public ImmutableBooleanList collectBoolean(BooleanFunction booleanFunction) { BooleanArrayList result = new BooleanArrayList(this.size()); this.forEach(new CollectBooleanProcedure(booleanFunction, result)); return result.toImmutable(); } public ImmutableByteList collectByte(ByteFunction byteFunction) { ByteArrayList result = new ByteArrayList(this.size()); this.forEach(new CollectByteProcedure(byteFunction, result)); return result.toImmutable(); } public ImmutableCharList collectChar(CharFunction charFunction) { CharArrayList result = new CharArrayList(this.size()); this.forEach(new CollectCharProcedure(charFunction, result)); return result.toImmutable(); } public ImmutableDoubleList collectDouble(DoubleFunction doubleFunction) { DoubleArrayList result = new DoubleArrayList(this.size()); this.forEach(new CollectDoubleProcedure(doubleFunction, result)); return result.toImmutable(); } public ImmutableFloatList collectFloat(FloatFunction floatFunction) { FloatArrayList result = new FloatArrayList(this.size()); this.forEach(new CollectFloatProcedure(floatFunction, result)); return result.toImmutable(); } public ImmutableIntList collectInt(IntFunction intFunction) { IntArrayList result = new IntArrayList(this.size()); this.forEach(new CollectIntProcedure(intFunction, result)); return result.toImmutable(); } public ImmutableLongList collectLong(LongFunction longFunction) { LongArrayList result = new LongArrayList(this.size()); this.forEach(new CollectLongProcedure(longFunction, result)); return result.toImmutable(); } public ImmutableShortList collectShort(ShortFunction shortFunction) { ShortArrayList result = new ShortArrayList(this.size()); this.forEach(new CollectShortProcedure(shortFunction, result)); return result.toImmutable(); } public ImmutableMap collect(Function2> function) { return MapIterate.collect(this, function, UnifiedMap.newMap()).toImmutable(); } @Override public ImmutableList collectWith(Function2 function, P parameter) { return this.collect(Functions.bind(function, parameter)); } public ImmutableList collectIf(Predicate predicate, Function function) { return this.collectIf(predicate, function, FastList.newList(this.size())).toImmutable(); } public ImmutableSortedMap collectValues(Function2 function) { return MapIterate.collectValues(this, function, TreeSortedMap.newMap(this.comparator())).toImmutable(); } public Pair detect(Predicate2 predicate) { return MapIterate.detect(this, predicate); } @Override public

V detectWith(Predicate2 predicate, P parameter) { return this.detect(Predicates.bind(predicate, parameter)); } @Override public

V detectWithIfNone(Predicate2 predicate, P parameter, Function0 function) { return this.detectIfNone(Predicates.bind(predicate, parameter), function); } public ImmutableList flatCollect(Function> function) { return this.flatCollect(function, FastList.newList(this.size())).toImmutable(); } public ImmutableList> zip(Iterable that) { return this.zip(that, FastList.>newList(this.size())).toImmutable(); } public ImmutableList> zipWithIndex() { return this.zipWithIndex(FastList.>newList(this.size())).toImmutable(); } public SortedMap subMap(K fromKey, K toKey) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".subMap() not implemented yet"); } public SortedMap headMap(K toKey) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".headMap() not implemented yet"); } public SortedMap tailMap(K fromKey) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".tailMap() not implemented yet"); } public ImmutableListMultimap groupBy(Function function) { return this.groupBy(function, FastListMultimap.newMultimap()).toImmutable(); } public ImmutableListMultimap groupByEach(Function> function) { return this.groupByEach(function, FastListMultimap.newMultimap()).toImmutable(); } public ImmutableMap groupByUniqueKey(Function function) { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".groupByUniqueKey() not implemented yet"); } public ImmutableMap aggregateInPlaceBy( Function groupBy, Function0 zeroValueFactory, Procedure2 mutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure(map, groupBy, zeroValueFactory, mutatingAggregator)); return map.toImmutable(); } public ImmutableMap aggregateBy( Function groupBy, Function0 zeroValueFactory, Function2 nonMutatingAggregator) { MutableMap map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map.toImmutable(); } }