Skip to content

Commit

Permalink
Merge branch 'develop' into gh-194-update-to-java-11
Browse files Browse the repository at this point in the history
  • Loading branch information
t92549 committed Feb 3, 2022
2 parents 5d1e6c7 + 88d8b1c commit 5463b94
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 440 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String>, Iterable<Map<String, Object>>> implements Serializable {
public class CsvLinesToMaps extends KorypheFunction<Iterable<String>, Iterable<Map<String, Object>>>
implements Serializable {
private static final long serialVersionUID = -4225921410795200955L;
private List<String> header = new ArrayList<>();
private int firstRow = 0;
Expand All @@ -62,7 +61,7 @@ public Iterable<Map<String, Object>> apply(final Iterable<String> csvStrings) {
return null;
}

final CloseableIterable<String> csvRecords = IterableUtil.limit(csvStrings, firstRow, null, false);
final Iterable<String> csvRecords = IterableUtil.limit(csvStrings, firstRow, null, false);
return IterableUtil.map(csvRecords, (item) -> createMap((String) item));
}

Expand All @@ -81,8 +80,8 @@ private Map<String, Object> 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);
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Iterable<Map<String, Object>>> implements Serializable {
private static final long serialVersionUID = 891938487168606844L;
Expand All @@ -62,12 +61,14 @@ public Iterable<Map<String, Object>> apply(final String csv) {
return null;
}

try {
final CSVParser csvParser = new CSVParser(new StringReader(csv), getCsvFormat());
final CloseableIterable<CSVRecord> csvRecords = IterableUtil.limit(csvParser.getRecords(), firstRow, null, false);
return IterableUtil.map(csvRecords, item -> extractMap((CSVRecord) item));
Iterable<CSVRecord> 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);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -194,5 +196,4 @@ public int hashCode() {
.append(delimiter)
.toHashCode();
}

}
49 changes: 49 additions & 0 deletions core/src/main/java/uk/gov/gchq/koryphe/impl/function/ToDouble.java
Original file line number Diff line number Diff line change
@@ -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 <code>ToDouble</code> is a {@link java.util.function.Function} that takes
* an Object and casts it to a Double.
* <p>
* 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<Object, Double> {

@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);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5463b94

Please sign in to comment.