Created
January 1, 2020 04:22
-
-
Save joshiraez/c57367e8eebfc732c60df5d94b935150 to your computer and use it in GitHub Desktop.
Transforming it to an abstract approach instead of interface approach (having some part of the code already implemented).
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
class OperationAbstractAsClass implements Operation { | |
Predicate<String> isOperation; | |
IntBinaryOperator reductionOperation; | |
private OperationAbstractAsClass(){}; | |
public static OperationAbstractAsClass of( | |
Predicate<String> isOperation, | |
IntBinaryOperator reductionOperation | |
){ | |
OperationAbstractAsClass toBuild = new OperationAbstractAsClass(); | |
toBuild.isOperation = isOperation; | |
toBuild.reductionOperation = reductionOperation; | |
return toBuild; | |
} | |
@Override | |
public boolean isOperation(final String input) { | |
return this.isOperation.test(input); | |
} | |
@Override | |
public int operate(final List<Integer> numbers) { | |
return this.operateWithGivenReductionOperation(numbers); | |
} | |
private int operateWithGivenReductionOperation(final List<Integer> numbers) { | |
return numbers | |
.stream() | |
.mapToInt(Integer::intValue) | |
.reduce( | |
reductionOperation | |
).getAsInt(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment