-
Notifications
You must be signed in to change notification settings - Fork 1
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 #697 from mP1/feature/ExpressionFunction-setParame…
…ters ExpressionFunction.setParameters
- Loading branch information
Showing
7 changed files
with
281 additions
and
6 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
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
72 changes: 72 additions & 0 deletions
72
...va/walkingkooka/tree/expression/function/ExpressionFunctionParameterValuesParameters.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,72 @@ | ||
/* | ||
* Copyright 2020 Miroslav Pokorny (github.com/mP1) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package walkingkooka.tree.expression.function; | ||
|
||
import walkingkooka.tree.expression.ExpressionEvaluationContext; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Wraps an {@link ExpressionFunction} and has {@link ExpressionFunctionParameter} definitions different from those of the wrapped {@link ExpressionFunction}. | ||
* This will result in a different preparation of parameter value before the wrapped {@link ExpressionFunction} is executed. | ||
*/ | ||
final class ExpressionFunctionParameterValuesParameters<T, C extends ExpressionEvaluationContext> extends ExpressionFunctionParameterValues<T, C> { | ||
|
||
static <T, C extends ExpressionEvaluationContext> ExpressionFunctionParameterValuesParameters<T, C> with(final List<ExpressionFunctionParameter<?>> parameters, | ||
final ExpressionFunction<T, C> function) { | ||
checkParameters(parameters); | ||
checkFunction(function); | ||
|
||
return new ExpressionFunctionParameterValuesParameters<>(parameters, function); | ||
} | ||
|
||
private static void checkParameters(final List<ExpressionFunctionParameter<?>> parameters) { | ||
Objects.requireNonNull(parameters, "parameters"); | ||
} | ||
|
||
private ExpressionFunctionParameterValuesParameters(final List<ExpressionFunctionParameter<?>> parameters, | ||
final ExpressionFunction<T, C> function) { | ||
super(function); | ||
this.parameters = parameters; | ||
} | ||
|
||
@Override | ||
public List<ExpressionFunctionParameter<?>> parameters(final int count) { | ||
return this.parameters; | ||
} | ||
|
||
@Override | ||
public T apply(final List<Object> parameters, | ||
final C context) { | ||
return this.function.apply( | ||
parameters, | ||
context | ||
); | ||
} | ||
|
||
/** | ||
* The different/custom parameters definitions that will be used to preprocess parameter values before the wrapped {@link #function} is executed. | ||
*/ | ||
private final List<ExpressionFunctionParameter<?>> parameters; | ||
|
||
@Override | ||
public String toString() { | ||
return this.function.toString(); | ||
} | ||
} |
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
163 changes: 163 additions & 0 deletions
163
...alkingkooka/tree/expression/function/ExpressionFunctionParameterValuesParametersTest.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,163 @@ | ||
/* | ||
* Copyright 2020 Miroslav Pokorny (github.com/mP1) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package walkingkooka.tree.expression.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import walkingkooka.Cast; | ||
import walkingkooka.collect.list.Lists; | ||
import walkingkooka.tree.expression.ExpressionEvaluationContext; | ||
import walkingkooka.tree.expression.ExpressionPurityContext; | ||
import walkingkooka.tree.expression.FunctionExpressionName; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public final class ExpressionFunctionParameterValuesParametersTest extends ExpressionFunctionTestCase<ExpressionFunctionParameterValuesParameters<Object, ExpressionEvaluationContext>, Object> { | ||
|
||
private final static List<ExpressionFunctionParameter<?>> PARAMETERS = Lists.of( | ||
ExpressionFunctionParameter.CHARACTER | ||
); | ||
|
||
private final static ExpressionFunction<Object, ExpressionEvaluationContext> FUNCTION = new FakeExpressionFunction<>() { | ||
|
||
@Override | ||
public Object apply(final List<Object> parameters, | ||
final ExpressionEvaluationContext context) { | ||
return parameters.stream() | ||
.map(p -> p.toString().toUpperCase()) | ||
.collect(Collectors.joining(",")); | ||
} | ||
|
||
@Override | ||
public boolean isPure(final ExpressionPurityContext context) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Optional<FunctionExpressionName> name() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public List<ExpressionFunctionParameter<?>> parameters(final int count) { | ||
return Lists.of( | ||
ExpressionFunctionParameterName.with("parameter1") | ||
.required(String.class) | ||
); | ||
} | ||
}; | ||
|
||
private final static Optional<FunctionExpressionName> NAME = Optional.of( | ||
FunctionExpressionName.with("custom-namedFunction") | ||
); | ||
|
||
@Test | ||
public void testWithNullParametersFails() { | ||
assertThrows( | ||
NullPointerException.class, | ||
() -> ExpressionFunctionParameterValuesParameters.with( | ||
null, | ||
FUNCTION | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void testWithNullFunctionFails() { | ||
assertThrows( | ||
NullPointerException.class, | ||
() -> ExpressionFunctionParameterValuesParameters.with( | ||
PARAMETERS, | ||
null | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void testApply() { | ||
this.applyAndCheck( | ||
Lists.of( | ||
"aaa", // toUpperCase() | ||
"bbb", | ||
3, | ||
"ddd" | ||
), | ||
"AAA,BBB,3,DDD" | ||
); | ||
} | ||
|
||
@Test | ||
public void testName() { | ||
this.checkEquals( | ||
NAME, | ||
this.createBiFunction().name() | ||
); | ||
} | ||
|
||
@Test | ||
public void testSetParametersSame() { | ||
final ExpressionFunctionParameterValuesParameters<Object, ExpressionEvaluationContext> function = this.createBiFunction(); | ||
|
||
assertSame( | ||
function, | ||
function.setParameters( | ||
PARAMETERS | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void testParameters() { | ||
assertSame( | ||
PARAMETERS, | ||
this.createBiFunction().parameters(0) | ||
); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
this.toStringAndCheck( | ||
this.createBiFunction(), | ||
FUNCTION.toString() | ||
); | ||
} | ||
|
||
// helpers.......................................................................................................... | ||
|
||
@Override | ||
public ExpressionFunctionParameterValuesParameters<Object, ExpressionEvaluationContext> createBiFunction() { | ||
return ExpressionFunctionParameterValuesParameters.with( | ||
PARAMETERS, | ||
FUNCTION | ||
); | ||
} | ||
|
||
@Override | ||
public int minimumParameterCount() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public Class<ExpressionFunctionParameterValuesParameters<Object, ExpressionEvaluationContext>> type() { | ||
return Cast.to(ExpressionFunctionParameterValuesParameters.class); | ||
} | ||
} |