Skip to content

Commit

Permalink
Add toImmutableSortedMap and toImmutableSortedMapBy to Collectors2
Browse files Browse the repository at this point in the history
Signed-off-by: Sirisha Pratha <sirisha.pratha@bnymellon.com>
  • Loading branch information
prathasirisha committed Sep 3, 2021
1 parent 7ec0d87 commit 9f8a4d3
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.eclipse.collections.api.map.MutableMapIterable;
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
import org.eclipse.collections.api.map.primitive.MutableObjectLongMap;
import org.eclipse.collections.api.map.sorted.ImmutableSortedMap;
import org.eclipse.collections.api.map.sorted.MutableSortedMap;
import org.eclipse.collections.api.multimap.ImmutableMultimap;
import org.eclipse.collections.api.multimap.MutableMultimap;
Expand Down Expand Up @@ -736,7 +737,7 @@ private Collectors2()
* <p>
* Equivalent to using @{@link RichIterable#toSortedMap(Comparator, Function, Function)} (Comparator)}}
* </p>
* {@code MutableSortedMap<Integer, String> map = Interval.oneTo(5).toMap(Comparators.naturalOrder(), Functions.identity(), Object::toString);}
* {@code MutableSortedMap<Integer, String> map = Interval.oneTo(5).toSortedMap(Comparators.naturalOrder(), Functions.identity(), Object::toString);}
*
* @since 11.0
*/
Expand Down Expand Up @@ -764,9 +765,9 @@ private Collectors2()
* {@code MutableSortedMap<Integer, String> map2 =
* Interval.oneTo(5).reduceInPlace(Collectors2.toSortedMap(Object::toString, Functions.identity(), Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toSortedMapBy(Function, Function, Function)} (Comparator)}}
* Equivalent to using @{@link RichIterable#toSortedMapBy(Function, Function, Function)}
* </p>
* {@code MutableSortedMap<Integer, String> map = Interval.oneTo(5).toMap(Object::toString, Functions.identity(), Object::toString);}
* {@code MutableSortedMap<Integer, String> map = Interval.oneTo(5).toSortedMapBy(Object::toString, Functions.identity(), Object::toString);}
*
* @since 11.0
*/
Expand Down Expand Up @@ -806,6 +807,89 @@ private Collectors2()
EMPTY_CHARACTERISTICS);
}

/**
* <p>Returns the elements as a ImmutableSortedMap that has been sorted after applying the keyFunction and valueFunction to each element.</p>
* <p>Examples:</p>
* {@code ImmutableSortedMap<Integer, String> map1 =
* Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedMap(Functions.identity(), Object::toString));}<br>
* {@code ImmutableSortedMap<Integer, String> map2 =
* Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedMap(Functions.identity(), Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toSortedMap(Function, Function)}} followed by: @{@link MutableSortedMap#toImmutable()}.
* </p>
* {@code ImmutableSortedMap<Integer, String> map = Interval.oneTo(5).toSortedMap(Functions.identity(), Object::toString).toImmutable();}
*
* @since 11.0
*/
public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction)
{
return Collector.<T, MutableSortedMap<K, V>, ImmutableSortedMap<K, V>>of(
SortedMaps.mutable::empty,
(map, each) -> map.put(keyFunction.valueOf(each), valueFunction.valueOf(each)),
(r1, r2) ->
{
r1.putAll(r2);
return r1;
},
MutableSortedMap::toImmutable,
EMPTY_CHARACTERISTICS);
}

/**
* <p>Returns the elements as a ImmutableSortedMap that has been sorted using the specified comparator after applying the keyFunction and valueFunction to each element.</p>
* <p>Examples:</p>
* {@code ImmutableSortedMap<Integer, String> map1 =
* Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedMap(Comparators.naturalOrder(), Functions.identity(), Object::toString));}<br>
* {@code ImmutableSortedMap<Integer, String> map2 =
* Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedMap(Comparators.naturalOrder(), Functions.identity(), Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toSortedMap(Comparator, Function, Function)}} followed by: @{@link MutableSortedMap#toImmutable()}.
* </p>
* {@code ImmutableSortedMap<Integer, String> map = Interval.oneTo(5).toSortedMap(Comparators.naturalOrder(), Functions.identity(), Object::toString).toImmutable();}
*
* @since 11.0
*/
public static <T, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMap(
Comparator<? super K> comparator,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction)
{
return Collector.<T, MutableSortedMap<K, V>, ImmutableSortedMap<K, V>>of(
() -> SortedMaps.mutable.with(comparator),
(map, each) -> map.put(keyFunction.valueOf(each), valueFunction.valueOf(each)),
(r1, r2) ->
{
r1.putAll(r2);
return r1;
},
MutableSortedMap::toImmutable,
EMPTY_CHARACTERISTICS);
}

