Skip to content

Commit

Permalink
Add toImmutableBiMap to RichIterable.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed May 12, 2021
1 parent 4372b31 commit 9ea7f6c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.collections.api.bag.MutableBagIterable;
import org.eclipse.collections.api.bag.sorted.ImmutableSortedBag;
import org.eclipse.collections.api.bag.sorted.MutableSortedBag;
import org.eclipse.collections.api.bimap.ImmutableBiMap;
import org.eclipse.collections.api.bimap.MutableBiMap;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
Expand Down Expand Up @@ -1888,6 +1889,18 @@ default <NK, NV> ImmutableMap<NK, NV> toImmutableMap(
return this.<NK, NV>toMap(keyFunction, valueFunction).toImmutable();
}

/**
* Converts the collection to an immutable BiMap implementation using the specified key and value functions.
*
* @since 11.0
*/
default <NK, NV> ImmutableBiMap<NK, NV> toImmutableBiMap(
Function<? super T, ? extends NK> keyFunction,
Function<? super T, ? extends NV> valueFunction)
{
return this.<NK, NV>toBiMap(keyFunction, valueFunction).toImmutable();
}

/**
* Returns a lazy (deferred) iterable, most likely implemented by calling LazyIterate.adapt(this).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,26 @@ public void toBiMap()
() -> integers.toBiMap(i -> "Constant Key", i -> "Constant Value"));
}

@Test
public void toImmutableBiMap()
{
RichIterable<Integer> integers = this.newWith(1, 2, 3);

Assert.assertEquals(
Maps.mutable.with("1", "1", "2", "2", "3", "3"),
integers.toImmutableBiMap(Object::toString, Object::toString));

Assert.assertThrows(
IllegalArgumentException.class,
() -> integers.toImmutableBiMap(i -> "Constant Key", Objects::toString));
Assert.assertThrows(
IllegalArgumentException.class,
() -> integers.toImmutableBiMap(Object::toString, i -> "Constant Value"));
Assert.assertThrows(
IllegalArgumentException.class,
() -> integers.toImmutableBiMap(i -> "Constant Key", i -> "Constant Value"));
}

@Test
public void testToString()
{
Expand Down

0 comments on commit 9ea7f6c

Please sign in to comment.