Skip to content

Commit

Permalink
Merge pull request #731 from mP1/feature/ExpressionEvaluationContext-…
Browse files Browse the repository at this point in the history
…expressionFunction-returns-Optional-was-function

ExpressionEvaluationContext.expressionFunction returns Optional was f…
  • Loading branch information
mP1 authored May 28, 2024
2 parents 615a0c7 + c58f480 commit eb70368
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class BasicExpressionEvaluationContext implements ExpressionEvaluationCont
* Factory that creates a {@link BasicExpressionEvaluationContext}
*/
static BasicExpressionEvaluationContext with(final ExpressionNumberKind expressionNumberKind,
final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions,
final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions,
final Function<RuntimeException, Object> exceptionHandler,
final Function<ExpressionReference, Optional<Optional<Object>>> references,
final Function<ExpressionReference, ExpressionEvaluationException> referenceNotFound,
Expand Down Expand Up @@ -73,7 +73,7 @@ static BasicExpressionEvaluationContext with(final ExpressionNumberKind expressi
* Private ctor use factory
*/
private BasicExpressionEvaluationContext(final ExpressionNumberKind expressionNumberKind,
final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions,
final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions,
final Function<RuntimeException, Object> exceptionHandler,
final Function<ExpressionReference, Optional<Optional<Object>>> references,
final Function<ExpressionReference, ExpressionEvaluationException> referenceNotFound,
Expand Down Expand Up @@ -243,15 +243,17 @@ public ExpressionEvaluationContext context(final Function<ExpressionReference, O
}

@Override
public ExpressionFunction<?, ExpressionEvaluationContext> function(final FunctionExpressionName name) {
public Optional<ExpressionFunction<?, ExpressionEvaluationContext>> expressionFunction(final FunctionExpressionName name) {
return this.functions.apply(name);
}

private final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions;
private final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions;

@Override
public boolean isPure(final FunctionExpressionName name) {
return this.function(name).isPure(this);
return this.expressionFunction(name)
.map(f -> f.isPure(this))
.orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public ExpressionEvaluationContext context(final Function<ExpressionReference, O
}

@Override
public ExpressionFunction<?, ExpressionEvaluationContext> function(final FunctionExpressionName name) {
return this.context.function(name);
public Optional<ExpressionFunction<?, ExpressionEvaluationContext>> expressionFunction(final FunctionExpressionName name) {
return this.context.expressionFunction(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import walkingkooka.tree.expression.function.ExpressionFunctionParameter;
import walkingkooka.tree.expression.function.ExpressionFunctionParameterKind;
import walkingkooka.tree.expression.function.ExpressionFunctions;
import walkingkooka.tree.expression.function.UnknownExpressionFunctionException;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -69,7 +70,16 @@ default Object evaluateIfNecessary(final Object value) {
/**
* Returns the {@link ExpressionFunction} with the given {@link FunctionExpressionName}.
*/
ExpressionFunction<?, ExpressionEvaluationContext> function(final FunctionExpressionName name);
Optional<ExpressionFunction<?, ExpressionEvaluationContext>> expressionFunction(final FunctionExpressionName name);

/**
* Helper that invokes {@link #expressionFunction(FunctionExpressionName)} and throws a {@link UnknownExpressionFunctionException}
* if none was found.
*/
default ExpressionFunction<?, ExpressionEvaluationContext> expressionFunctionOrFail(final FunctionExpressionName name) {
return this.expressionFunction(name)
.orElseThrow(() -> new UnknownExpressionFunctionException(name));
}

/**
* Creates a lambda {@link ExpressionFunction}, the given parameters become scoped variables when the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ default void toValueAndCheck(final Expression node, final ExpressionEvaluationCo
}

@Test
default void testFunctionNullFunctionNameFails() {
default void testExpressionFunctionNullFunctionNameFails() {
assertThrows(
NullPointerException.class,
() -> this.createContext()
.function(null)
.expressionFunctionOrFail(null)
);
}

@Test
default void testFunctionUnknownFunctionNameFails() {
default void testExpressionFunctionUnknownFunctionNameFails() {
assertThrows(
UnknownExpressionFunctionException.class,
() -> this.createContext()
.function(this.unknownFunctionName())
.expressionFunctionOrFail(this.unknownFunctionName())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class ExpressionEvaluationContexts implements PublicStaticHelper {
* {@see BasicExpressionEvaluationContext}
*/
public static ExpressionEvaluationContext basic(final ExpressionNumberKind expressionNumberKind,
final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions,
final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions,
final Function<RuntimeException, Object> exceptionHandler,
final Function<ExpressionReference, Optional<Optional<Object>>> references,
final Function<ExpressionReference, ExpressionEvaluationException> referenceNotFound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ExpressionEvaluationContext context(final Function<ExpressionReference, O
}

@Override
public ExpressionFunction<?, ExpressionEvaluationContext> function(final FunctionExpressionName name) {
public Optional<ExpressionFunction<?, ExpressionEvaluationContext>> expressionFunction(final FunctionExpressionName name) {
Objects.requireNonNull(name, "name");
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void accept(final ExpressionVisitor visitor) {

@Override
ExpressionFunction<?, ExpressionEvaluationContext> function(final ExpressionEvaluationContext context) {
return context.function(this.value);
return context.expressionFunctionOrFail(this.value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public ExpressionEvaluationContext context(final Function<ExpressionReference, O
}

@Override
public ExpressionFunction<?, ExpressionEvaluationContext> function(final FunctionExpressionName name) {
return this.context.function(name);
public Optional<ExpressionFunction<?, ExpressionEvaluationContext>> expressionFunction(final FunctionExpressionName name) {
return this.context.expressionFunction(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public void testCaseSensitivity2() {

@Test
public void testToString() {
final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions = this.functions();
final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions = this.functions();
final Function<ExpressionReference, Optional<Optional<Object>>> references = this.references();
final Function<ExpressionReference, ExpressionEvaluationException> referenceNotFound = ExpressionEvaluationContexts.referenceNotFound();
final ConverterContext converterContext = this.converterContext();
Expand Down Expand Up @@ -595,7 +595,7 @@ private BasicExpressionEvaluationContext createContext(final boolean pure,
);
}

private BasicExpressionEvaluationContext createContext(final Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions,
private BasicExpressionEvaluationContext createContext(final Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions,
final Function<RuntimeException, Object> exceptionHandler) {
return BasicExpressionEvaluationContext.with(
KIND,
Expand All @@ -608,40 +608,42 @@ private BasicExpressionEvaluationContext createContext(final Function<FunctionEx
);
}

private Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions() {
private Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions() {
return this.functions(true);
}

private Function<FunctionExpressionName, ExpressionFunction<?, ExpressionEvaluationContext>> functions(final boolean pure) {
private Function<FunctionExpressionName, Optional<ExpressionFunction<?, ExpressionEvaluationContext>>> functions(final boolean pure) {
return (functionName) -> {
Objects.requireNonNull(functionName, "functionName");

if (false == this.functionName().equals(functionName)) {
throw new UnknownExpressionFunctionException(functionName);
}

return new FakeExpressionFunction<>() {
@Override
public Object apply(final List<Object> parameters,
final ExpressionEvaluationContext context) {
Objects.requireNonNull(parameters, "parameters");
Objects.requireNonNull(context, "context");

return BasicExpressionEvaluationContextTest.this.functionValue();
}

@Override
public List<ExpressionFunctionParameter<?>> parameters(final int count) {
return Lists.of(
ExpressionFunctionParameterName.VALUE.required(Object.class)
);
}

@Override
public boolean isPure(final ExpressionPurityContext context) {
return pure;
}
};
return Optional.of(
new FakeExpressionFunction<>() {
@Override
public Object apply(final List<Object> parameters,
final ExpressionEvaluationContext context) {
Objects.requireNonNull(parameters, "parameters");
Objects.requireNonNull(context, "context");

return BasicExpressionEvaluationContextTest.this.functionValue();
}

@Override
public List<ExpressionFunctionParameter<?>> parameters(final int count) {
return Lists.of(
ExpressionFunctionParameterName.VALUE.required(Object.class)
);
}

@Override
public boolean isPure(final ExpressionPurityContext context) {
return pure;
}
}
);
};
}

Expand Down
Loading

0 comments on commit eb70368

Please sign in to comment.