@Override @SuppressWarnings("unchecked") public Object convert(Object object) throws TypeException { if (object instanceof List) { List<Object> list = (List<Object>) object; for (int i = 0, len = list.size(); i < len; i++) { Object value = list.get(i); list.set(i, type.convert(value)); } return object; } throw new TypeException("Incompatible object: " + object.getClass() + " for type " + getName()); }
@Override @SuppressWarnings("unchecked") public Object convert(Object object) throws TypeException { if (object instanceof Map) { Map<Object, Object> map = (Map<Object, Object>) object; for (Entry<Object, Object> entry : map.entrySet()) { String key = entry.getKey().toString(); Field field = getField(key); if (field == null) { throw new IllegalArgumentException("Field " + key + " is not defined for the complex type " + getName()); } entry.setValue(field.getType().convert(entry.getValue())); } return object; } throw new TypeException("Incompatible object: " + object.getClass() + " for type " + this); }
/** * Check if the given field type store a list of values and if the given value is compatible with the given type. We * assume the Type store a list of scalar values, not complex types. */ protected void checkFieldType(Type type, Object value) throws OperationException { if (!type.isListType()) { throw new OperationException("Only multivalued types can be set using this operation"); } ListType listType = (ListType) type; Type itemType = listType.getFieldType(); if (itemType.isComplexType()) { throw new UnsupportedOperationException("Manage only lists of scalar items"); } try { if (itemType.convert(value) == null) { String exceptionReason = String.format("Given type \"%s\" value is not a %s type", value, itemType.getName()); throw new UnsupportedOperationException(exceptionReason); } } catch (TypeException | ClassCastException e) { String exceptionReason = String.format("Given type \"%s\" value is not a %s type", value, itemType.getName()); throw new OperationException(exceptionReason, e); } } }