net.officefloor.eclipse.configurer.ValueValidator Maven / Gradle / Ivy
/*
* OfficeFloor - http://www.officefloor.net
* Copyright (C) 2005-2018 Daniel Sagenschneider
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package net.officefloor.eclipse.configurer;
import javafx.beans.property.ReadOnlyProperty;
/**
* Validates the value.
*
* @author Daniel Sagenschneider
*/
public interface ValueValidator {
/**
* Convenience {@link ValueValidator} for ensuring not null.
*
* @param
* Model type.
* @param
* Value type.
* @param errorMessage
* Error message if null.
* @return {@link ValueValidator} to validate not null.
*/
public static ValueValidator notNull(String errorMessage) {
return (context) -> {
Object value = context.getValue().getValue();
notNull(value, errorMessage, context);
};
}
/**
* Convenience method to provide error if value is null.
*
* @param value
* Value to check for null.
* @param errorMessage
* Error message if empty string.
* @param context
* {@link ValueValidatorContext}.
*/
public static void notNull(Object value, String errorMessage, ValueValidatorContext, ?> context) {
if (value == null) {
context.setError(errorMessage);
}
}
/**
* Convenience {@link ValueValidator} for ensuring not an empty {@link String}.
*
* @param
* Model type.
* @param errorMessage
* Error message if empty {@link String}.
* @return {@link ValueValidator} to validate not an empty {@link String}.
*/
public static ValueValidator notEmptyString(String errorMessage) {
return (context) -> {
String value = context.getValue().getValue();
notEmptyString(value, errorMessage, context);
};
}
/**
* Convenience method to provide error if value is empty string.
*
* @param value
* Value to check for empty string.
* @param errorMessage
* Error message if empty string.
* @param context
* {@link ValueValidatorContext}.
*/
public static void notEmptyString(String value, String errorMessage, ValueValidatorContext, ?> context) {
if ((value == null) || (value.trim().length() == 0)) {
context.setError(errorMessage);
}
}
/**
* Undertakes the validation.
*
* @param context
* {@link ValueValidatorContext}.
* @throws Exception
* If failure in validation. Message of {@link Exception} is used as
* error.
*/
void validate(ValueValidatorContext context) throws Exception;
/**
* Context for the {@link ValueValidator}.
*/
public interface ValueValidatorContext {
/**
* Obtains the model.
*
* @return Model.
*/
M getModel();
/**
* Obtains the value.
*
* @return Value.
*/
ReadOnlyProperty getValue();
/**
* Specifies an error.
*
* @param message
* Message.
*/
void setError(String message);
/**
*
* Triggers reloading the value from the model for the particular
* {@link Builder}.
*
* This allows validation to update the model and reload values from the model.
*
* @param builder
* {@link Builder} to identify the value to reload.
*/
void reload(Builder, ?, ?> builder);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy