Skip to content

Commit

Permalink
gh-67 - added Util, updated classes, tests, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
m607123 committed Dec 18, 2017
1 parent 7ccbf76 commit f848f65
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import uk.gov.gchq.koryphe.predicate.KoryphePredicate;

/**
* An <code>AgeOff</code> is a {@link java.util.function.Predicate} that ages off old data based on a provided age of time in milliseconds.
* An <code>AgeOff</code> is a {@link java.util.function.Predicate} that ages off old data based on a provided age off time in milliseconds.
*/
public class AgeOff extends KoryphePredicate<Long> {
public static final long HOURS_TO_MILLISECONDS = 60L * 60L * 1000L;
public static final long DAYS_TO_MILLISECONDS = 24L * HOURS_TO_MILLISECONDS;

/**
* The default age of time (1 year) in milliseconds.
* The default age off time (1 year) in milliseconds.
*/
public static final long AGE_OFF_TIME_DEFAULT = 365L * DAYS_TO_MILLISECONDS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import uk.gov.gchq.koryphe.ValidationResult;
import uk.gov.gchq.koryphe.predicate.KoryphePredicate;
import uk.gov.gchq.koryphe.signature.InputValidator;
import uk.gov.gchq.koryphe.signature.TypeResolver;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

/**
* An <code>IsLessThan</code> is a {@link java.util.function.Predicate} that checks that the input
* {@link Comparable} is less than a control value. There is also an orEqualTo flag that can be set to allow
* the input value to be less than or equal to the control value.
*/
public class IsLessThan extends KoryphePredicate<Comparable> implements InputValidator {
public class IsLessThan extends KoryphePredicate<Comparable>
implements InputValidator,
TypeResolver {
private Comparable controlValue;
private boolean orEqualTo;

Expand Down Expand Up @@ -94,12 +98,25 @@ public ValidationResult isInputValid(final Class<?>... arguments) {
}

if (!controlValue.getClass().isAssignableFrom(arguments[0])) {
result.addError("Control value class " + controlValue.getClass().getName() + " is not compatible with the input type: " + arguments[0]);
result.add(resolveTypes(arguments[0]));
}

return result;
}

@Override
public ValidationResult resolveTypes(final Class<?>... arguments) {
final ValidationResult result = new ValidationResult();
final String json;
try {
json = JsonSerialiserUtil.serialise(controlValue);
controlValue = (Comparable) JsonSerialiserUtil.deserialise(json, arguments[0]);
} catch (final Exception e) {
result.addError("Control value class " + controlValue.getClass().getName() + " is not compatible with the input type: " + arguments[0]);
}
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import uk.gov.gchq.koryphe.ValidationResult;
import uk.gov.gchq.koryphe.predicate.KoryphePredicate;
import uk.gov.gchq.koryphe.signature.InputValidator;
import uk.gov.gchq.koryphe.signature.TypeResolver;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

/**
* An <code>IsMoreThan</code> is a {@link java.util.function.Predicate} that checks that the input
* {@link Comparable} is more than a control value. There is also an orEqualTo flag that can be set to allow
* the input value to be more than or equal to the control value.
*/
public class IsMoreThan extends KoryphePredicate<Comparable> implements InputValidator {
public class IsMoreThan extends KoryphePredicate<Comparable>
implements InputValidator,
TypeResolver {
private Comparable controlValue;
private boolean orEqualTo;

Expand Down Expand Up @@ -128,9 +132,22 @@ public ValidationResult isInputValid(final Class<?>... arguments) {
}

if (!controlValue.getClass().isAssignableFrom(arguments[0])) {
result.addError("Control value class " + controlValue.getClass().getName() + " is not compatible with the input type: " + arguments[0]);
result.add(resolveTypes(arguments[0]));
}

return result;
}

@Override
public ValidationResult resolveTypes(final Class<?>... arguments) {
final ValidationResult result = new ValidationResult();
final String json;
try {
json = JsonSerialiserUtil.serialise(controlValue);
controlValue = (Comparable) JsonSerialiserUtil.deserialise(json, arguments[0]);
} catch (final Exception e) {
result.addError("Control value class " + controlValue.getClass().getName() + " is not compatible with the input type: " + arguments[0]);
}
return result;
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/uk/gov/gchq/koryphe/signature/TypeResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017 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.signature;

import uk.gov.gchq.koryphe.ValidationResult;

public interface TypeResolver {
ValidationResult resolveTypes(final Class<?>... arguments);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 Crown Copyright
* Copyright 2017 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 All @@ -13,18 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.gchq.koryphe.example.util;
package uk.gov.gchq.koryphe.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;

public final class JsonSerialiser {
private JsonSerialiser() { }
public final class JsonSerialiserUtil {
private JsonSerialiserUtil() {
}

private static final ObjectMapper MAPPER = createObjectMapper();

Expand All @@ -36,8 +37,8 @@ private static ObjectMapper createObjectMapper() {
return mapper;
}

public static String serialise(final Object object) throws IOException {
return MAPPER.writeValueAsString(object);
public static String serialise(final Object obj) throws IOException {
return MAPPER.writeValueAsString(obj);
}

public static <T> T deserialise(final String json, final Class<T> type) throws IOException {
Expand All @@ -47,4 +48,8 @@ public static <T> T deserialise(final String json, final Class<T> type) throws I
public static <T> T deserialise(final String json, final TypeReference<T> typeReference) throws IOException {
return MAPPER.readValue(json, typeReference);
}

public static <T> T deserialise(final byte[] bytes, final Class<T> clazz) throws IOException {
return MAPPER.readValue(bytes, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,25 @@ public void shouldJsonSerialiseAndDeserialiseComplex() throws IOException {
public void shouldCheckInputClass() {
And<?> predicate = new And<>(new IsMoreThan(1), new IsLessThan(10));
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());

predicate = new And<>(new IsMoreThan(1.0), new IsLessThan(10.0));
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());

predicate = new And<>(new IsMoreThan(1), new IsLessThan(10.0));
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Double.class).isValid());

predicate = new And<>(
new IntegerTupleAdaptedPredicate(new IsMoreThan(1), 0),
new IntegerTupleAdaptedPredicate(new IsLessThan(10.0), 1)
);
assertTrue(predicate.isInputValid(Integer.class, Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class, Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class, Integer.class).isValid());


predicate = new And.Builder()
Expand All @@ -236,7 +237,7 @@ public void shouldCheckInputClass() {
.build();
assertTrue(predicate.isInputValid(Integer.class, Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class, Integer.class).isValid());
assertTrue(predicate.isInputValid(Double.class, Integer.class).isValid());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import uk.gov.gchq.koryphe.util.JsonSerialiser;

import java.io.IOException;
import java.util.Date;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -144,10 +145,25 @@ public void shouldCheckInputClass() {
final IsLessThan predicate = new IsLessThan(1);

assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());
}

@Test
public void shouldDeserialiseToDate() throws IOException {
final String json = String.format("{%n" +
" \"class\" : \"uk.gov.gchq.koryphe.impl.predicate.IsLessThan\",%n" +
" \"orEqualTo\" : false,%n" +
" \"value\" : 10000000000%n" +
"}");

final IsLessThan deserialised = JsonSerialiser.deserialise(json, IsLessThan.class);

deserialised.resolveTypes(Date.class);

assertEquals(new Date(10000000000L), deserialised.getControlValue());
}

@Override
protected Class<IsLessThan> getPredicateClass() {
return IsLessThan.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void shouldCheckInputClass() {
final IsMoreThan predicate = new IsMoreThan(1);

assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void shouldCheckInputClass() {
Predicate predicate = new Not<>(new IsMoreThan(1));
Signature input = Signature.getInputSignature(predicate);
assertTrue(input.assignable(Integer.class).isValid());
assertFalse(input.assignable(Double.class).isValid());
assertTrue(input.assignable(Double.class).isValid());
assertFalse(input.assignable(Integer.class, Integer.class).isValid());

predicate = new Not<>(new IsXLessThanY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,26 @@ public void shouldJsonSerialiseAndDeserialiseComplex() throws IOException {
public void shouldCheckInputClass() {
Or<?> predicate = new Or<>(new IsMoreThan(1), new IsLessThan(10));
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());

predicate = new Or<>(new IsMoreThan(1.0), new IsLessThan(10.0));
assertTrue(predicate.isInputValid(Double.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Integer.class).isValid());

predicate = new Or<>(new IsMoreThan(1), new IsLessThan(10.0));
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertTrue(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class, Double.class).isValid());

predicate = new Or<>(
new IntegerTupleAdaptedPredicate(new IsMoreThan(1), 0),
new IntegerTupleAdaptedPredicate(new IsLessThan(10.0), 1)
);
assertTrue(predicate.isInputValid(Integer.class, Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class, Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class, Integer.class).isValid());


predicate = new Or.Builder()
.select(0)
Expand All @@ -235,8 +237,8 @@ public void shouldCheckInputClass() {
.execute(new IsLessThan(10.0))
.build();
assertTrue(predicate.isInputValid(Integer.class, Double.class).isValid());
assertTrue(predicate.isInputValid(Double.class, Integer.class).isValid());
assertFalse(predicate.isInputValid(Integer.class).isValid());
assertFalse(predicate.isInputValid(Double.class, Integer.class).isValid());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.gchq.koryphe.example;

import uk.gov.gchq.koryphe.example.util.JsonSerialiser;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

import java.util.Optional;
import java.util.function.BinaryOperator;
Expand All @@ -27,7 +27,7 @@ public abstract class KorypheBinaryOperatorExample<T> extends KorypheExample<T,
@Override
protected void executeExample() throws Exception {
System.out.println("Binary Operator json: ");
System.out.println(JsonSerialiser.serialise(getBinaryOperator()));
System.out.println(JsonSerialiserUtil.serialise(getBinaryOperator()));
System.out.println();
System.out.println("Binary Operator inputs: ");
getInput().forEach(i -> printInput(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.gchq.koryphe.example;

import uk.gov.gchq.koryphe.example.util.JsonSerialiser;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

import java.util.function.Function;

Expand All @@ -26,7 +26,7 @@ public abstract class KorypheFunctionExample<I, O> extends KorypheExample<I, O>
@Override
protected void executeExample() throws Exception {
System.out.println("Function json: ");
System.out.println(JsonSerialiser.serialise(getFunction()));
System.out.println(JsonSerialiserUtil.serialise(getFunction()));
System.out.println();
System.out.println("Function inputs: ");
getInput().forEach(this::printInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package uk.gov.gchq.koryphe.example;

import uk.gov.gchq.koryphe.example.util.JsonSerialiser;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

import java.util.function.Predicate;

Expand All @@ -26,7 +26,7 @@ public abstract class KoryphePredicateExample<I> extends KorypheExample<I, I> {
@Override
protected void executeExample() throws Exception {
System.out.println("Predicate json: ");
System.out.println(JsonSerialiser.serialise(getPredicate()));
System.out.println(JsonSerialiserUtil.serialise(getPredicate()));
System.out.println();
System.out.println("Predicate inputs: ");
getInput().forEach(this::printInput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package uk.gov.gchq.koryphe.example.function.iterable;

import uk.gov.gchq.koryphe.example.KorypheFunctionExample;
import uk.gov.gchq.koryphe.example.util.JsonSerialiser;
import uk.gov.gchq.koryphe.util.JsonSerialiserUtil;

public abstract class KorypheIterableFunctionExample<I extends Iterable, O extends Iterable> extends KorypheFunctionExample<I, O> {
@Override
protected void executeExample() throws Exception {
System.out.println("Function json: ");
System.out.println(JsonSerialiser.serialise(getFunction()));
System.out.println(JsonSerialiserUtil.serialise(getFunction()));
System.out.println();
System.out.println("Function inputs: ");
getInput().forEach(this::printInput);
Expand Down

0 comments on commit f848f65

Please sign in to comment.