Skip to content

Commit

Permalink
gh-266: Cleanup and add more constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
t92549 committed May 6, 2022
1 parent f3af34c commit ee5ca25
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class ChainedIterator<T> implements Closeable, Iterator<T> {
private Iterator<? extends T> currentIterator = Collections.emptyIterator();

public ChainedIterator(final Iterator<? extends Iterable<? extends T>> iterablesIterator) {
if (null == iterablesIterator) {
throw new IllegalArgumentException("iterables are required");
}
this.iterablesIterator = iterablesIterator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package uk.gov.gchq.koryphe.iterable;

import com.google.common.collect.Lists;

import uk.gov.gchq.koryphe.util.CloseableUtil;

import java.io.Closeable;
Expand All @@ -34,10 +36,22 @@ public class FilteredIterable<T> implements Closeable, Iterable<T> {
private final Iterable<T> iterable;
private final List<Predicate> predicates;

public FilteredIterable(final Iterable<T> iterable, final Predicate... predicates) {
this(iterable, Lists.newArrayList(predicates));
}

public FilteredIterable(final Iterable<T> iterable, final List<Predicate> predicates) {
if (null == iterable) {
throw new IllegalArgumentException("iterable is required");
}
if (null == predicates) {
throw new IllegalArgumentException("List of predicates cannot be null");
}
for (final Predicate predicate : predicates) {
if (null == predicate) {
throw new IllegalArgumentException("Predicates list cannot contain a null predicate");
}
}

this.iterable = iterable;
this.predicates = predicates;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ public FilteredIterator(final Iterator<T> iterator, final List<Predicate> predic
if (null == iterator) {
throw new IllegalArgumentException("iterator is required");
}

if (null == predicates) {
throw new IllegalArgumentException("List of predicates cannot be null");
}
for (final Predicate predicate : predicates) {
if (null == predicate) {
throw new IllegalArgumentException("Predicates list cannot contain a null predicate");
}
}
this.iterator = iterator;
this.andPredicate = new And<>(predicates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package uk.gov.gchq.koryphe.iterable;

import com.google.common.collect.Lists;

import uk.gov.gchq.koryphe.util.CloseableUtil;

import java.io.Closeable;
Expand All @@ -35,10 +37,22 @@ public class MappedIterable<I_ITEM, O_ITEM> implements Closeable, Iterable<O_ITE
private final Iterable<I_ITEM> iterable;
private final List<Function> functions;

public MappedIterable(final Iterable<I_ITEM> iterable, final Function... functions) {
this(iterable, Lists.newArrayList(functions));
}

public MappedIterable(final Iterable<I_ITEM> iterable, final List<Function> functions) {
if (null == iterable) {
throw new IllegalArgumentException("iterable is required");
}
if (null == functions) {
throw new IllegalArgumentException("List of functions cannot be null");
}
for (final Function function : functions) {
if (null == function) {
throw new IllegalArgumentException("Functions list cannot contain a null function");
}
}

this.iterable = iterable;
this.functions = functions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public MappedIterator(final Iterator<I_ITEM> iterator, final List<Function> func
if (null == iterator) {
throw new IllegalArgumentException("iterator is required");
}
if (null == functions) {
throw new IllegalArgumentException("List of functions cannot be null");
}
for (final Function function : functions) {
if (null == function) {
throw new IllegalArgumentException("Functions list cannot contain a null function");
}
}

this.iterator = iterator;
this.functions = functions;
Expand Down
26 changes: 0 additions & 26 deletions core/src/main/java/uk/gov/gchq/koryphe/util/IterableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ private IterableUtil() {
* @return the lazily filtered iterable
*/
public static <T> Iterable<T> filter(final Iterable<T> iterable, final Predicate predicate) {
if (null == predicate) {
throw new IllegalArgumentException("Predicate cannot be null");
}
return filter(iterable, Collections.singletonList(predicate));
}

Expand All @@ -68,40 +65,17 @@ public static <T> Iterable<T> filter(final Iterable<T> iterable, final List<Pred
if (null == iterable) {
return null;
}

if (null == predicates) {
throw new IllegalArgumentException("List of predicates cannot be null");
}

for (final Predicate predicate : predicates) {
if (null == predicate) {
throw new IllegalArgumentException("Predicates list cannot contain a null predicate");
}
}
return new FilteredIterable<>(iterable, predicates);
}

public static <I_ITEM, O_ITEM> Iterable<O_ITEM> map(final Iterable<I_ITEM> iterable, final Function function) {
if (null == function) {
throw new IllegalArgumentException("Function cannot be null");
}
return map(iterable, Collections.singletonList(function));
}

public static <I_ITEM, O_ITEM> Iterable<O_ITEM> map(final Iterable<I_ITEM> iterable, final List<Function> functions) {
if (null == iterable) {
return null;
}

if (null == functions) {
throw new IllegalArgumentException("List of functions cannot be null");
}

for (final Function func : functions) {
if (null == func) {
throw new IllegalArgumentException("Functions list cannot contain a null function");
}
}
return new MappedIterable<>(iterable, functions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@
public class FilteredIterableTest {

@Test
public void shouldThrowIAXWhenArrayOfIterablesAreNull() {
public void shouldThrowIAXWhenArrayOfIterablesIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new FilteredIterable(null, Lists.newArrayList(new IsLessThan(4))));
}

@Test
public void shouldThrowIAXWhenListOfPredicatesIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new FilteredIterable(Lists.newArrayList(1, 2, 3, 4, 5), (List) null));
}

@Test
public void shouldThrowIAXWhenOnePredicateIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new FilteredIterable(Lists.newArrayList(1, 2, 3, 4, 5), Lists.newArrayList(new IsLessThan(4), (Predicate) null)));
}

@Test
public void shouldCorrectlyFilterSinglePredicate() {
// Given
final List<Integer> itr = Lists.newArrayList(1, 2, 3, 4, 5);
final List<Predicate> predicates = Lists.newArrayList(new IsLessThan(4));

// When
FilteredIterable<Integer> filteredIterable = new FilteredIterable<Integer>(itr, predicates);
FilteredIterable<Integer> filteredIterable = new FilteredIterable<Integer>(itr, new IsLessThan(4));

// Then
assertThat(filteredIterable).containsExactly(1, 2, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,27 @@
public class MappedIterableTest {

@Test
public void shouldThrowIAXWhenArrayOfIterablesAreNull() {
public void shouldThrowIAXWhenArrayOfIterablesIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new MappedIterable(null, Lists.newArrayList(new Increment(4))));
}

@Test
public void shouldThrowIAXWhenListOfFunctionsIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new MappedIterable(Lists.newArrayList(1, 2, 3, 4, 5), (List) null));
}

@Test
public void shouldThrowIAXWhenOneFunctionIsNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new MappedIterable(Lists.newArrayList(1, 2, 3, 4, 5), Lists.newArrayList(new Increment(4), (Function) null)));
}

@Test
public void shouldCorrectlyApplySingleFunction() {
// Given
final List<Integer> itr = Lists.newArrayList(1, 2, 3, 4, 5);
final List<Function> functions = Lists.newArrayList(new Increment(4));

// When
MappedIterable<Integer, Integer> mappedIterable = new MappedIterable<Integer, Integer>(itr, functions);
MappedIterable<Integer, Integer> mappedIterable = new MappedIterable<Integer, Integer>(itr, new Increment(4));

// Then
assertThat(mappedIterable).containsExactly(5, 6, 7, 8, 9);
Expand Down

0 comments on commit ee5ca25

Please sign in to comment.