//constraint for later conversions!
List<DataTypeEnum> sortedActiveDataTypes = new ArrayList<DataTypeEnum>(valueConstraint.getDataTypes().size());
//NOTE: using a LinkedHashSet would slow down this code, because EnumSet
// gives constant processing time even for bulk operations!
for(String dataTypeUri : valueConstraint.getDataTypes()){
DataTypeEnum dataType = DataTypeEnum.getDataType(dataTypeUri);
if(dataType == null){
log.warn(String.format("DataType %s not supported"));
} else {
if(activeDataTypes.add(dataType)){
//only of set has changed to avoid duplicates in the list
sortedActiveDataTypes.add(dataType);
}
}
}
//2) now process the values
// log.info(" --- Filter values ---");
//calculating acceptable and not acceptable types needs some processing time
//and usually values will be only of very less different types.
//Therefore it makes sense to cache accepted and rejected types!
Set<Class<?>> accepted = new HashSet<Class<?>>();
Set<Class<?>> rejected = new HashSet<Class<?>>();
//Set that stores rejected values. Such will be converted later on!
Set<Object> needConversion = new HashSet<Object>();
for(Iterator<Object> it = values.iterator();it.hasNext();){
Object value = it.next();
// if(accepted.contains(value.getClass())){
// log.info(String.format(" + value %s(type:%s) accepted by value filter",value,value.getClass()));
//nothing to do
// } else
if(rejected.contains(value.getClass())){
it.remove(); //remove also the current value of that type
needConversion.add(value); //save as value that need to be converted
// log.info(String.format(" - value %s(type:%s) rejected by value filter",value,value.getClass()));
} else { //new class ... calculate
Set<DataTypeEnum> valueTypes = DataTypeEnum.getAllDataTypes(value.getClass());
if(valueTypes.removeAll(activeDataTypes)){
accepted.add(value.getClass());
// log.info(String.format(" + value %s(type:%s) accepted by value filter",value,value.getClass()));
} else {
rejected.add(getClass());
it.remove(); //remove the Item
needConversion.add(value); //save as value that need to be converted
// log.info(String.format(" - value %s(type:%s) rejected by value filter",value,value.getClass()));
}
}
}
//3) try to convert values to the active dataTypes
// log.info(" --- Try to Convert rejected values ---");
for(Object value : needConversion){
Object converted = null;
DataTypeEnum convertedTo = null;
for(Iterator<DataTypeEnum> dataTypes = sortedActiveDataTypes.iterator(); //iterate over all active dataTypes
converted == null && dataTypes.hasNext();){ //while converted still null and more dataTypes to try
convertedTo = dataTypes.next();
converted = valueConverter.convert(value, convertedTo.getUri(),valueFactory); //try the conversion
}
if(converted != null){
// log.info(String.format(" + value %s(javaType=%s) successfully converted to %s(datatype=%s)",
// value,value.getClass().getSimpleName(),converted,convertedTo.getShortName()));
values.add(converted);