/** * Verifies the input to be always true. * * @param input the value to validate. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @return null if input is true and a {@link List} otherwise. */ protected static List<RaveError> mustBeTrue(boolean input, ValidationContext validationContext) { return input ? Collections.<RaveError>emptyList() : createNewList(new RaveError(validationContext, RaveErrorStrings.MUST_BE_TRUE_ERROR)); }
private static List<RaveError> createIntDefError( int value, int[] acceptableValues, ValidationContext validationContext) { StringBuilder stringBuilder = new StringBuilder(); boolean first = true; for (int string : acceptableValues) { if (!first) { stringBuilder.append(" "); } else { stringBuilder.append("{"); } stringBuilder.append(string); first = false; } stringBuilder.append("}"); return createNewList( new RaveError(validationContext, value + " " + RaveErrorStrings.INT_DEF_ERROR + stringBuilder.toString())); }
private static List<RaveError> createLongDefError( long value, long[] acceptableValues, ValidationContext validationContext) { StringBuilder stringBuilder = new StringBuilder(); boolean first = true; for (long string : acceptableValues) { if (!first) { stringBuilder.append(" "); } else { stringBuilder.append("{"); } stringBuilder.append(string); first = false; } stringBuilder.append("}"); return createNewList(new RaveError( validationContext, String.format("%s %s %s", value, RaveErrorStrings.LONG_DEF_ERROR, stringBuilder.toString()))); }
/** * Checks the input long value is within the bounds of {@code from} and {@code to} inclusive. * * @param validationContext the {@link BaseValidator.ValidationContext} * @param value the value to check. * @param from the lower bound of the check, inclusive. * @param to the upper bound of the check, inclusive. * @return the {@link RaveError} if the checked value is not within the bounds. */ protected static List<RaveError> checkIntRange( ValidationContext validationContext, long value, long from, long to) { if (value <= to && value >= from) { return Collections.<RaveError>emptyList(); } return createNewList(new RaveError(validationContext, value + " " + RaveErrorStrings.INT_RANGE_ERROR + " which " + "should be between " + from + " and " + to)); }
/** * Check a double value to be in the correct range. {@code from} and {@code to} are used as the boundry checks and * are inclusive. Note {@link Double#POSITIVE_INFINITY} or {@link Double#NEGATIVE_INFINITY} are supported. * If used any value is always greater than {@link Double#NEGATIVE_INFINITY} and always less than * {@link Double#POSITIVE_INFINITY} by definition. * * @param validationContext the {@link BaseValidator.ValidationContext} * @param value the value to check * @param from the lower bound of the check (inclusive) * @param to the upper bound of the check (inclusive) * @return the {@link RaveError} if the checked value is not within the bounds. */ protected static List<RaveError> checkFloatRange( ValidationContext validationContext, double value, double from, double to) { boolean lowerIsOk = (from == Double.NEGATIVE_INFINITY) || value >= from; boolean upperIsOk = (to == Double.POSITIVE_INFINITY) || value <= to; if (lowerIsOk && upperIsOk) { return Collections.<RaveError>emptyList(); } return createNewList(new RaveError(validationContext, value + " " + RaveErrorStrings.FLOAT_RANGE_ERROR + " which should be between " + from + " and " + to)); }
/** * Create the error for stringdef. * * @param value the string value * @param acceptableValues the acceptable values for the string. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @return a {@link RaveError} corresponding to the non matching error. */ private static List<RaveError> createStringDefError( @Nullable String value, String[] acceptableValues, ValidationContext validationContext) { StringBuilder stringBuilder = new StringBuilder(); boolean first = true; for (String string : acceptableValues) { if (!first) { stringBuilder.append(" "); } else { stringBuilder.append("{"); } stringBuilder.append(string); first = false; } stringBuilder.append("}"); return createNewList( new RaveError(validationContext, value + " " + RaveErrorStrings.STRING_DEF_ERROR + stringBuilder.toString())); }
/** * Merge a msg with a {@link List}s of {@link RaveError}s. * * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param msg the message to add to the list. * @param raveErrors the {@link List} of {@link RaveError} to add to. * @return a new {@link List} or {@link RaveError}s */ private static List<RaveError> appendError( ValidationContext validationContext, String msg, @Nullable List<RaveError> raveErrors) { // If the list is empty it is also immutable so we need a new one. if (raveErrors == null || raveErrors.isEmpty()) { return createNewList(new RaveError(validationContext, msg)); } raveErrors.add(new RaveError(validationContext, msg)); return raveErrors; }