Skip to content

Commit

Permalink
Update custom collections with functionality required to complete Pet…
Browse files Browse the repository at this point in the history
… Kata Exercises 1-3.

Signed-off-by: Donald Raab <[email protected]>
  • Loading branch information
donraab committed Oct 31, 2023
1 parent 2d20c1f commit 1080e45
Show file tree
Hide file tree
Showing 6 changed files with 88 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);
}

0 comments on commit 1080e45

Please sign in to comment.