forked from ibanknatoPrad/opel
-
Notifications
You must be signed in to change notification settings - Fork 0
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 allegro#4 from rzukow/functions_and_variables_in_e…
…ngine registering functions and variables in engine
- Loading branch information
Showing
16 changed files
with
1,249 additions
and
908 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,3 @@ classes | |
|
||
# mac os x | ||
.DS_Store | ||
|
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
100 changes: 100 additions & 0 deletions
100
src/main/java/pl/allegro/tech/opel/EvalContextBuilder.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,100 @@ | ||
package pl.allegro.tech.opel; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class EvalContextBuilder { | ||
private final Map<String, CompletableFuture<Object>> variables = new HashMap<>(); | ||
private final Map<String, OpelAsyncFunction<?>> functions = new HashMap<>(); | ||
private Optional<EvalContext> parentEvalContext = Optional.empty(); | ||
|
||
static EvalContext fromMaps(Map<String, CompletableFuture<Object>> variables, Map<String, OpelAsyncFunction<?>> functions) { | ||
return new EvalContext() { | ||
@Override | ||
public Optional<OpelAsyncFunction<?>> getFunction(String name) { | ||
return Optional.ofNullable(functions.get(name)); | ||
} | ||
|
||
@Override | ||
public Optional<CompletableFuture<?>> getVariable(String name) { | ||
return Optional.ofNullable(variables.get(name)); | ||
} | ||
}; | ||
} | ||
|
||
public static EvalContextBuilder create() { | ||
return new EvalContextBuilder(); | ||
} | ||
|
||
public EvalContextBuilder withParentEvalContext(EvalContext evalContext) { | ||
this.parentEvalContext = Optional.of(evalContext); | ||
return this; | ||
} | ||
|
||
public EvalContextBuilder withVariable(String variableName, CompletableFuture<Object> variable) { | ||
variables.put(variableName, variable); | ||
return this; | ||
} | ||
|
||
public EvalContextBuilder withVariables(Map<String, CompletableFuture<Object>> variables) { | ||
this.variables.putAll(variables); | ||
return this; | ||
} | ||
|
||
public EvalContextBuilder withCompletedVariable(String variableName, Object variable) { | ||
variables.put(variableName, CompletableFuture.completedFuture(variable)); | ||
return this; | ||
} | ||
|
||
public EvalContextBuilder withFunction(String functionName, OpelAsyncFunction<?> function) { | ||
functions.put(functionName, function); | ||
return this; | ||
} | ||
|
||
public EvalContextBuilder withFunctions(Map<String, OpelAsyncFunction<?>> functions) { | ||
this.functions.putAll(functions); | ||
return this; | ||
} | ||
|
||
public EvalContext build() { | ||
return parentEvalContext.map(this::mergeContexts).orElse(fromMaps(variables, functions)); | ||
} | ||
|
||
EvalContext mergeContexts(EvalContext parent) { | ||
return new EvalContext() { | ||
@Override | ||
public Optional<OpelAsyncFunction<?>> getFunction(String name) { | ||
if (functions.containsKey(name)) { | ||
return Optional.ofNullable(functions.get(name)); | ||
} | ||
return parent.getFunction(name); | ||
} | ||
|
||
@Override | ||
public Optional<CompletableFuture<?>> getVariable(String name) { | ||
if (variables.containsKey(name)) { | ||
return Optional.ofNullable(variables.get(name)); | ||
} | ||
return parent.getVariable(name); | ||
} | ||
}; | ||
} | ||
|
||
static EvalContext mergeContexts(EvalContext primary, EvalContext secondary) { | ||
return new EvalContext() { | ||
@Override | ||
public Optional<OpelAsyncFunction<?>> getFunction(String name) { | ||
Optional<OpelAsyncFunction<?>> function = primary.getFunction(name); | ||
return function.isPresent() ? function : secondary.getFunction(name); | ||
} | ||
|
||
@Override | ||
public Optional<CompletableFuture<?>> getVariable(String name) { | ||
Optional<CompletableFuture<?>> variable = primary.getVariable(name); | ||
return (variable.isPresent()) ? variable : secondary.getVariable(name); | ||
} | ||
}; | ||
} | ||
} |
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
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,65 @@ | ||
package pl.allegro.tech.opel; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Function; | ||
|
||
public class OpelEngineBuilder { | ||
private final Map<String, OpelAsyncFunction<?>> embeddedFunctions = new HashMap<>(); | ||
private final Map<String, CompletableFuture<Object>> embeddedVariables = new HashMap<>(); | ||
private MethodExecutionFilter methodExecutionFilter = MethodExecutionFilters.ALLOW_ALL; | ||
private final ImplicitConversion implicitConversion; | ||
|
||
private OpelEngineBuilder() { | ||
implicitConversion = new ImplicitConversion(); | ||
implicitConversion.registerNumberConversion(); | ||
} | ||
|
||
public static OpelEngineBuilder create() { | ||
return new OpelEngineBuilder(); | ||
} | ||
|
||
public OpelEngineBuilder withFunction(String functionName, OpelAsyncFunction<?> function) { | ||
embeddedFunctions.put(functionName, function); | ||
return this; | ||
} | ||
|
||
public OpelEngineBuilder withFunctions(Map<String, OpelAsyncFunction<?>> functions) { | ||
embeddedFunctions.putAll(functions); | ||
return this; | ||
} | ||
|
||
public OpelEngineBuilder withVariable(String variableName, CompletableFuture<Object> value) { | ||
embeddedVariables.put(variableName, value); | ||
return this; | ||
} | ||
|
||
public OpelEngineBuilder withVariables(Map<String, CompletableFuture<Object>> variables) { | ||
embeddedVariables.putAll(variables); | ||
return this; | ||
} | ||
|
||
public OpelEngineBuilder withCompletedVariable(String variableName, Object value) { | ||
embeddedVariables.put(variableName, CompletableFuture.completedFuture(value)); | ||
return this; | ||
} | ||
|
||
public OpelEngineBuilder withMethodExecutionFilter(MethodExecutionFilter methodExecutionFilter) { | ||
this.methodExecutionFilter = methodExecutionFilter; | ||
return this; | ||
} | ||
|
||
public <T, R> OpelEngineBuilder withImplicitConversion(Class<T> from, Class<R> to, Function<T, R> conversion) { | ||
implicitConversion.register(new ImplicitConversionUnit<>(from, to, conversion)); | ||
return this; | ||
} | ||
|
||
public OpelEngine build() { | ||
EvalContext context = EvalContextBuilder.create() | ||
.withFunctions(embeddedFunctions) | ||
.withVariables(embeddedVariables) | ||
.build(); | ||
return new OpelEngine(methodExecutionFilter, implicitConversion, context); | ||
} | ||
} |
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.