Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpressionFunction.setParameters #697

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ default void checkParameterCount(final List<Object> parameters) {
}
}

/**
* Returns a {@link ExpressionFunction} with the given parameters.
*/
default ExpressionFunction<T, C> setParameters(final List<ExpressionFunctionParameter<?>> parameters) {
Objects.requireNonNull(parameters, "parameters");

return parameters.equals(
this.parameters(parameters.size())
) ?
this :
ExpressionFunctionParameterValuesParameters.with(
parameters,
this
);
}

/**
* The return type of this function
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import walkingkooka.tree.expression.ExpressionPurityContext;
import walkingkooka.tree.expression.FunctionExpressionName;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -48,11 +47,6 @@ public final Optional<FunctionExpressionName> name() {
return this.function.name();
}

@Override
public final List<ExpressionFunctionParameter<?>> parameters(final int count) {
return this.function.parameters(count);
}

@Override
public final Class<T> returnType() {
return this.function.returnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ private ExpressionFunctionParameterValuesFilter(final BiPredicate<Object, C> fil
this.filter = filter;
}

@Override
public List<ExpressionFunctionParameter<?>> parameters(final int count) {
return this.function.parameters(count);
}

@Override
public T apply(final List<Object> parameters,
final C context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ private ExpressionFunctionParameterValuesMapper(final BiFunction<List<Object>, C
this.mapper = mapper;
}

@Override
public List<ExpressionFunctionParameter<?>> parameters(final int count) {
return this.function.parameters(count);
}

@Override
public T apply(final List<Object> parameters,
final C context) {
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ default void testSetNameSame() {
);
}

@Test
default void testSetParametersNullFails() {
assertThrows(
NullPointerException.class,
() -> this.createBiFunction()
.setParameters(null)
);
}

@Test
default void testSetParametersSame() {
final F function = this.createBiFunction();
assertSame(
function,
function.setParameters(
function.parameters(0)
)
);
}

@Test
default void testParameterNamesUnique() {
final F function = this.createBiFunction();
Expand Down
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);
}
}
Loading