/**
* <p>Returns the elements as a ImmutableSortedMap that has been sorted using the specified comparator.</p>
* <p>Examples:</p>
* {@code ImmutableSortedMap<Integer, String> map1 =
* Interval.oneTo(5).stream().collect(Collectors2.toImmutableSortedMap(Object::toString, Functions.identity(), Object::toString));}<br>
* {@code ImmutableSortedMap<Integer, String> map2 =
* Interval.oneTo(5).reduceInPlace(Collectors2.toImmutableSortedMap(Object::toString, Functions.identity(), Object::toString));}
* <p>
* Equivalent to using @{@link RichIterable#toSortedMapBy(Function, Function, Function)} followed by: @{@link MutableSortedMap#toImmutable()}.
* </p>
* {@code ImmutableSortedMap<Integer, String> map = Interval.oneTo(5).toSortedMap(Object::toString, Functions.identity(), Object::toString).toImmutable();}
*
* @since 11.0
*/
public static <T, KK extends Comparable<? super KK>, K, V> Collector<T, ?, ImmutableSortedMap<K, V>> toImmutableSortedMapBy(
Function<? super K, KK> sortBy,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction)
{
return Collectors2.toImmutableSortedMap(Comparators.byFunction(sortBy), keyFunction, valueFunction);
}

/**
* Returns the counts of all of the values returned by applying the specified function to each
* item of the Stream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,72 @@ public void toSortedMapByParallel()
this.bigData.parallelStream().collect(Collectors2.toSortedMapBy(Object::toString, Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMap()
{
Assert.assertEquals(
SMALL_INTERVAL.toSortedMap(Object::toString, i -> i).toImmutable(),
this.smallData.stream().collect(Collectors2.toImmutableSortedMap(Object::toString, i -> i)));
Assert.assertEquals(
SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMap(Object::toString, i -> i)),
this.smallData.stream().collect(Collectors2.toImmutableSortedMap(Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMapParallel()
{
Assert.assertEquals(
LARGE_INTERVAL.toSortedMap(Object::toString, i -> i).toImmutable(),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMap(Object::toString, i -> i)));
Assert.assertEquals(
LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMap(Object::toString, i -> i)),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMap(Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMapWithComparator()
{
Assert.assertEquals(
SMALL_INTERVAL.toSortedMap(Comparator.reverseOrder(), Object::toString, i -> i).toImmutable(),
this.smallData.stream().collect(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)));
Assert.assertEquals(
SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)),
this.smallData.stream().collect(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMapParallelWithComparator()
{
Assert.assertEquals(
LARGE_INTERVAL.toSortedMap(Comparator.reverseOrder(), Object::toString, i -> i).toImmutable(),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)));
Assert.assertEquals(
LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMap(Comparator.reverseOrder(), Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMapBy()
{
Assert.assertEquals(
SMALL_INTERVAL.toSortedMapBy(Object::toString, Object::toString, i -> i).toImmutable(),
this.smallData.stream().collect(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)));
Assert.assertEquals(
SMALL_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)),
this.smallData.stream().collect(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)));
}

@Test
public void toImmutableSortedMapByParallel()
{
Assert.assertEquals(
LARGE_INTERVAL.toSortedMapBy(Object::toString, Object::toString, i -> i).toImmutable(),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)));
Assert.assertEquals(
LARGE_INTERVAL.reduceInPlace(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)),
this.bigData.parallelStream().collect(Collectors2.toImmutableSortedMapBy(Object::toString, Object::toString, i -> i)));
}

@Test
public void toImmutableSortedListWithComparator()
{
Expand Down

0 comments on commit 9f8a4d3

Please sign in to comment.