Skip to content

Commit

Permalink
Merge pull request #687 from mP1/feature/ExpressionFunction-mapParame…
Browse files Browse the repository at this point in the history
…terValues-was-mapParameter

ExpressionFunction.mapParameterValues was mapParameter
  • Loading branch information
mP1 authored Dec 6, 2023
2 parents cda3c54 + ed71fc5 commit fcebff1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ default void checkParameterCount(final List<Object> parameters) {
Class<T> returnType();

/**
* Returns a new {@link ExpressionFunction} that adds the parameter mapper before this namedFunction.
* Returns a new {@link ExpressionFunction} that adds the parameter mapper before this {@link ExpressionFunction#apply(Object, Object)}
* is called, but after parameter values are prepared.
*/
default ExpressionFunction<T, C> mapParameters(final BiFunction<List<Object>, C, List<Object>> mapper) {
return ExpressionFunctionParametersMapper.with(mapper, this);
default ExpressionFunction<T, C> mapParameterValues(final BiFunction<List<Object>, C, List<Object>> mapper) {
return ExpressionFunctionParameterValuesMapper.with(mapper, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@
/**
* Wraps an {@link ExpressionFunction} and applies a {@link java.util.function.Function} on the parameters before calling the wrapped {@link ExpressionFunction}.
*/
final class ExpressionFunctionParametersMapper<T, C extends ExpressionEvaluationContext> extends ExpressionFunctionParameters<T, C> {
final class ExpressionFunctionParameterValuesMapper<T, C extends ExpressionEvaluationContext> extends ExpressionFunctionParameters<T, C> {

static <T, C extends ExpressionEvaluationContext> ExpressionFunctionParametersMapper<T, C> with(final BiFunction<List<Object>, C, List<Object>> mapper,
final ExpressionFunction<T, C> function) {
static <T, C extends ExpressionEvaluationContext> ExpressionFunctionParameterValuesMapper<T, C> with(final BiFunction<List<Object>, C, List<Object>> mapper,
final ExpressionFunction<T, C> function) {
Objects.requireNonNull(mapper, "mapper");
checkFunction(function);

return new ExpressionFunctionParametersMapper<>(mapper, function);
return new ExpressionFunctionParameterValuesMapper<>(mapper, function);
}

private ExpressionFunctionParametersMapper(final BiFunction<List<Object>, C, List<Object>> mapper,
final ExpressionFunction<T, C> function) {
private ExpressionFunctionParameterValuesMapper(final BiFunction<List<Object>, C, List<Object>> mapper,
final ExpressionFunction<T, C> function) {
super(function);
this.mapper = mapper;
}
Expand All @@ -58,10 +58,10 @@ public T apply(final List<Object> parameters,
* Special cases that handles if the new mapper is equal to the current.
*/
@Override
public ExpressionFunction<T, C> mapParameters(final BiFunction<List<Object>, C, List<Object>> mapper) {
public ExpressionFunction<T, C> mapParameterValues(final BiFunction<List<Object>, C, List<Object>> mapper) {
return this.mapper.equals(mapper) ?
this :
ExpressionFunctionParametersMapper.with(mapper, this);
ExpressionFunctionParameterValuesMapper.with(mapper, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ default void testParametersFlattenMustBeVariable() {
}

@Test
default void testMapParametersWithNullFunction() {
default void testMapParameterValuesWithNullFunction() {
assertThrows(
NullPointerException.class,
() -> this.createBiFunction()
.mapParameters(null)
.mapParameterValues(null)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

public final class ExpressionFunctionParametersMapperTest extends ExpressionFunctionTestCase<ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext>, Object> {
public final class ExpressionFunctionParameterValuesMapperTest extends ExpressionFunctionTestCase<ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext>, Object> {

private final static BiFunction<List<Object>, ExpressionEvaluationContext, List<Object>> MAPPER = (p, c) -> p.stream()
.map(pp -> pp.toString().toUpperCase())
Expand Down Expand Up @@ -74,12 +74,12 @@ public List<ExpressionFunctionParameter<?>> parameters(final int count) {

@Test
public void testWithNullMapperFails() {
assertThrows(NullPointerException.class, () -> ExpressionFunctionParametersMapper.with(null, FUNCTION));
assertThrows(NullPointerException.class, () -> ExpressionFunctionParameterValuesMapper.with(null, FUNCTION));
}

@Test
public void testWithNullFunctionFails() {
assertThrows(NullPointerException.class, () -> ExpressionFunctionParametersMapper.with(MAPPER, null));
assertThrows(NullPointerException.class, () -> ExpressionFunctionParameterValuesMapper.with(MAPPER, null));
}

@Test
Expand All @@ -96,18 +96,18 @@ public void testName() {

@Test
public void testMapSameFunction() {
final ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext> function = this.createBiFunction();
assertSame(function, function.mapParameters(MAPPER));
final ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext> function = this.createBiFunction();
assertSame(function, function.mapParameterValues(MAPPER));
}

@Test
public void testMapFunctionThenApply() {
final ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext> function = this.createBiFunction();
final ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext> function = this.createBiFunction();

final BiFunction<List<Object>, ExpressionEvaluationContext, List<Object>> mapper = (p, c) -> p.stream()
.map(pp -> pp.toString() + "-a")
.collect(Collectors.toList());
final ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext> function2 = Cast.to(function.mapParameters(mapper));
final ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext> function2 = Cast.to(function.mapParameterValues(mapper));
assertNotSame(function, function2);

this.applyAndCheck(function2,
Expand All @@ -124,8 +124,8 @@ public void testToString() {
// helpers..........................................................................................................

@Override
public ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext> createBiFunction() {
return ExpressionFunctionParametersMapper.with(MAPPER, FUNCTION);
public ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext> createBiFunction() {
return ExpressionFunctionParameterValuesMapper.with(MAPPER, FUNCTION);
}

@Override
Expand All @@ -134,7 +134,7 @@ public int minimumParameterCount() {
}

@Override
public Class<ExpressionFunctionParametersMapper<Object, ExpressionEvaluationContext>> type() {
return Cast.to(ExpressionFunctionParametersMapper.class);
public Class<ExpressionFunctionParameterValuesMapper<Object, ExpressionEvaluationContext>> type() {
return Cast.to(ExpressionFunctionParameterValuesMapper.class);
}
}

0 comments on commit fcebff1

Please sign in to comment.