All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.aguacate.configuration.field.FieldArray Maven / Gradle / Ivy

There is a newer version: 0.10.9
Show newest version
package net.sf.aguacate.configuration.field;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.aguacate.validator.ValidationConversionResult;

/**
 * 
 * "field": {
 *   "type": "ARRAY",
 *   "unique": ...,
 *   "data": {
 *     "type": "INTEGER"
 *     "minval": ...,
 *     "maxval": ...
 *   }
 * }
 * 
*/ public class FieldArray extends Field { private final String contextName; private final Field field; private final boolean unique; public FieldArray(String name, boolean optional, String contextName, Field field, boolean unique) { super(name, Field.ARRAY, optional); this.contextName = contextName; this.field = field; this.unique = unique; } @Override public ValidationConversionResult validateAndConvert(Object value) { if (value instanceof List) { @SuppressWarnings("unchecked") List list = (List) value; int size = list.size(); List result = new ArrayList<>(size); for (int i = 0; i < size; i++) { ValidationConversionResult temp = field.validateAndConvert(list.get(i)); if (temp.isSuccess()) { Map sub = new HashMap<>(); sub.put(contextName, temp.getValue()); if (unique && result.contains(sub)) { return new ValidationConversionResult("duplicate value"); } else { result.add(sub); } } else { return temp; } } return new ValidationConversionResult(result); } else { throw new IllegalStateException(value.getClass().getName()); } } }