diff --git a/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvLinesToMaps.java b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvLinesToMaps.java index 4fdeab532..4d46a6b18 100644 --- a/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvLinesToMaps.java +++ b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvLinesToMaps.java @@ -27,7 +27,6 @@ import uk.gov.gchq.koryphe.Since; import uk.gov.gchq.koryphe.Summary; import uk.gov.gchq.koryphe.function.KorypheFunction; -import uk.gov.gchq.koryphe.iterable.CloseableIterable; import uk.gov.gchq.koryphe.util.IterableUtil; import java.io.IOException; @@ -45,10 +44,10 @@ @Since("1.8.0") @Summary("Parses CSV lines into Maps") -@JsonPropertyOrder(value = {"header", "firstRow", "delimiter", "quoted", "quoteChar"}, - alphabetic = true) +@JsonPropertyOrder(value = { "header", "firstRow", "delimiter", "quoted", "quoteChar" }, alphabetic = true) @JsonInclude(JsonInclude.Include.NON_DEFAULT) -public class CsvLinesToMaps extends KorypheFunction, Iterable>> implements Serializable { +public class CsvLinesToMaps extends KorypheFunction, Iterable>> + implements Serializable { private static final long serialVersionUID = -4225921410795200955L; private List header = new ArrayList<>(); private int firstRow = 0; @@ -62,7 +61,7 @@ public Iterable> apply(final Iterable csvStrings) { return null; } - final CloseableIterable csvRecords = IterableUtil.limit(csvStrings, firstRow, null, false); + final Iterable csvRecords = IterableUtil.limit(csvStrings, firstRow, null, false); return IterableUtil.map(csvRecords, (item) -> createMap((String) item)); } @@ -81,8 +80,8 @@ private Map extractMap(final CSVRecord csvRecord) { private CSVRecord parseCsv(final String csv) { final CSVRecord csvRecord; - try { - csvRecord = new CSVParser(new StringReader(csv), getCsvFormat()).iterator().next(); + try (final CSVParser csvParser = new CSVParser(new StringReader(csv), getCsvFormat())) { + csvRecord = csvParser.iterator().next(); } catch (final IOException e) { throw new RuntimeException(e); } @@ -91,8 +90,7 @@ private CSVRecord parseCsv(final String csv) { throw new IllegalArgumentException( "CSV has " + csvRecord.size() + " columns, but there are " + header.size() - + " provided column names" - ); + + " provided column names"); } return csvRecord; } @@ -192,7 +190,7 @@ public boolean equals(final Object o) { return false; // Does class checking } - CsvLinesToMaps that = (CsvLinesToMaps) o; + final CsvLinesToMaps that = (CsvLinesToMaps) o; return new EqualsBuilder() .append(header, that.header) .append(quoted, that.quoted) diff --git a/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvToMaps.java b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvToMaps.java index 1af340104..047f0aa93 100644 --- a/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvToMaps.java +++ b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/CsvToMaps.java @@ -27,7 +27,7 @@ import uk.gov.gchq.koryphe.Since; import uk.gov.gchq.koryphe.Summary; import uk.gov.gchq.koryphe.function.KorypheFunction; -import uk.gov.gchq.koryphe.iterable.CloseableIterable; +import uk.gov.gchq.koryphe.util.CloseableUtil; import uk.gov.gchq.koryphe.util.IterableUtil; import java.io.IOException; @@ -45,8 +45,7 @@ @Since("1.8.0") @Summary("Parses a CSV into Maps") -@JsonPropertyOrder(value = {"header", "firstRow", "delimiter", "quoted", "quoteChar"}, - alphabetic = true) +@JsonPropertyOrder(value = { "header", "firstRow", "delimiter", "quoted", "quoteChar" }, alphabetic = true) @JsonInclude(JsonInclude.Include.NON_DEFAULT) public class CsvToMaps extends KorypheFunction>> implements Serializable { private static final long serialVersionUID = 891938487168606844L; @@ -62,12 +61,14 @@ public Iterable> apply(final String csv) { return null; } - try { - final CSVParser csvParser = new CSVParser(new StringReader(csv), getCsvFormat()); - final CloseableIterable csvRecords = IterableUtil.limit(csvParser.getRecords(), firstRow, null, false); - return IterableUtil.map(csvRecords, item -> extractMap((CSVRecord) item)); + Iterable csvRecords = null; + try (final CSVParser csvParser = new CSVParser(new StringReader(csv), getCsvFormat())) { + csvRecords = IterableUtil.limit(csvParser.getRecords(), firstRow, null, false); + return IterableUtil.map(csvRecords, (item) -> extractMap((CSVRecord) item)); } catch (final IOException e) { throw new RuntimeException("Unable to parse csv", e); + } finally { + CloseableUtil.close(csvRecords); } } @@ -163,6 +164,7 @@ public CsvToMaps quoteChar(final char quoteChar) { this.quoteChar = quoteChar; return this; } + @Override public boolean equals(final Object o) { if (this == o) { @@ -173,7 +175,7 @@ public boolean equals(final Object o) { return false; // Does class checking } - CsvToMaps that = (CsvToMaps) o; + final CsvToMaps that = (CsvToMaps) o; return new EqualsBuilder() .append(header, that.header) .append(quoted, that.quoted) @@ -194,5 +196,4 @@ public int hashCode() { .append(delimiter) .toHashCode(); } - } diff --git a/core/src/main/java/uk/gov/gchq/koryphe/impl/function/ToDouble.java b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/ToDouble.java new file mode 100644 index 000000000..0ab1821b7 --- /dev/null +++ b/core/src/main/java/uk/gov/gchq/koryphe/impl/function/ToDouble.java @@ -0,0 +1,49 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.koryphe.impl.function; + +import uk.gov.gchq.koryphe.Since; +import uk.gov.gchq.koryphe.Summary; +import uk.gov.gchq.koryphe.function.KorypheFunction; + +/** + * A ToDouble is a {@link java.util.function.Function} that takes + * an Object and casts it to a Double. + *

+ * The resulting object is what is returned from the method. + */ +@Since("2.0.0") +@Summary("Casts input Object to a Double") +public class ToDouble extends KorypheFunction { + + @Override + public Double apply(final Object value) { + if (null == value) { + return null; + } + + if (value instanceof Number) { + return ((Number) value).doubleValue(); + } + + if (value instanceof String) { + return Double.valueOf(((String) value)); + } + + throw new IllegalArgumentException("Could not convert value to Double: " + value); + } +} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterable.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterable.java deleted file mode 100644 index 4bac40ad9..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterable.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import java.io.Closeable; - -/** - * A {@code CloseableIterable} is an {@link Iterable} which must provide an implementation - * of the {@link Closeable#close()} method. - * - * @param the type of items in the iterable. - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. - */ -@Deprecated -public interface CloseableIterable extends Iterable, Closeable { - @Override - void close(); - - @Override - CloseableIterator iterator(); -} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterator.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterator.java deleted file mode 100644 index cd7346d0f..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/CloseableIterator.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import java.io.Closeable; -import java.util.Iterator; - -/** - * A {@code CloseableIterator} is an {@link Iterator} which must provide an implementation - * of the {@link Closeable#close()} method. - * - * @param the type of items in the iterator. - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. - */ -@Deprecated() -public interface CloseableIterator extends Iterator, Closeable { - @Override - void close(); -} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/EmptyCloseableIterator.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/EmptyCloseableIterator.java deleted file mode 100644 index 72b8d76c6..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/EmptyCloseableIterator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2019-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - - -import java.util.NoSuchElementException; - -/** - * @param the type of items in the iterator. - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. - */ -@Deprecated -public class EmptyCloseableIterator implements CloseableIterator { - @Override - public void close() { - // nothing to close - } - - @Override - public boolean hasNext() { - return false; - } - - @Override - public T next() { - throw new NoSuchElementException(); - } -} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterable.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterable.java deleted file mode 100644 index 7a5ae44cf..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterable.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2019-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import com.fasterxml.jackson.annotation.JsonIgnore; - -import uk.gov.gchq.koryphe.util.CloseableUtil; - -import java.util.function.Supplier; -import java.util.stream.Stream; - -/** - * @param the type of items in the iterator. - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. - */ -@Deprecated -public class StreamIterable implements CloseableIterable { - private final Supplier> streamSupplier; - - public StreamIterable(final Supplier> streamSupplier) { - this.streamSupplier = streamSupplier; - } - - @Override - public void close() { - CloseableUtil.close(streamSupplier); - } - - @Override - public CloseableIterator iterator() { - return new StreamIterator<>(streamSupplier.get()); - } - - @JsonIgnore - public Stream getStream() { - return streamSupplier.get(); - } -} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterator.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterator.java deleted file mode 100644 index 2d35e0add..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/StreamIterator.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2019-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import uk.gov.gchq.koryphe.util.CloseableUtil; - -import java.util.Iterator; -import java.util.stream.Stream; - -/** - * @param the type of items in the iterator. - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. - */ -@Deprecated -public class StreamIterator implements CloseableIterator { - private final Stream stream; - private final Iterator iterator; - - public StreamIterator() { - this(null); - } - - public StreamIterator(final Stream stream) { - if (null == stream) { - this.stream = Stream.empty(); - this.iterator = new EmptyCloseableIterator<>(); - } else { - this.stream = stream; - this.iterator = stream.iterator(); - } - } - - @Override - public void close() { - CloseableUtil.close(iterator); - CloseableUtil.close(stream); - } - - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public T next() { - return iterator.next(); - } - - @Override - public void remove() { - iterator.remove(); - } - - public Stream getStream() { - return stream; - } -} diff --git a/core/src/main/java/uk/gov/gchq/koryphe/iterable/package-info.java b/core/src/main/java/uk/gov/gchq/koryphe/iterable/package-info.java deleted file mode 100644 index 876eb0845..000000000 --- a/core/src/main/java/uk/gov/gchq/koryphe/iterable/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2017-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Gaffer-specific {@link java.lang.Iterable}s and {@link java.util.Iterator}s used when retrieving - * and manipulating data from stores. - */ -package uk.gov.gchq.koryphe.iterable; diff --git a/core/src/main/java/uk/gov/gchq/koryphe/util/IterableUtil.java b/core/src/main/java/uk/gov/gchq/koryphe/util/IterableUtil.java index 45d1f7b60..f6d4347ca 100644 --- a/core/src/main/java/uk/gov/gchq/koryphe/util/IterableUtil.java +++ b/core/src/main/java/uk/gov/gchq/koryphe/util/IterableUtil.java @@ -18,9 +18,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import uk.gov.gchq.koryphe.impl.predicate.And; -import uk.gov.gchq.koryphe.iterable.CloseableIterable; -import uk.gov.gchq.koryphe.iterable.CloseableIterator; +import java.io.Closeable; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -51,7 +50,7 @@ private IterableUtil() { * @param the type of the items in the iterable * @return the lazily filtered iterable */ - public static CloseableIterable filter(final Iterable iterable, final Predicate predicate) { + public static Iterable filter(final Iterable iterable, final Predicate predicate) { if (null == predicate) { throw new IllegalArgumentException("Predicate cannot be null"); } @@ -67,7 +66,7 @@ public static CloseableIterable filter(final Iterable iterable, final * @param the type of the items in the iterable * @return the lazily filtered iterable */ - public static CloseableIterable filter(final Iterable iterable, final List predicates) { + public static Iterable filter(final Iterable iterable, final List predicates) { if (null == iterable) { return null; } @@ -84,14 +83,14 @@ public static CloseableIterable filter(final Iterable iterable, final return new FilteredIterable<>(iterable, predicates); } - public static CloseableIterable map(final Iterable iterable, final Function function) { + public static Iterable map(final Iterable iterable, final Function function) { if (null == function) { throw new IllegalArgumentException("Function cannot be null"); } return map(iterable, Collections.singletonList(function)); } - public static CloseableIterable map(final Iterable iterable, final List functions) { + public static Iterable map(final Iterable iterable, final List functions) { if (null == iterable) { return null; } @@ -108,21 +107,19 @@ public static CloseableIterable map(final Iterable(iterable, functions); } - public static CloseableIterable concat(final Iterable> iterables) { + public static Iterable concat(final Iterable> iterables) { return new ChainedIterable<>(iterables); } - public static CloseableIterable limit(final Iterable iterable, final int start, final Integer end, final boolean truncate) { + public static Iterable limit(final Iterable iterable, final int start, final Integer end, final boolean truncate) { return new LimitedIterable<>(iterable, start, end, truncate); } /** * @param input type of items in the input iterator * @param output type of items in the output iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class MappedIterable implements CloseableIterable { + private static class MappedIterable implements Closeable, Iterable { private final Iterable iterable; private final List functions; @@ -132,7 +129,7 @@ private static class MappedIterable implements CloseableIterable } @Override - public CloseableIterator iterator() { + public Iterator iterator() { return new MappedIterator<>(iterable.iterator(), functions); } @@ -144,10 +141,8 @@ public void close() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class MappedIterator implements CloseableIterator { + private static class MappedIterator implements Closeable, Iterator { private final Iterator iterator; private final List functions; @@ -182,10 +177,8 @@ public void close() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class FilteredIterable implements CloseableIterable { + private static class FilteredIterable implements Closeable, Iterable { private final Iterable iterable; private final List predicates; @@ -195,7 +188,7 @@ private static class FilteredIterable implements CloseableIterable { } @Override - public CloseableIterator iterator() { + public Iterator iterator() { return new FilteredIterator<>(iterable.iterator(), predicates); } @@ -207,10 +200,8 @@ public void close() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class FilteredIterator implements CloseableIterator { + private static class FilteredIterator implements Closeable, Iterator { private final Iterator iterator; private final And andPredicate; @@ -271,10 +262,8 @@ public void close() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class ChainedIterable implements CloseableIterable { + private static class ChainedIterable implements Closeable, Iterable { private final Iterable> iterables; ChainedIterable(final Iterable> iterables) { @@ -285,7 +274,7 @@ private static class ChainedIterable implements CloseableIterable { } @Override - public CloseableIterator iterator() { + public Iterator iterator() { return new ChainedIterator<>(iterables.iterator()); } @@ -299,10 +288,8 @@ public void close() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static class ChainedIterator implements CloseableIterator { + private static class ChainedIterator implements Closeable, Iterator { private final Iterator> iterablesIterator; private Iterator currentIterator = Collections.emptyIterator(); @@ -349,10 +336,8 @@ private Iterator getIterator() { /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static final class LimitedIterable implements CloseableIterable { + private static final class LimitedIterable implements Closeable, Iterable { private final Iterable iterable; private final int start; private final Integer end; @@ -386,17 +371,15 @@ public void close() { } @Override - public CloseableIterator iterator() { + public Iterator iterator() { return new LimitedIterator<>(iterable.iterator(), start, end, truncate); } } /** * @param the type of items in the iterator - * @deprecated Closable will be removed, it is used with scaling Big Data and does not belong in Koryphe. */ - @Deprecated - private static final class LimitedIterator implements CloseableIterator { + private static final class LimitedIterator implements Closeable, Iterator { private final Iterator iterator; private final Integer end; private int index = 0; diff --git a/core/src/test/java/uk/gov/gchq/koryphe/impl/function/ToDoubleTest.java b/core/src/test/java/uk/gov/gchq/koryphe/impl/function/ToDoubleTest.java new file mode 100644 index 000000000..7b1310d39 --- /dev/null +++ b/core/src/test/java/uk/gov/gchq/koryphe/impl/function/ToDoubleTest.java @@ -0,0 +1,128 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.koryphe.impl.function; + +import org.junit.jupiter.api.Test; +import uk.gov.gchq.koryphe.function.FunctionTest; +import uk.gov.gchq.koryphe.util.JsonSerialiser; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ToDoubleTest extends FunctionTest { + + @Test + public void shouldThrowException() { + // Given + final ToDouble function = new ToDouble(); + + // When + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + function.apply(true); + }); + + String expectedMessage = "Could not convert value to Double: "; + String actualMessage = exception.getMessage(); + + // Then + assertTrue(actualMessage.contains(expectedMessage)); + } + + @Test + public void shouldConvertStringToDouble() { + // Given + final ToDouble function = new ToDouble(); + + // When + Object output = function.apply("5.2"); + + // Then + assertEquals(5.2, output); + assertEquals(Double.class, output.getClass()); + } + + @Test + public void shouldConvertNumberToDouble() { + // Given + final ToDouble function = new ToDouble(); + + // When + Object output = function.apply(5); + + // Then + assertEquals(5.0, output); + assertEquals(Double.class, output.getClass()); + } + + @Test + public void shouldReturnNullWhenValueIsNull() { + // Given + final ToDouble function = new ToDouble(); + + // When + final Object output = function.apply(null); + + // Then + assertNull(output); + } + + @Override + protected ToDouble getInstance() { + return new ToDouble(); + } + + @Override + protected Iterable getDifferentInstancesOrNull() { + return null; + } + + @Override + protected Class[] getExpectedSignatureInputClasses() { + return new Class[] { Object.class }; + } + + @Override + protected Class[] getExpectedSignatureOutputClasses() { + return new Class[] { Double.class }; + } + + @Test + @Override + public void shouldJsonSerialiseAndDeserialise() throws IOException { + // Given + final ToDouble function = new ToDouble(); + + // When + final String json = JsonSerialiser.serialise(function); + + // Then + JsonSerialiser.assertEquals(String.format("{%n" + + " \"class\" : \"uk.gov.gchq.koryphe.impl.function.ToDouble\"%n" + + "}"), json); + + // When 2 + final ToDouble deserialisedMethod = JsonSerialiser.deserialise(json, ToDouble.class); + + // Then 2 + assertNotNull(deserialisedMethod); + } +} diff --git a/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIterableTest.java b/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIterableTest.java deleted file mode 100644 index 2ea4c0957..000000000 --- a/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIterableTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2019-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import org.junit.jupiter.api.Test; - -import java.util.Iterator; -import java.util.function.Supplier; -import java.util.stream.Stream; - -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class StreamIterableTest { - - @Test - public void shouldDelegateIteratorToIterable() { - // Given - final Supplier> streamSupplier = mock(Supplier.class); - final Stream stream = mock(Stream.class); - given(streamSupplier.get()).willReturn(stream); - final StreamIterable wrappedIterable = new StreamIterable<>(streamSupplier); - final Iterator iterator = mock(Iterator.class); - given(stream.iterator()).willReturn(iterator); - - // When - final CloseableIterator result = wrappedIterable.iterator(); - - // Then - call has next and check it was called on the mock. - result.hasNext(); - verify(iterator).hasNext(); - } -} diff --git a/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIteratorTest.java b/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIteratorTest.java deleted file mode 100644 index f6ed613b8..000000000 --- a/core/src/test/java/uk/gov/gchq/koryphe/iterable/StreamIteratorTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2019-2020 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.gchq.koryphe.iterable; - -import org.junit.jupiter.api.Test; - -import java.util.stream.Stream; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -public class StreamIteratorTest { - - @Test - public void shouldDelegateCloseToWrappedIterator() { - // Given - final Stream stream = mock(Stream.class); - final StreamIterator streamIterator = new StreamIterator<>(stream); - - // When - streamIterator.close(); - - // Then - verify(stream).close(); - } -} diff --git a/core/src/test/java/uk/gov/gchq/koryphe/tuple/binaryoperator/TupleAdaptedBinaryOperatorCompositeTest.java b/core/src/test/java/uk/gov/gchq/koryphe/tuple/binaryoperator/TupleAdaptedBinaryOperatorCompositeTest.java index 4452f14f6..71dc37a09 100644 --- a/core/src/test/java/uk/gov/gchq/koryphe/tuple/binaryoperator/TupleAdaptedBinaryOperatorCompositeTest.java +++ b/core/src/test/java/uk/gov/gchq/koryphe/tuple/binaryoperator/TupleAdaptedBinaryOperatorCompositeTest.java @@ -15,15 +15,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TupleAdaptedBinaryOperatorCompositeTest + extends BinaryOperatorTest> { -public class TupleAdaptedBinaryOperatorCompositeTest extends BinaryOperatorTest { @Test @Override + @SuppressWarnings("unchecked") public void shouldJsonSerialiseAndDeserialise() throws IOException { // Given - TupleAdaptedBinaryOperatorComposite instance = getInstance(); - String json = "" + + final TupleAdaptedBinaryOperatorComposite instance = getInstance(); + final String json = "" + "{" + "\"operators\": [" + "{" + @@ -36,8 +39,9 @@ public void shouldJsonSerialiseAndDeserialise() throws IOException { "}"; // When - String serialised = JsonSerialiser.serialise(instance); - TupleAdaptedBinaryOperatorComposite deserialised = JsonSerialiser.deserialise(json, TupleAdaptedBinaryOperatorComposite.class); + final String serialised = JsonSerialiser.serialise(instance); + final TupleAdaptedBinaryOperatorComposite deserialised = JsonSerialiser.deserialise(json, + TupleAdaptedBinaryOperatorComposite.class); // Then JsonSerialiser.assertEquals(json, serialised); @@ -46,43 +50,42 @@ public void shouldJsonSerialiseAndDeserialise() throws IOException { } @Override - protected TupleAdaptedBinaryOperatorComposite getInstance() { - return new TupleAdaptedBinaryOperatorComposite.Builder() + protected TupleAdaptedBinaryOperatorComposite getInstance() { + return new TupleAdaptedBinaryOperatorComposite.Builder<>() .select(new String[] { "input", "anotherInput" }) .execute(new Sum()) .build(); } @Override - protected Iterable getDifferentInstancesOrNull() { + protected Iterable> getDifferentInstancesOrNull() { return Arrays.asList( - new TupleAdaptedBinaryOperatorComposite.Builder() + new TupleAdaptedBinaryOperatorComposite.Builder<>() .select(new String[] { "differentInput", "anotherInput" }) .execute(new Sum()) .build(), - new TupleAdaptedBinaryOperatorComposite.Builder() + new TupleAdaptedBinaryOperatorComposite.Builder<>() .select(new String[] { "input", "anotherInput" }) .execute(new Product()) .build(), - new TupleAdaptedBinaryOperatorComposite(), - new TupleAdaptedBinaryOperatorComposite.Builder() + new TupleAdaptedBinaryOperatorComposite<>(), + new TupleAdaptedBinaryOperatorComposite.Builder<>() .select(new String[] { "input", "anotherInput" }) .execute(new Sum()) - .select(new String[] { "input", "differentInput"}) + .select(new String[] { "input", "differentInput" }) .execute(new Max()) - .build() - ); + .build()); } @Test public void shouldErrorIfObjectsAreTheWrongType() { // Given - ArrayTuple stateTuple = new ArrayTuple(5, 10, 15); - ArrayTuple inputTuple = new ArrayTuple(1, "two", 3); + final ArrayTuple stateTuple = new ArrayTuple(5, 10, 15); + final ArrayTuple inputTuple = new ArrayTuple(1, "two", 3); // When - TupleAdaptedBinaryOperatorComposite boc = new TupleAdaptedBinaryOperatorComposite.Builder() - .select(new Integer[]{ 0 }) + final TupleAdaptedBinaryOperatorComposite boc = new TupleAdaptedBinaryOperatorComposite.Builder() + .select(new Integer[] { 0 }) .execute(new Product()) .select(new Integer[] { 1 }) .execute(new Max()) @@ -91,24 +94,20 @@ public void shouldErrorIfObjectsAreTheWrongType() { .build(); // Then - try { - boc.apply(stateTuple, inputTuple); - fail("Expected aggregation to fail"); - } catch (ClassCastException e) { - assertNotNull(e.getMessage()); - } - + final ClassCastException actual = assertThrows(ClassCastException.class, + () -> boc.apply(stateTuple, inputTuple)); + assertNotNull(actual.getMessage()); } @Test public void shouldMergeTheInputTupleIntoTheStateTuple() { // Given - ArrayTuple stateTuple = new ArrayTuple(5, 10, 15); - ArrayTuple inputTuple = new ArrayTuple(1, 2, 3); + final ArrayTuple stateTuple = new ArrayTuple(5, 10, 15); + final ArrayTuple inputTuple = new ArrayTuple(1, 2, 3); // When - TupleAdaptedBinaryOperatorComposite boc = new TupleAdaptedBinaryOperatorComposite.Builder() - .select(new Integer[]{ 0 }) + final TupleAdaptedBinaryOperatorComposite boc = new TupleAdaptedBinaryOperatorComposite.Builder() + .select(new Integer[] { 0 }) .execute(new Product()) .select(new Integer[] { 1 }) .execute(new Max()) @@ -116,13 +115,11 @@ public void shouldMergeTheInputTupleIntoTheStateTuple() { .execute(new Sum()) .build(); - Tuple agg = boc.apply(stateTuple, inputTuple); + final Tuple agg = boc.apply(stateTuple, inputTuple); // Then assertEquals(5, agg.get(0)); assertEquals(10, agg.get(1)); assertEquals(18, agg.get(2)); } - - } \ No newline at end of file diff --git a/core/src/test/java/uk/gov/gchq/koryphe/util/IterableUtilTest.java b/core/src/test/java/uk/gov/gchq/koryphe/util/IterableUtilTest.java index 71067e61d..b87ff6f04 100644 --- a/core/src/test/java/uk/gov/gchq/koryphe/util/IterableUtilTest.java +++ b/core/src/test/java/uk/gov/gchq/koryphe/util/IterableUtilTest.java @@ -19,8 +19,6 @@ import com.google.common.collect.Lists; import org.junit.jupiter.api.Test; -import uk.gov.gchq.koryphe.iterable.CloseableIterable; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -112,7 +110,7 @@ public void shouldLimitResultsToFirstItem() { final int end = 1; // When - final CloseableIterable limitedValues = IterableUtil.limit(values, start, end, true); + final Iterable limitedValues = IterableUtil.limit(values, start, end, true); // Then assertEquals(values.subList(start, end), Lists.newArrayList(limitedValues)); @@ -126,7 +124,7 @@ public void shouldLimitResultsToLastItem() { final int end = Integer.MAX_VALUE; // When - final CloseableIterable limitedValues = IterableUtil.limit(values, start, end, true); + final Iterable limitedValues = IterableUtil.limit(values, start, end, true); // Then assertEquals(values.subList(start, values.size()), Lists.newArrayList(limitedValues)); @@ -140,7 +138,7 @@ public void shouldNotLimitResults() { final int end = Integer.MAX_VALUE; // When - final CloseableIterable limitedValues = IterableUtil.limit(values, start, end, true); + final Iterable limitedValues = IterableUtil.limit(values, start, end, true); // Then assertEquals(values, Lists.newArrayList(limitedValues)); @@ -154,7 +152,7 @@ public void shouldReturnNoValuesIfStartIsBiggerThanSize() { final int end = Integer.MAX_VALUE; // When - final CloseableIterable limitedValues = IterableUtil.limit(values, start, end, true); + final Iterable limitedValues = IterableUtil.limit(values, start, end, true); // Then assertTrue(Lists.newArrayList(limitedValues).isEmpty()); @@ -183,7 +181,7 @@ public void shouldThrowExceptionIfDataIsTruncated() { final boolean truncate = false; // When - final CloseableIterable limitedValues = IterableUtil.limit(values, start, end, truncate); + final Iterable limitedValues = IterableUtil.limit(values, start, end, truncate); // Then final Exception exception = assertThrows(NoSuchElementException.class, () -> { @@ -197,7 +195,7 @@ public void shouldThrowExceptionIfDataIsTruncated() { @Test public void shouldHandleNullIterable() { // Given - final CloseableIterable nullIterable = IterableUtil.limit(null, 0, 1, true); + final Iterable nullIterable = IterableUtil.limit(null, 0, 1, true); // Then assertTrue(Lists.newArrayList(nullIterable).isEmpty()); @@ -212,7 +210,7 @@ public void shouldHandleLimitEqualToIterableLength() { final boolean truncate = false; // When - final CloseableIterable equalValues = IterableUtil.limit(values, start, end, truncate); + final Iterable equalValues = IterableUtil.limit(values, start, end, truncate); // Then assertEquals(values, Lists.newArrayList(equalValues));