This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1513 from cdowding-sl/feature/reorganise_profile_…
…module/restructure Restructured profile module
- Loading branch information
Showing
198 changed files
with
1,948 additions
and
1,753 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
common/src/main/java/com/scottlogic/deg/common/commands/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
common/src/main/java/com/scottlogic/deg/common/commands/CommandBus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
common/src/main/java/com/scottlogic/deg/common/commands/CommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
common/src/main/java/com/scottlogic/deg/common/commands/CommandResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
common/src/main/java/com/scottlogic/deg/common/validators/ValidationResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
common/src/main/java/com/scottlogic/deg/common/validators/Validator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.