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 #1513 from cdowding-sl/feature/reorganise_profile_…
Browse files Browse the repository at this point in the history
…module/restructure

Restructured profile module
  • Loading branch information
cdowding-sl authored Nov 1, 2019
2 parents 0117c7c + 4a6ffd6 commit 633cf64
Show file tree
Hide file tree
Showing 198 changed files with 1,948 additions and 1,753 deletions.
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
compile "com.fasterxml.jackson.core:jackson-core:${JACKSON_VERSION}"
compile "com.fasterxml.jackson.core:jackson-annotations:${JACKSON_VERSION}"
compile "com.fasterxml.jackson.core:jackson-databind:${JACKSON_VERSION}"
compile group: "com.google.inject", name: "guice", version: "${GUICE_VERSION}"

implementation "org.threeten:threeten-extra:${THREE_TEN_VERSION}"
testCompile "org.junit.jupiter:junit-jupiter-api:${JUNIT_JUPITER_VERSION}"
testCompile "junit:junit:${JUNIT_4_VERSION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ValidationException(String msg) {
}

public ValidationException(List<String> errorMessages){
super();
super(String.join("\n", errorMessages));
this.errorMessages = errorMessages;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.scottlogic.deg.common.commands;

import java.lang.reflect.Type;

public abstract class Command<TResponse>
{
public Type commandType;

protected Command()
{
commandType = getClass();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.scottlogic.deg.common.commands;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

import java.util.HashMap;
import java.util.Map;

public abstract class CommandBus
{
private final Map<Class<?>, Pair<Class<? extends CommandHandler>, ? extends CommandHandler>> map = new HashMap<>();

protected <TCommand extends Command<TResponse>, TResponse> void register(Class<TCommand> commandClass, CommandHandler handler)
{
map.put(commandClass, new ImmutablePair<>(handler.getClass(), handler));
}

public <TCommand extends Command<TResponse>, TResponse> CommandResult<TResponse> send(TCommand command)
{
Class<? extends Command> commandClass = command.getClass();
Class<? extends CommandHandler> commandHandlerClass = map.get(commandClass).getKey();
CommandHandler commandHandler = map.get(commandClass).getValue();

return commandHandlerClass.cast(commandHandler).handle(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.scottlogic.deg.common.commands;

import com.scottlogic.deg.common.validators.ValidationResult;
import com.scottlogic.deg.common.validators.Validator;

public abstract class CommandHandler<TCommand extends Command<TResponse>, TResponse>
{
private final Validator<TCommand> validator;

protected CommandHandler(Validator<TCommand> validator)
{
this.validator = validator;
}

public CommandResult<TResponse> handle(TCommand command)
{
ValidationResult validationResult = validator.validate(command);
if(validationResult.isValid) return handleCommand(command);
return CommandResult.failure(validationResult.errors);
}

public abstract CommandResult<TResponse> handleCommand(TCommand command);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.scottlogic.deg.common.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CommandResult<T>
{
public final T value;
public final boolean isSuccess;
public final List<String> errors;

private CommandResult(T value, boolean isSuccess, List<String> errors)
{
this.value = value;
this.isSuccess = isSuccess;
this.errors = errors;
}

public static <T> CommandResult<T> success(T value)
{
return new CommandResult<>(value, true, new ArrayList<>());
}

@SafeVarargs
public static <T> CommandResult<T> failure(List<String>... errors)
{
return new CommandResult<>(null, false, Arrays.stream(errors).flatMap(Collection::stream).collect(Collectors.toList()));
}

public <TResult> CommandResult<TResult> map(Function<T, TResult> mapping)
{
return isSuccess ? success(mapping.apply(value)) : failure(errors);
}


public static <T> CommandResult<List<T>> combine(List<CommandResult<T>> results)
{
List<T> values = new ArrayList<>();
List<String> errors = new ArrayList<>();
results.forEach(result ->
{
if(result.isSuccess) values.add(result.value);
else errors.addAll(result.errors);
});

return values.size() == results.size() ? CommandResult.success(values) : CommandResult.failure(errors);
}
}
47 changes: 32 additions & 15 deletions common/src/main/java/com/scottlogic/deg/common/profile/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,51 @@
import java.util.Objects;

public class Field {
public final String name;
private final FieldType type;
private final String name;
private final SpecificFieldType type;
private final boolean unique;
private final String formatting;
private final boolean internal;
private final boolean nullable;

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

public boolean isInternal() {
return internal;
public FieldType getType() {
return type.getFieldType();
}

public SpecificFieldType getSpecificType() {
return type;
}

public boolean isUnique() {
return unique;
}

public String getFormatting() {
return formatting;
}

public boolean isInternal() {
return internal;
}

public boolean isNullable()
{
return nullable;
}


@Override
public String toString() {
return this.name;
return this.getName();
}

@Override
Expand All @@ -54,20 +74,17 @@ public boolean equals(Object o) {
return Objects.equals(name, field.name)
&& Objects.equals(unique, field.unique)
&& Objects.equals(type, field.type)
&& Objects.equals(formatting, field.formatting);
&& Objects.equals(formatting, field.formatting)
&& Objects.equals(nullable, field.nullable);
}

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

public String getFormatting() {
return formatting;
return Objects.hash(name, unique, formatting, type, nullable);
}

public FieldType getType() {
return type;
public String getName()
{
return name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
import java.util.List;
import java.util.stream.Stream;

public class ProfileFields implements Iterable<Field> {
public class Fields implements Iterable<Field> {
private final List<Field> fields;

public ProfileFields(List<Field> fields) {
public Fields(List<Field> fields) {
this.fields = fields;
}

public Field getByName(String fieldName) {
return this.fields.stream()
.filter(f -> f.name.equals(fieldName))
.filter(f -> f.getName().equals(fieldName))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Profile fields do not contain " + fieldName));
}
Expand Down Expand Up @@ -66,8 +66,8 @@ public boolean equals(Object obj) {
return false;
}

ProfileFields profileFields = (ProfileFields) obj;
return fields.equals(profileFields.fields);
Fields fields = (Fields) obj;
return this.fields.equals(fields.fields);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.scottlogic.deg.common.validators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ValidationResult
{
public final boolean isValid;
public final List<String> errors;

private ValidationResult(boolean isValid, List<String> errors)
{
this.isValid = isValid;
this.errors = errors;
}

public static ValidationResult success(){
return new ValidationResult(true, new ArrayList<>());
}

public static ValidationResult failure(String... errors)
{
return new ValidationResult(false, Arrays.stream(errors).collect(Collectors.toList()));
}

public static ValidationResult failure(List<String> errors)
{
return new ValidationResult(false, errors);
}

public static ValidationResult combine(List<ValidationResult> validationResults)
{
boolean isValid = validationResults.stream()
.allMatch(validationResult -> validationResult.isValid);
List<String> errors = validationResults.stream()
.flatMap(validationResult -> validationResult.errors.stream())
.collect(Collectors.toList());
return new ValidationResult(isValid, errors);
}

public static ValidationResult combine(ValidationResult... validationResults)
{
boolean isValid = Arrays.stream(validationResults)
.allMatch(validationResult -> validationResult.isValid);
List<String> errors = Arrays.stream(validationResults)
.flatMap(validationResult -> validationResult.errors.stream())
.collect(Collectors.toList());
return new ValidationResult(isValid, errors);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.scottlogic.deg.common.validators;

public interface Validator<T>
{
ValidationResult validate(T obj);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

public class FieldBuilder {
public static Field createField(String name) {
return createField(name, FieldType.STRING);
return createField(name, SpecificFieldType.STRING);
}
public static Field createInternalField(String name) {
return createInternalField(name, FieldType.STRING);
return createInternalField(name, SpecificFieldType.STRING);
}
public static Field createField(String name, FieldType type) {
return new Field(name, type, false, null, false);
public static Field createField(String name, SpecificFieldType type) {
return new Field(name, type, false, null, false, false);
}
public static Field createInternalField(String name, FieldType type) {
return new Field(name, type, false, null, true);
public static Field createInternalField(String name, SpecificFieldType type) {
return new Field(name, type, false, null, true, false);
}
}
Loading

0 comments on commit 633cf64

Please sign in to comment.