Skip to content

Commit

Permalink
Gh 261 iterable concat null pointer exception (#262)
Browse files Browse the repository at this point in the history
* gh-261 IterableConcat NullPointerException bug

* gh-261 IterableConcat NullPointerException bug

* Update IterableFlattenTest.java

* Amend tests, clarify Javadoc

* improve .gitignore

Co-authored-by: GCHQDev404 <[email protected]>
Co-authored-by: GCHQDeveloper314 <[email protected]>
  • Loading branch information
3 people authored May 5, 2022
1 parent 7d5af85 commit f3744df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017-2020 Crown Copyright
# Copyright 2017-2022 Crown Copyright

# 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,4 +29,7 @@ target/
.vscode
*/.classpath
__pycache__
*.pem
*.pem
.DS_Store
*/.DS_Store

Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;

import java.util.Objects;
import java.util.function.BinaryOperator;
import java.util.stream.StreamSupport;

import static java.util.Objects.nonNull;

/**
* A {@code IterableFlatten} is a {@link java.util.function.Function} that takes items from an {@link Iterable} and
* combines them into a single result based on a provided {@link BinaryOperator}
* combines them into a single result based on a provided {@link BinaryOperator}.
*
* Any {@code null} items are ignored.
*
* @param <I_ITEM> the type of object in the iterable
*/
Expand All @@ -54,6 +57,7 @@ public I_ITEM apply(final Iterable<I_ITEM> items) {
if (nonNull(items) && nonNull(operator)) {
return StreamSupport.stream(items.spliterator(), false)
.onClose(() -> CloseableUtil.close(items))
.filter(Objects::nonNull)
.reduce(operator)
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;

public class IterableFlattenTest extends FunctionTest<IterableFlatten> {

Expand Down Expand Up @@ -59,6 +60,31 @@ public void shouldFlattenIterableNumbers() {
assertThat(result).isEqualTo(15);
}

@Test
public void shouldFlattenIterableNumbersWithNull() {
// Given
final IterableFlatten<Number> function = new IterableFlatten<>(new Sum());
final List<Number> input = Lists.newArrayList(2, 4, 6, 8, null, 10);

// When
final Number result = function.apply(input);

// Then
assertThat(result).isEqualTo(30);
}
@Test
public void shouldFlattenIterableNumbersAllNull() {
// Given
final IterableFlatten<Number> function = new IterableFlatten<>(new Sum());
final List<Number> input = Lists.newArrayList(null, null, null, null);

// When
final Number result = function.apply(input);

//then
assertNull(result);
}

@Test
public void shouldFlattenIterableStrings() {
// Given
Expand All @@ -84,12 +110,12 @@ protected Iterable<IterableFlatten> getDifferentInstancesOrNull() {

@Override
protected Class[] getExpectedSignatureInputClasses() {
return new Class[]{ Iterable.class };
return new Class[]{Iterable.class};
}

@Override
protected Class[] getExpectedSignatureOutputClasses() {
return new Class[]{ Object.class };
return new Class[]{Object.class};
}

@Test
Expand Down

0 comments on commit f3744df

Please sign in to comment.