Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1481 from finos/1430-custom-generator-interface
Browse files Browse the repository at this point in the history
1430 custom generator interface
  • Loading branch information
cdowding-sl authored Nov 11, 2019
2 parents b22e440 + 3d96dec commit 1378282
Show file tree
Hide file tree
Showing 69 changed files with 882 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ public class Field {
private final String formatting;
private final boolean internal;
private final boolean nullable;
private final String generator;

public Field(String name, SpecificFieldType type, boolean unique, String formatting, boolean internal, boolean nullable) {
public Field(String name, SpecificFieldType type, boolean unique, String formatting, boolean internal, boolean nullable, String generator) {
this.name = name;
this.type = type;
this.unique = unique;
this.formatting = formatting;
this.internal = internal;
this.nullable = nullable;
this.generator = generator;
}

public FieldType getType() {
Expand Down Expand Up @@ -60,6 +62,9 @@ public boolean isNullable()
return nullable;
}

public boolean usesCustomGenerator() { return generator != null; }

public String getCustomGeneratorName() { return generator; }

@Override
public String toString() {
Expand All @@ -75,12 +80,13 @@ public boolean equals(Object o) {
&& Objects.equals(unique, field.unique)
&& Objects.equals(type, field.type)
&& Objects.equals(formatting, field.formatting)
&& Objects.equals(nullable, field.nullable);
&& Objects.equals(nullable, field.nullable)
&& Objects.equals(generator, field.generator);
}

@Override
public int hashCode() {
return Objects.hash(name, unique, formatting, type, nullable);
return Objects.hash(name, unique, formatting, type, nullable, generator);
}

public String getName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public static Field createInternalField(String name) {
return createInternalField(name, SpecificFieldType.STRING);
}
public static Field createField(String name, SpecificFieldType type) {
return new Field(name, type, false, null, false, false);
return new Field(name, type, false, null, false, false, null);
}
public static Field createInternalField(String name, SpecificFieldType type) {
return new Field(name, type, false, null, true, false);
return new Field(name, type, false, null, true, false, null);
}
}
3 changes: 3 additions & 0 deletions custom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
out/
bin/
15 changes: 15 additions & 0 deletions custom/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id 'java'
}

group 'com.scottlogic.deg'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* 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 com.scottlogic.deg.custom;

import java.util.stream.Stream;

public interface CustomGenerator<T> {

/***
* REQUIRED! used in profile reading
* @return the name of the custom generator
*/
String generatorName();

/**
* STRING for Strings
* NUMERIC for BigDecimals
* DATETIME for OffsetDateTimes
* @return accepted field type
*/
CustomGeneratorFieldType fieldType();

/**
* the part of the generator to be used during random generation
*
* Required if you want your custom generator to support random generation
*
* @return your stream of random values
*/
Stream<T> generateRandom();

/**
* the part of the generator to be used when the generator constraint is negated during random generation
* - this should be implemented as the values that your regular generator should not be outputting
*
* Required if you want your custom generator to support being negated in random mode
* Required if you want your custom generator to support used in the IF part of IF THEN constraints
*
* @return your stream of random values that are not what would be produced by the generator if it were not negated
*/
Stream<T> generateNegatedRandom();

/**
* the part of the generator to be used during sequential generation
*
* Required if you want your custom generator to support sequential generation
* Required if you want your custom generator to support unique keys generation
*
* @return your stream of random values
*/
Stream<T> generateSequential();

/**
* the part of the generator to be used when the generator constraint is negated during sequential generation
*
* Required if you want your custom generator to support being negated in sequential mode
* should not be used with unique keys generation
*
* @return your stream of sequential values that are not what would be produced by the generator if it were not negated
*/
Stream<T> generateNegatedSequential();

/**
* The function to check whether a value from a set can be generated from this generator
*
* Required if you want your custom generator to support being combined with inSet and equalTo constraints
*
* @param value the value from the set to be tested against
*
* @return true if the generator would be able to produce the value
*/
boolean setMatchingFunction(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* 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 com.scottlogic.deg.custom;

public enum CustomGeneratorFieldType {
NUMERIC,
STRING,
DATETIME
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* 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 com.scottlogic.deg.custom;

import com.scottlogic.deg.custom.example.LoremIpsumGeneratorCreator;

import java.util.Arrays;
import java.util.List;

public class CustomGeneratorList {
public List<CustomGenerator> get() {
return Arrays.asList(
LoremIpsumGeneratorCreator.create()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* 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 com.scottlogic.deg.custom.builder;

import com.scottlogic.deg.custom.CustomGenerator;
import com.scottlogic.deg.custom.CustomGeneratorFieldType;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class BuiltCustomGenerator<T> implements CustomGenerator<T> {

private final CustomGeneratorFieldType fieldType;
private final String name;
private final Function<T, Boolean> matchingFunction;
private final Supplier<Stream<T>> randomGenerator;
private final Supplier<Stream<T>> negatedRandomGenerator;
private final Supplier<Stream<T>> sequentialGenerator;
private final Supplier<Stream<T>> negatedSequentialGenerator;

public BuiltCustomGenerator(CustomGeneratorFieldType fieldType,
String name,
Function<T, Boolean> matchingFunction,
Supplier<Stream<T>> randomGenerator,
Supplier<Stream<T>> negatedRandomGenerator,
Supplier<Stream<T>> sequentialGenerator,
Supplier<Stream<T>> negatedSequentialGenerator) {
this.fieldType = fieldType;
this.name = name;
this.matchingFunction = matchingFunction;
this.randomGenerator = randomGenerator;
this.negatedRandomGenerator = negatedRandomGenerator;
this.sequentialGenerator = sequentialGenerator;
this.negatedSequentialGenerator = negatedSequentialGenerator;
}

@Override
public String generatorName() {
return name;
}

@Override
public CustomGeneratorFieldType fieldType() {
return fieldType;
}

@Override
public Stream<T> generateRandom() {
return randomGenerator.get();
}

@Override
public Stream<T> generateNegatedRandom() {
return negatedRandomGenerator.get();
}

@Override
public Stream<T> generateSequential() {
return sequentialGenerator.get();
}

@Override
public Stream<T> generateNegatedSequential() {
return negatedSequentialGenerator.get();
}

@Override
public boolean setMatchingFunction(T value) {
return matchingFunction.apply(value);
}
}
Loading

0 comments on commit 1378282

Please sign in to comment.