Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package org.xillium.data.validation;
import org.xillium.base.Trace;
import org.xillium.data.DataObject;
import java.lang.reflect.*;
import java.util.*;
/**
* A Reifier of extended types for data reification and validation.
*/
public class Reifier {
protected static final Map, Map> _cached = new HashMap, Map>();
public static synchronized Validator cache(Class> type, String name, Validator validator) {
Map validators = _cached.get(type);
if (validators == null) {
_cached.put(type, validators = new HashMap());
}
validators.put(name, validator);
return validator;
}
public static synchronized Validator find(Class> type, String name) {
Map validators = _cached.get(type);
return validators != null ? validators.get(name) : null;
}
Map _named = new HashMap();
/**
* Adds a set of data type specifications.
* @param spec - a class that defines data types as member fields
*/
public Reifier addTypeSet(Class> spec) {
for (Field field: spec.getDeclaredFields()) {
if (Modifier.isPublic(field.getModifiers())) {
String name = field.getName();
try {
_named.put(name, new Validator(name, field.getType(), field));
} catch (IllegalArgumentException x) {
Trace.g.std.note(Reifier.class, "Ignored " + name + ": " + x.getMessage());
}
} else {
Trace.g.std.note(Reifier.class, "Ignored non-public field: " + field.getName());
}
}
return this;
}
public Map getValidators() {
return _named;
}
/**
* Populates a data object by collecting and validating the named values from a Map<String, String>.
*
* @throws EmptyDataObjectException if a required member is missing and the data object is empty
* @throws MissingParameterException if a required member is missing but the data object has other members
* @throws DataValidationException if all other data validation fails
* @throws SecurityException if the data object is inproperly designed
*/
public T collect(T data, Map binder) throws SecurityException, DataValidationException {
return collect(data, binder, null);
}
private final T collect(T data, Map binder, String prefix) throws SecurityException, DataValidationException {
int present = 0;
String absent = null;
for (Field field: data.getClass().getFields()) {
if (Modifier.isStatic(field.getModifiers()) || Modifier.isTransient(field.getModifiers())) continue;
Class> ftype = field.getType();
String name = field.getName();
String qualified = prefix != null ? prefix + '.' + name : name;
Trace.g.std.note(Reifier.class, "collect(): qualified = " + qualified);
if (ftype.isArray()) {
Trace.g.std.note(Reifier.class, "collect(): field is an array");
ArrayList