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

net.sf.aguacate.configuration.field.FieldStructureArray 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.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.aguacate.validator.ValidationConversionResult;

/**
 * 
 * "field": {
 *   "type": "STRUCTURE_ARRAY",
 *   "data": {
 *     "innerField1": {
 *       "type": "INTEGER",
 *       "minval": ...,
 *       "maxval": ...
 *     },
 *     "innerField2": {
 *       "type" : "STRING",
 *       "minlen": ...,
 *       "maxlen": ...,
 *       "regex": ...
 *     }
 *   }
 * }
 * 
* */ public class FieldStructureArray extends Field { private final Field[] fields; public FieldStructureArray(String name, boolean optional, Map fields) { this(name, optional, fields.values()); } public FieldStructureArray(String name, boolean optional, Collection fields) { this(name, optional, fields.toArray(new Field[fields.size()])); } public FieldStructureArray(String name, boolean optional, Field[] fields) { super(name, Field.STRUCTURE_ARRAY, optional); this.fields = fields; } @Override public ValidationConversionResult validateAndConvert(Object value) { @SuppressWarnings("unchecked") List> list = (List>) value; List> r = new ArrayList<>(); int size = list.size(); for (int i = 0; i < size; i++) { Map data = list.get(i); Map map = new HashMap<>(); for (Field field : fields) { String fieldName = field.getName(); Object obj = data.get(fieldName); if (field.isOptional()) { if (obj == null) { map.put(fieldName, null); } else { ValidationConversionResult result = field.validateAndConvert(obj); if (result.isSuccess()) { map.put(fieldName, result.getValue()); } else { return result; } } } else { ValidationConversionResult result = field.validateAndConvert(obj); if (result.isSuccess()) { map.put(fieldName, result.getValue()); } else { return result; } } } r.add(map); } return new ValidationConversionResult(r); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy