Skip to content

Commit

Permalink
Merge pull request #1709 from motlin/collectKeysUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
motlin authored Oct 31, 2024
2 parents 2c34f08 + a94562b commit 0e24ab1
Show file tree
Hide file tree
Showing 47 changed files with 511 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public interface BiMap<K, V> extends MapIterable<K, V>
@Override
<R> BiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> BiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
SetIterable<V> select(Predicate<? super V> predicate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public interface ImmutableBiMap<K, V> extends BiMap<K, V>, ImmutableMapIterable<
@Override
<R> ImmutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
ImmutableSet<V> select(Predicate<? super V> predicate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public interface MutableBiMap<K, V> extends BiMap<K, V>, MutableMapIterable<K, V
@Override
<R> MutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
MutableSet<V> select(Predicate<? super V> predicate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public interface ImmutableMap<K, V>
@Override
<R> ImmutableMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
<VV> ImmutableBag<VV> collect(Function<? super V, ? extends VV> function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public interface ImmutableMapIterable<K, V> extends MapIterable<K, V>
@Override
<R> ImmutableMapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableMapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
ImmutableCollection<V> select(Predicate<? super V> predicate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public interface ImmutableOrderedMap<K, V> extends OrderedMap<K, V>, ImmutableMa
@Override
<R> ImmutableOrderedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
ImmutableOrderedMap<K, V> toReversed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ default V getOrDefault(Object key, V defaultValue)
*/
<R> MapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

/**
* For each key and value of the map the function is evaluated. The results of these evaluations are returned in
* a new map. The map returned will use the keys projected from the function rather than the original keys.
*
* @throws IllegalStateException if the keys returned by the function are not unique
*
* @since 12.0
*/
<R> MapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

/**
* Return the first key and value of the map for which the predicate evaluates to true when they are given
* as arguments. The predicate will only be evaluated until such pair is found or until all the keys and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ <E> MutableMap<K, V> collectKeysAndValues(
@Override
<R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
<K2, V2> MutableMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ default void putAllMapIterable(MapIterable<? extends K, ? extends V> mapIterable
@Override
<R> MutableMapIterable<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableMapIterable<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
MutableCollection<V> select(Predicate<? super V> predicate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public interface MutableOrderedMap<K, V> extends OrderedMap<K, V>, MutableMapIte
@Override
<R> MutableOrderedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
MutableOrderedMap<K, V> toReversed();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.collections.api.list.primitive.ImmutableShortList;
import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.ImmutableMapIterable;
import org.eclipse.collections.api.map.ImmutableOrderedMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.multimap.list.ImmutableListMultimap;
Expand Down Expand Up @@ -142,6 +143,9 @@ default <R> ImmutableList<R> collectWithIndex(ObjectIntToObjectFunction<? super
@Override
<R> ImmutableSortedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> ImmutableList<R> collectIf(
Predicate<? super V> predicate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.map.MutableOrderedMap;
import org.eclipse.collections.api.multimap.list.MutableListMultimap;
import org.eclipse.collections.api.multimap.sortedset.MutableSortedSetMultimap;
import org.eclipse.collections.api.partition.list.PartitionMutableList;
Expand Down Expand Up @@ -100,6 +101,9 @@ <E> MutableSortedMap<K, V> collectKeysAndValues(
@Override
<R> MutableSortedMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
<R> MutableList<R> collect(Function<? super V, ? extends R> function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public <R> ImmutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ?
return BiMaps.immutable.withAll(result);
}

@Override
public <R> ImmutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
ImmutableMap<R, V> result = this.delegate.collectKeysUnique(function);
return BiMaps.immutable.withAll(result);
}

@Override
public ImmutableBooleanBag collectBoolean(BooleanFunction<? super V> booleanFunction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ public <R> HashBiMap<K, R> collectValues(Function2<? super K, ? super V, ? exten
return result;
}

@Override
public <R> MutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return MapIterate.collectKeysUnique(this.delegate, function, BiMaps.mutable.empty());
}

@Override
public <VV> MutableBag<VV> collect(Function<? super V, ? extends VV> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ public <R> MutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
}
}

@Override
public <R> MutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectKeysUnique(function);
}
}

@Override
public MutableSet<V> select(Predicate<? super V> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ public <R> MutableBiMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return this.delegate.collectValues(function);
}

@Override
public <R> MutableBiMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return this.delegate.collectKeysUnique(function);
}

@Override
public BooleanIterable collectBoolean(BooleanFunction<? super V> booleanFunction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public FixedSizeMap<K, V> tap(Procedure<? super V> procedure)
@Override
public abstract <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? extends R> function);

@Override
public abstract <R> FixedSizeMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function);

@Override
public abstract <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ public <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return Maps.fixedSize.of(this.key1, function.value(this.key1, this.value1), this.key2, function.value(this.key2, this.value2));
}

@Override
public <R> FixedSizeMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
R key1 = function.value(this.key1, this.value1);
R key2 = function.value(this.key2, this.value2);
if (Objects.equals(key1, key2))
{
throw new IllegalStateException("Key " + key1 + " already exists in map!");
}
return Maps.fixedSize.of(
key1,
this.value1,
key2,
this.value2);
}

@Override
public <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return Maps.fixedSize.of();
}

@Override
public <R> FixedSizeMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return Maps.fixedSize.of();
}

@Override
public <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ public <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return Maps.fixedSize.of(this.key1, function.value(this.key1, this.value1));
}

@Override
public <R> FixedSizeMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return Maps.fixedSize.of(function.value(this.key1, this.value1), this.value1);
}

@Override
public <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,33 @@ public <R> FixedSizeMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return Maps.fixedSize.of(this.key1, function.value(this.key1, this.value1), this.key2, function.value(this.key2, this.value2), this.key3, function.value(this.key3, this.value3));
}

@Override
public <R> FixedSizeMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
R key1 = function.value(this.key1, this.value1);
R key2 = function.value(this.key2, this.value2);
if (Objects.equals(key1, key2))
{
throw new IllegalStateException("Key " + key1 + " already exists in map!");
}
R key3 = function.value(this.key3, this.value3);
if (Objects.equals(key1, key3))
{
throw new IllegalStateException("Key " + key1 + " already exists in map!");
}
if (Objects.equals(key2, key3))
{
throw new IllegalStateException("Key " + key2 + " already exists in map!");
}
return Maps.fixedSize.of(
key1,
this.value1,
key2,
this.value2,
key3,
this.value3);
}

@Override
public <K2, V2> FixedSizeMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ public <R> ImmutableMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return result.toImmutable();
}

@Override
public <R> ImmutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
UnifiedMap<R, V> result = MapIterate.collectKeysUnique(this, function, UnifiedMap.newMap(this.size()));
return result.toImmutable();
}

@Override
public ImmutableMap<K, V> select(Predicate2<? super K, ? super V> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public <R> ImmutableMap<K, R> collectValues(Function2<? super K, ? super V, ? ex
return Maps.immutable.empty();
}

@Override
public <R> ImmutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return Maps.immutable.empty();
}

@Override
public Pair<K, V> detect(Predicate2<? super K, ? super V> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public <R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? exte
return MapIterate.collectValues(this, function, this.newEmpty(this.size()));
}

@Override
public <R> MutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return MapIterate.collectKeysUnique(this, function, this.newEmpty(this.size()));
}

@Override
public MutableMap<K, V> select(Predicate2<? super K, ? super V> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ public <R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? exte
}
}

@Override
public <R> MutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
synchronized (this.lock)
{
return this.getDelegate().collectKeysUnique(function);
}
}

@Override
public MutableMap<K, V> tap(Procedure<? super V> procedure)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,12 @@ public <R> MutableMap<K, R> collectValues(Function2<? super K, ? super V, ? exte
return this.getMutableMap().collectValues(function);
}

@Override
public <R> MutableMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return this.getMutableMap().collectKeysUnique(function);
}

@Override
public <K2, V2> MutableMap<K2, V2> collect(Function2<? super K, ? super V, Pair<K2, V2>> function)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ public <R> ImmutableOrderedMap<K, R> collectValues(Function2<? super K, ? super
return this.delegate.<R>collectValues(function).toImmutable();
}

@Override
public <R> ImmutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return this.delegate.<R>collectKeysUnique(function).toImmutable();
}

@Override
public ImmutableOrderedMap<K, V> select(Predicate2<? super K, ? super V> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ public <R> MutableOrderedMap<K, R> collectValues(Function2<? super K, ? super V,
OrderedMapAdapter.adapt(new LinkedHashMap<>(this.size())));
}

@Override
public <R> MutableOrderedMap<R, V> collectKeysUnique(Function2<? super K, ? super V, ? extends R> function)
{
return MapIterate.collectKeysUnique(
this,
function,
OrderedMapAdapter.adapt(new LinkedHashMap<>(this.size())));
}

@Override
public MutableOrderedMap<K, V> tap(Procedure<? super V> procedure)
{
Expand Down
Loading

0 comments on commit 0e24ab1

Please sign in to comment.