public code v1

This commit is contained in:
2026-05-22 11:14:29 +02:00
parent 427197ec5a
commit b8141736eb
28859 changed files with 575079 additions and 0 deletions
@@ -0,0 +1,222 @@
package afryca.valueforcer;
import org.eclipse.osgi.util.NLS;
import afryca.valueforcer.l10n.Messages;
/**
* Force a value condition
*
* @author Sinbad²
*
* @version 3.0
*/
public class ValueForcer {
private ValueForcer() {
}
/**
* Not null value
*
* @param value
* Value to test
*
* @throws NullPointerException
* if value is null
*/
public static void notNull(Object value) {
if (value == null) {
throw new NullPointerException(Messages.not_null);
}
}
/**
* Not negative value
*
* @param number
* number
*
* @throws NullPointerException
* if number is null
* @throws IllegalArgumentException
* if number is negative
*/
public static void notNegative(Number number) {
try {
notLowerThan(number, 0);
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException(NLS.bind(Messages.negative_value, number));
}
}
/**
* Not empty value
*
* @param array
* Object[]
*
* @throws NullPointerException
* if array is empty
*/
public static void notEmpty(Object[] array) {
if (isEmpty(array)) {
throw new IllegalArgumentException(Messages.empty_value);
}
}
public static boolean isEmpty(Object[] parameter) {
notNull(parameter);
return (parameter.length == 0);
}
/**
* Value not lower than limit
*
* @param number
* Number
* @param limit
* Limit
*
* @throws NullPointerException
* if number is null
* @throws NullPointerException
* if limit is null
* @throws IllegalArgumentException
* if value is lower than limit
*/
public static void notLowerThan(Number number, Number limit) {
notNull(number);
notNull(limit);
if (number.doubleValue() < limit.doubleValue()) {
throw new IllegalArgumentException(NLS.bind(Messages.is_lower_than, number, limit));
}
}
/**
* Value not greater than limit
*
* @param number
* Number
* @param limit
* Limit
*
* @throws NullPointerException
* if number is null
* @throws NullPointerException
* if limit is null
* @throws IllegalArgumentException
* if value is greater than limit
*/
public static void notGreaterThan(Number number, Number limit) {
try {
notLowerThan(limit, number);
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException(NLS.bind(Messages.is_greater_than, number, limit));
}
}
/**
* Value not out of range [lowerLimit, upperLimit]
*
* @param number
* number
* @param lowerLimit
* Lower limit
* @param upperLimit
* Upper limit
*
* @throws NullPointerException
* if number is null
* @throws NullPointerException
* if lowerLimit is null
* @throws NullPointerException
* if upperLimit is null
* @throws IllegalArgumentException
* if upperLimit is lower than lowerLimit
* @throws IllegalArgumentException
* if number is lower than lowerLimit
* @throws IllegalArgumentException
* if number is greater than upperLimit
*/
public static void notOutOfRange(Number number, Number lowerLimit, Number upperLimit) {
notNull(number);
notNull(lowerLimit);
notNull(upperLimit);
notLowerThan(upperLimit, lowerLimit);
try {
notLowerThan(number, lowerLimit);
notLowerThan(upperLimit, number);
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException(
NLS.bind(Messages.is_out_of_range, new Object[] { number, lowerLimit, upperLimit }));
}
}
/**
* Invalid array size
*
* @param length
* Array length
* @param i
* Valid value
* @param j
* Valid value
* @throws IllegalArgumentException
* The size is not allowed
*/
public static void notInvalidSizeArray(Number length, Number i, Number j) {
if(length != i && length != j) {
throw new IllegalArgumentException(
NLS.bind(Messages.is_out_of_range, new Object[] { length, i, j }));
}
}
public static boolean inRange(double value, double min, double max) {
if (min > max) {
throw new IllegalArgumentException(Messages.is_out_of_range);
}
return ((min <= value) && (value <= max));
}
public static void notIllegalElementType(Object object, String[] types) {
if (!validElementType(object, types)) {
throw new IllegalArgumentException(Messages.illegal_type);
}
}
/**
* Comprueba que un elemento sea de alguno de los tipos indicados.
*
* @param object
* Objeto que debe ser del tipo indicado.
* @param types
* Tipos permitidos.
* @return True si el elemento es de alguno de los tipos indicados, False en
* caso contrario.
* @throw IllegalArgumentException Si object es null o,
* <p>
* si types está vacío o
* <p>
* si types es null.
*/
public static boolean validElementType(Object object, String[] types) {
notEmpty(types);
notNull(object);
String objectClass = object.getClass().toString();
for (String type : types) {
if (objectClass.equals(type)) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,30 @@
package afryca.valueforcer.l10n;
import org.eclipse.osgi.util.NLS;
/**
* Localization messages
*
* @author Sinbad²
* @version 3.0
*/
public class Messages extends NLS {
private static final String BUNDLE_NAME = "afryca.valueforcer.l10n.messages"; //$NON-NLS-1$
public static String is_lower_than;
public static String is_greater_than;
public static String is_out_of_range;
public static String negative_value;
public static String empty_value;
public static String not_null;
public static String illegal_type;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}
private Messages() {
}
}
@@ -0,0 +1,7 @@
is_greater_than={0} is greater than {1}
is_lower_than={0} is lower than {1}
is_out_of_range={0} is out of range [{1},{2}]
negative_value={0} has a negative value
not_null=Not allowed null value
empty_value=Not allowed empty value
illegal_type=Illegal type
@@ -0,0 +1,7 @@
is_greater_than={0} es mayor que {1}
is_lower_than={0} es menor que {1}
is_out_of_range={0} está fuera del rango [{1},{2}]
negative_value={0} tiene un valor negativo
not_null=Valores nulos no permitidos
empty_value=Valores vacíos no permitidos
illegal_type=Tipo no permitido