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

Change checkParameters() error messages to always give name of function #3

Merged
merged 1 commit into from
Sep 5, 2019
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 @@ -34,8 +34,9 @@ public AbstractComparisonFunction(
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal) && !(param instanceof String))
Expand Down
44 changes: 25 additions & 19 deletions src/main/java/net/rptools/parser/function/AbstractFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public abstract class AbstractFunction implements Function {
private final int maxParameters;
private final boolean deterministic;

// Could use i18n instead, but would require additional dependency
private static final String WRONG_NUM_PARAM =
"Function '%s' requires exactly %d parameters; %d were provided.";
private static final String NOT_ENOUGH_PARAM =
"Function '%s' requires at least %d parameters; %d were provided.";
private static final String TOO_MANY_PARAM =
"Function '%s' requires no more than %d parameters; %d were provided.";

public AbstractFunction(String... aliases) {
this(0, UNLIMITED_PARAMETERS, aliases);
}
Expand All @@ -47,7 +55,7 @@ public final String[] getAliases() {

public final Object evaluate(Parser parser, String functionName, List<Object> parameters)
throws ParserException {
checkParameters(parameters);
checkParameters(functionName, parameters);

return childEvaluate(parser, functionName, parameters);
}
Expand All @@ -68,31 +76,29 @@ public final boolean isDeterministic() {
* Default implementation only checks count. Override this to implement more complex parameter
* checking.
*
* @param parameters
* @param functionName the name of the function
* @param parameters the list of parameters
*/
public void checkParameters(List<Object> parameters) throws ParameterException {
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
int pCount = parameters == null ? 0 : parameters.size();

if (pCount < minParameters
|| (maxParameters != UNLIMITED_PARAMETERS && parameters.size() > maxParameters))
throw new ParameterException(
String.format(
"Invalid number of parameters %d, expected %s",
pCount, formatExpectedParameterString()));
if (minParameters == maxParameters) {
if (pCount != maxParameters)
throw new ParameterException(
String.format(WRONG_NUM_PARAM, functionName, maxParameters, pCount));
} else {
if (pCount < minParameters)
throw new ParameterException(
String.format(NOT_ENOUGH_PARAM, functionName, minParameters, pCount));
if (maxParameters != UNLIMITED_PARAMETERS && pCount > maxParameters)
throw new ParameterException(
String.format(TOO_MANY_PARAM, functionName, maxParameters, pCount));
}
}

public void checkParameterTypes(List<Object> parameters, List<Class> allowedTypes) {}

private String formatExpectedParameterString() {
if (minParameters == maxParameters)
return String.format("exactly %d parameter(s)", maxParameters);

if (maxParameters == UNLIMITED_PARAMETERS)
return String.format("at least %d parameters", minParameters);

return String.format("between %d and %d parameters", minParameters, maxParameters);
}

protected boolean containsString(List<Object> parameters) {
for (Object param : parameters) {
if (param instanceof String) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public AbstractLogicalOperatorFunction(
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public AbstractNumberFunction(
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal))
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/rptools/parser/function/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public interface Function {
public Object evaluate(Parser parser, String functionName, List<Object> parameters)
throws ParserException;

public void checkParameters(List<Object> parameters) throws ParameterException;
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException;

public int getMinimumParameterCount();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/Addition.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal || param instanceof String))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/Equals.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal || param instanceof String))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof String))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/Mean.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/Median.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public int compare(Object o1, Object o2) {
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/NotEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal || param instanceof String))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/rptools/parser/function/impl/StrEquals.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof String))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public Object childEvaluate(Parser parser, String functionName, List<Object> par
}

@Override
public void checkParameters(List<Object> parameters) throws ParameterException {
super.checkParameters(parameters);
public void checkParameters(String functionName, List<Object> parameters)
throws ParameterException {
super.checkParameters(functionName, parameters);

for (Object param : parameters) {
if (!(param instanceof BigDecimal || param instanceof String))
Expand Down