/* * 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.map.mutable; import java.util.Iterator; 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.predicate.Predicate2; import com.gs.collections.api.block.procedure.Procedure2; import com.gs.collections.api.map.MutableMap; import com.gs.collections.api.map.MutableMapIterable; import com.gs.collections.api.tuple.Pair; import com.gs.collections.impl.block.procedure.MutatingAggregationProcedure; import com.gs.collections.impl.block.procedure.NonMutatingAggregationProcedure; import com.gs.collections.impl.map.AbstractMapIterable; import com.gs.collections.impl.tuple.AbstractImmutableEntry; import com.gs.collections.impl.utility.LazyIterate; import com.gs.collections.impl.utility.MapIterate; public abstract class AbstractMutableMapIterable<K, V> extends AbstractMapIterable<K, V> implements MutableMapIterable<K, V> { public Iterator<V> iterator() { return this.values().iterator(); } public V getIfAbsentPut(K key, Function0<? extends V> function) { V result = this.get(key); if (this.isAbsent(result, key)) { result = function.value(); this.put(key, result); } return result; } public V getIfAbsentPut(K key, V value) { V result = this.get(key); if (this.isAbsent(result, key)) { result = value; this.put(key, result); } return result; } public V getIfAbsentPutWithKey(K key, Function<? super K, ? extends V> function) { return this.getIfAbsentPutWith(key, function, key); } public <P> V getIfAbsentPutWith(K key, Function<? super P, ? extends V> function, P parameter) { V result = this.get(key); if (this.isAbsent(result, key)) { result = function.valueOf(parameter); this.put(key, result); } return result; } public V add(Pair<K, V> keyValuePair) { return this.put(keyValuePair.getOne(), keyValuePair.getTwo()); } public V updateValue(K key, Function0<? extends V> factory, Function<? super V, ? extends V> function) { V oldValue = this.getIfAbsent(key, factory); V newValue = function.valueOf(oldValue); this.put(key, newValue); return newValue; } public <P> V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V, ? super P, ? extends V> function, P parameter) { V oldValue = this.getIfAbsent(key, factory); V newValue = function.value(oldValue, parameter); this.put(key, newValue); return newValue; } public <V1> MutableMap<V1, V> groupByUniqueKey(Function<? super V, ? extends V1> function) { return this.groupByUniqueKey(function, UnifiedMap.<V1, V>newMap()); } public <K2, V2> MutableMap<K2, V2> aggregateInPlaceBy( Function<? super V, ? extends K2> groupBy, Function0<? extends V2> zeroValueFactory, Procedure2<? super V2, ? super V> mutatingAggregator) { MutableMap<K2, V2> map = UnifiedMap.newMap(); this.forEach(new MutatingAggregationProcedure<V, K2, V2>(map, groupBy, zeroValueFactory, mutatingAggregator)); return map; } public <K2, V2> MutableMap<K2, V2> aggregateBy( Function<? super V, ? extends K2> groupBy, Function0<? extends V2> zeroValueFactory, Function2<? super V2, ? super V, ? extends V2> nonMutatingAggregator) { MutableMap<K2, V2> map = UnifiedMap.newMap(); this.forEach(new NonMutatingAggregationProcedure<V, K2, V2>(map, groupBy, zeroValueFactory, nonMutatingAggregator)); return map; } public RichIterable<K> keysView() { return LazyIterate.adapt(this.keySet()); } public RichIterable<V> valuesView() { return LazyIterate.adapt(this.values()); } public RichIterable<Pair<K, V>> keyValuesView() { return LazyIterate.adapt(this.entrySet()).collect(AbstractImmutableEntry.<K, V>getPairFunction()); } public <K2, V2> MutableMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function) { return MapIterate.collect(this, function, UnifiedMap.<K2, V2>newMap(this.size())); } public MutableMap<V, K> flipUniqueValues() { return MapIterate.flipUniqueValues(this); } public Pair<K, V> detect(Predicate2<? super K, ? super V> predicate) { return MapIterate.detect(this, predicate); } }