protected void checkVariableArityArguments(List<FieldValue> arguments, int minArity){ if(arguments.size() < minArity){ throw new FunctionException(this, "Expected " + minArity + " or more values, got " + arguments.size() + " values"); } }
public FunctionException(Function function, String message){ super(message); setFunction(function); }
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); ZonedDateTime zonedDateTime = getRequiredArgument(arguments, 0, "input").asZonedDateTime(ZoneId.systemDefault()); Date date = Date.from(zonedDateTime.toInstant()); String pattern = translatePattern(getRequiredArgument(arguments, 1, "pattern").asString()); String result; try { result = String.format(pattern, date); } catch(IllegalFormatException ife){ throw new FunctionException(this, "Invalid \'pattern\' value") .initCause(ife); } return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); }
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); Number number = getRequiredArgument(arguments, 0, "input").asNumber(); String pattern = getRequiredArgument(arguments, 1, "pattern").asString(); String result; // According to the java.util.Formatter javadoc, Java formatting is more strict than C's printf formatting. // For example, in Java, if a conversion is incompatible with a flag, an exception will be thrown. In C's printf, inapplicable flags are silently ignored. try { result = String.format(pattern, number); } catch(IllegalFormatException ife){ throw new FunctionException(this, "Invalid \'pattern\' value") .initCause(ife); } return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } };
protected void checkFixedArityArguments(List<FieldValue> arguments, int arity){ if(arguments.size() != arity){ throw new FunctionException(this, "Expected " + arity + " values, got " + arguments.size() + " values"); } }
protected void checkVariableArityArguments(List<FieldValue> arguments, int minArity, int maxArity){ if(arguments.size() < minArity || arguments.size() > maxArity){ throw new FunctionException(this, "Expected " + minArity + " to " + maxArity + " values, got " + arguments.size() + " values"); } }
protected FieldValue getRequiredArgument(List<FieldValue> arguments, int index, String alias){ FieldValue argument = arguments.get(index); if(Objects.equals(FieldValues.MISSING_VALUE, argument)){ if(alias != null){ throw new FunctionException(this, "Missing \'" + alias + "\' value at position " + index); } else { throw new FunctionException(this, "Missing value at position " + index); } } return checkArgument(argument, index, alias); }
@Override public FieldValue evaluate(List<FieldValue> arguments){ if(arguments.size() < 1 || arguments.size() > 2){ throw new FunctionException(this, "Expected 1 or 2 arguments, but got " + arguments.size() + " arguments"); } // End if if(arguments.contains(null)){ throw new FunctionException(this, "Missing arguments"); } Collection<?> values = FieldValueUtil.getValue(Collection.class, arguments.get(0)); Boolean biasCorrected = Boolean.FALSE; if(arguments.size() > 1){ biasCorrected = (arguments.get(1)).asBoolean(); } Double result = evaluate(values, biasCorrected); return FieldValueUtil.create(result); }
@Override protected FieldValue checkArgument(FieldValue argument, int index, String alias){ if(Objects.equals(FieldValues.MISSING_VALUE, argument)){ return argument; } DataType dataType = argument.getDataType(); switch(dataType){ case INTEGER: case FLOAT: case DOUBLE: break; default: if(alias != null){ throw new FunctionException(this, "Expected a numeric \'" + alias + "\' value at position " + index + ", got " + dataType.value() + " value"); } else { throw new FunctionException(this, "Expected a numeric value at position " + index + ", got " + dataType.value() + " value"); } } return argument; } }
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 3); String string = getRequiredArgument(arguments, 0, "input").asString(); int position = getRequiredArgument(arguments, 1, "startPos").asInteger(); if(position < 1){ throw new FunctionException(this, "Invalid \'startPos\' value " + position + ". Must be equal or greater than 1"); } // "The first character of a string is located at position 1 (not position 0)" int javaPosition = Math.min(position - 1, string.length()); int length = getRequiredArgument(arguments, 2, "length").asInteger(); if(length < 0){ throw new FunctionException(this, "Invalid \'length\' value " + length); } int javaLength = Math.min(length, (string.length() - javaPosition)); // This expression must never throw a StringIndexOutOfBoundsException String result = string.substring(javaPosition, javaPosition + javaLength); return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, result); } };
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkArguments(arguments, 2); Collection<?> values = FieldValueUtil.getValue(Collection.class, arguments.get(0)); int percentile = (arguments.get(1)).asInteger(); if(percentile < 1 || percentile > 100){ throw new FunctionException(this, "Invalid percentile value " + percentile + ". Must be greater than 0 and equal or less than 100"); } Double result = evaluate(values, percentile); return FieldValueUtil.create(result); }
throw new FunctionException(this, "Invalid \'length\' value " + length);
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); Collection<?> values = FieldValueUtil.getValue(Collection.class, getRequiredArgument(arguments, 0, "values")); int percentile = getRequiredArgument(arguments, 1, "percentile").asInteger(); if(percentile < 1 || percentile > 100){ throw new FunctionException(this, "Invalid \'percentile\' value " + percentile + ". Must be greater than 0 and equal or less than 100"); } Double result = evaluate(values, percentile); return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result); }
@Override public FieldValue evaluate(List<FieldValue> arguments){ checkFixedArityArguments(arguments, 2); Collection<?> values = FieldValueUtil.getValue(Collection.class, getRequiredArgument(arguments, 0, "values")); int percentile = getRequiredArgument(arguments, 1, "percentile").asInteger(); if(percentile < 1 || percentile > 100){ throw new FunctionException(this, "Invalid \'percentile\' value " + percentile + ". Must be greater than 0 and equal or less than 100"); } Double result = evaluate(values, percentile); return FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, result); }