Skip to content

Commit

Permalink
Merge pull request #154 from donraab/master
Browse files Browse the repository at this point in the history
Update custom collections with new functionality
  • Loading branch information
donraab authored Nov 8, 2023
2 parents 2d20c1f + 4984933 commit 0d9ee52
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,7 +44,11 @@ default ImmutableCollection<T> peek(Consumer<? super T> consumer)
default <K> ImmutableBag<K> countBy(Function<? super T, ? extends K> function)
{
var counts = MutableBag.<K>empty();
this.forEach(each -> counts.add(function.apply(each)));
return counts.toImmutable();
return this.map(function, counts).toImmutable();
}

default <V> ImmutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)
{
return this.flatMap(function, MutableBag.empty()).toImmutable();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,44 +79,27 @@ default MutableBag<T> peek(Consumer<? super T> consumer)
default MutableBag<T> filter(Predicate<? super T> predicate)
{
var mutableBag = MutableBag.<T>empty();
for (T each : this)
{
if (predicate.test(each))
{
mutableBag.add(each);
}
}
return mutableBag;
return this.filter(predicate, mutableBag);
}

@Override
default MutableBag<T> filterNot(Predicate<? super T> predicate)
{
var mutableBag = MutableBag.<T>empty();
for (T each : this)
{
if (!predicate.test(each))
{
mutableBag.add(each);
}
}
return mutableBag;
return this.filter(predicate.negate());
}

@Override
default <V> MutableBag<V> map(Function<? super T, ? extends V> function)
{
var mutableBag = MutableBag.<V>empty();
this.forEach(each -> mutableBag.add(function.apply(each)));
return mutableBag;
return this.map(function, mutableBag);
}

@Override
default <V> MutableBag<V> flatMap(Function<? super T, ? extends Iterable<V>> function)
{
var mutableBag = MutableBag.<V>empty();
this.forEach(each -> mutableBag.addAllIterable(function.apply(each)));
return mutableBag;
return this.flatMap(function, mutableBag);
}

default <K> ArrayListMultimap<K, T> groupBy(Function<? super T, ? extends K> function)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,15 +29,12 @@ default boolean addAllIterable(Iterable<T> iterable)
{
return this.addAll(collection);
}
else
boolean result = false;
for (T each : iterable)
{
boolean result = false;
for (T each : iterable)
{
result |= this.add(each);
}
return result;
result |= this.add(each);
}
return result;
}

@Override
Expand Down Expand Up @@ -71,4 +68,9 @@ default <K> MutableBag<K> countBy(Function<? super T, ? extends K> function)
this.forEach(each -> counts.add(function.apply(each)));
return counts;
}

default <V> MutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)
{
return this.flatMap(function, MutableBag.empty());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,44 +81,27 @@ default MutableList<T> asUnmodifiable()
default MutableList<T> filter(Predicate<? super T> predicate)
{
var mutableList = MutableList.<T>empty();
for (T each : this)
{
if (predicate.test(each))
{
mutableList.add(each);
}
}
return mutableList;
return this.filter(predicate, mutableList);
}

@Override
default MutableList<T> filterNot(Predicate<? super T> predicate)
{
var mutableList = MutableList.<T>empty();
for (T each : this)
{
if (!predicate.test(each))
{
mutableList.add(each);
}
}
return mutableList;
return this.filter(predicate.negate());
}

@Override
default <V> MutableList<V> map(Function<? super T, ? extends V> function)
{
var mutableList = MutableList.<V>empty();
this.forEach(each -> mutableList.add(function.apply(each)));
return mutableList;
return this.map(function, mutableList);
}

@Override
default <V> MutableList<V> flatMap(Function<? super T, ? extends Iterable<V>> function)
{
var mutableList = MutableList.<V>empty();
this.forEach(each -> mutableList.addAllIterable(function.apply(each)));
return mutableList;
return this.flatMap(function, mutableList);
}

@Override
Expand All @@ -135,6 +118,13 @@ default <K> MutableListMultimap<K, T> groupBy(Function<? super T, ? extends K> f
return multimap;
}

default <K> MutableListMultimap<K, T> groupByEach(Function<? super T, ? extends Iterable<K>> function)
{
var multimap = MutableListMultimap.<K, T>empty();
this.forEach(each -> function.apply(each).forEach(key -> multimap.put(key, each)));
return multimap;
}

default <K> MutableListMultimap<K, T> groupByUnmodifiable(Function<? super T, ? extends K> function)
{
var multimap = this.<K>groupBy(function);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,44 +78,27 @@ default java.util.Set<T> asUnmodifiable()
default MutableSet<T> filter(Predicate<? super T> predicate)
{
var mutableSet = MutableSet.<T>empty();
for (T each : this)
{
if (predicate.test(each))
{
mutableSet.add(each);
}
}
return mutableSet;
return this.filter(predicate, mutableSet);
}

@Override
default MutableSet<T> filterNot(Predicate<? super T> predicate)
{
var mutableSet = MutableSet.<T>empty();
for (T each : this)
{
if (!predicate.test(each))
{
mutableSet.add(each);
}
}
return mutableSet;
return this.filter(predicate.negate());
}

@Override
default <V> MutableSet<V> map(Function<? super T, ? extends V> function)
{
var mutableSet = MutableSet.<V>empty();
this.forEach(each -> mutableSet.add(function.apply(each)));
return mutableSet;
return this.map(function, mutableSet);
}

@Override
default <V> MutableSet<V> flatMap(Function<? super T, ? extends Iterable<V>> function)
{
var mutableSet = MutableSet.<V>empty();
this.forEach(each -> mutableSet.addAllIterable(function.apply(each)));
return mutableSet;
return this.flatMap(function, mutableSet);
}

default <K, V> MutableSetMultimap<K, T> groupBy(Function<? super T, ? extends K> function)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Bank of New York Mellon.
* Copyright 2023 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,19 +32,53 @@ public interface RichIterable<T> extends Iterable<T>

boolean isEmpty();

default boolean notEmpty()
{
return !this.isEmpty();
}

default Stream<T> stream()
{
throw new UnsupportedOperationException();
}

RichIterable<T> filter(Predicate<? super T> predicate);

default <R extends Collection<T>> R filter(Predicate<? super T> predicate, R target)
{
this.forEach(each ->
{
if (predicate.test(each))
{
target.add(each);
}
});
return target;
}

RichIterable<T> filterNot(Predicate<? super T> predicate);

default <R extends Collection<T>> R filterNot(Predicate<? super T> predicate, R target)
{
return this.filter(predicate.negate(), target);
}

<V> RichIterable<V> map(Function<? super T, ? extends V> function);

default <V, R extends Collection<V>> R map(Function<? super T, ? extends V> function, R target)
{
this.forEach(each -> target.add(function.apply(each)));
return target;
}

<V> RichIterable<V> flatMap(Function<? super T, ? extends Iterable<V>> function);

default <V, R extends MutableCollection<V>> R flatMap(Function<? super T, ? extends Iterable<V>> function, R target)
{
this.forEach(each -> target.addAllIterable(function.apply(each)));
return target;
}

default RichIterable<T> peek(Consumer<? super T> consumer)
{
this.forEach(consumer);
Expand Down Expand Up @@ -80,6 +114,11 @@ default boolean anyMatch(Predicate<? super T> predicate)
return false;
}

default <V> boolean containsBy(Function<? super T, ? extends V> function, V value)
{
return this.anyMatch(each -> value.equals(function.apply(each)));
}

default boolean allMatch(Predicate<? super T> predicate)
{
for (T each : this)
Expand Down Expand Up @@ -147,10 +186,19 @@ default MutableSet<T> toSet()
return this.toCollection(MutableSet::empty, Collection::add);
}

default MutableBag<T> toBag()
{
return this.toCollection(MutableBag::empty, Collection::add);
}

default <R extends Collection<T>> R toCollection(Supplier<R> supplier, BiConsumer<R, T> addMethod)
{
R collection = supplier.get();
this.forEach(each -> addMethod.accept(collection, each));
return collection;
}

<K> Bag<K> countBy(Function<? super T, ? extends K> function);

<V> Bag<V> countByEach(Function<? super T, ? extends Iterable<V>> function);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -412,4 +413,43 @@ public void addAllIterable()
list2.addAllIterable(expected::iterator);
Assertions.assertEquals(expected, list2);
}

@Test
public void countByEach()
{
MutableList<MutableList<Integer>> list = MutableList.of(MutableList.of(1, 2), MutableList.of(1, 2, 3));
MutableBag<Integer> counts = list.countByEach(each -> each);
Assertions.assertEquals(2, counts.getOccurrences(1));
Assertions.assertEquals(2, counts.getOccurrences(2));
Assertions.assertEquals(1, counts.getOccurrences(3));
}

@Test
public void groupByEach()
{
MutableList<Integer> list = MutableList.of(1, 2, 3);
MutableListMultimap<Integer, Integer> multimap = list.groupByEach(each -> IntStream.range(0, each).boxed().toList());
Assertions.assertEquals(List.of(1, 2, 3), multimap.get(0));
Assertions.assertEquals(List.of(2, 3), multimap.get(1));
Assertions.assertEquals(List.of(3), multimap.get(2));
}

@Test
public void toBag()
{
MutableList<Integer> list = MutableList.of(1, 2, 2, 3, 3, 3);
MutableBag<Integer> bag = list.toBag();

Assertions.assertEquals(MutableBag.of(1, 2, 2, 3, 3, 3), bag);
}

@Test
public void containsBy()
{
MutableList<Integer> hasEvens = MutableList.of(1, 2, 3);
MutableList<Integer> onlyOdds = MutableList.of(1, 3);

Assertions.assertTrue(hasEvens.containsBy(each -> each % 2, 0));
Assertions.assertFalse(onlyOdds.containsBy(each -> each % 2, 0));
}
}

0 comments on commit 0d9ee52

Please sign in to comment.