Skip to content

Commit

Permalink
ExpressionFunctionParameter.setType
Browse files Browse the repository at this point in the history
  • Loading branch information
mP1 committed Dec 6, 2023
1 parent 6ebc46b commit c13781b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static <T> ExpressionFunctionParameter<T> with(final ExpressionFunctionPa
final ExpressionFunctionParameterCardinality cardinality,
final Set<ExpressionFunctionParameterKind> kinds) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(type, "type");
checkType(type);
Objects.requireNonNull(typeParameters, "typeParameters");
Objects.requireNonNull(cardinality, "cardinality");
Objects.requireNonNull(kinds, "kinds");
Expand All @@ -102,6 +102,10 @@ public static <T> ExpressionFunctionParameter<T> with(final ExpressionFunctionPa
);
}

private static <T> Class<T> checkType(final Class<T> type) {
return Objects.requireNonNull(type, "type");
}

private ExpressionFunctionParameter(final ExpressionFunctionParameterName name,
final Class<T> type,
final ExpressionFunctionParameterCardinality cardinality,
Expand Down Expand Up @@ -142,6 +146,25 @@ public Class<T> type() {
return this.type;
}

/**
* Would be setter that returns a {@link ExpressionFunctionParameter} with the new type but copying all other properties.
* This is particularly useful when updating a {@link ExpressionFunction} parameters such as COUNT which initially has its parameters declared but Object but within a
* spreadsheet it needs all parameter values converted to a number.
*/
public <TT> ExpressionFunctionParameter<TT> setType(final Class<TT> type) {
checkType(type);

return this.type.equals(type) ?
Cast.to(this) :
new ExpressionFunctionParameter<>(
this.name,
type,
this.cardinality,
this.typeParameters,
this.kinds
);
}

private final Class<T> type;

public List<Class<?>> typeParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,46 @@ public void testSetNameWithDifferent() {
this.checkKinds(parameter);
}

// setType.........................................................................................................

@Test
public void testSetTypeWithNullFails() {
assertThrows(
NullPointerException.class,
() -> this.createObject().setType(null)
);
}

@Test
public void testSetTypeWithSame() {
final ExpressionFunctionParameter<?> parameter = this.createObject();
assertSame(
parameter,
parameter.setType(TYPE)
);
}

@Test
public void testSetTypeWithDifferent() {
final ExpressionFunctionParameter<?> parameter = this.createObject();
final Class<ExpressionFunctionParameterTest> type = ExpressionFunctionParameterTest.class;

final ExpressionFunctionParameter<?> different = parameter.setType(type);
assertNotSame(parameter, different);

this.checkName(different);
this.checkType(different, type);
this.checkTypeParameters(different);
this.checkCardinality(different);
this.checkKinds(different);

this.checkName(parameter);
this.checkType(parameter);
this.checkTypeParameters(parameter);
this.checkCardinality(parameter);
this.checkKinds(parameter);
}

// setCardinality...................................................................................................

@Test
Expand Down

0 comments on commit c13781b

Please sign in to comment.