/** * Add map entry, overriding existing one if happen to have the same key name. This method uses {@link #key} stored by a * previous call to {@link #setKey(Object)}; together with given <code>value</code> creates a new entry that is stored into * map. * * @param value map value. */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void setValue(Object value) { if (key instanceof String) { key = converter.asObject((String) key, keyType); } if (value instanceof String) { value = converter.asObject((String) value, valueType); } ((Map) instance).put(key, value); } }
/** * Get files archive meta data converted to requested type or default value if meta data key is missing. * * @param key meta data key, * @param type type to convert meta data value to, * @param defaultValue default value returned if key not found. * @param <T> meta data type. * @return meta data value converted to type or default value. */ public <T> T getMeta(String key, Class<T> type, T defaultValue) { String value = manifest.getMainAttributes().getValue(key); return value == null ? defaultValue : ConverterRegistry.getConverter().asObject(value, type); }
@Override public <T> T getProperty(String name, Class<T> type) { return ConverterRegistry.getConverter().asObject(contextParameters.getProperty(name), type); }
/** * Collect parsed item from JSON characters stream. * * @param value parsed collection item. */ @Override public void set(Object value) { if (value == null) { instance.add(null); } else if (value instanceof String) { if (!(type instanceof Class)) { throw new IllegalStateException(String.format("Expect primitive value as String but got type |%s| is parameterized.", type)); } instance.add(converter.asObject((String) value, (Class<?>) type)); } else { instance.add(value); } } }
/** * Get this configuration object value converted to requested type. Returns null if this configuration object has no * value. * * @param type desired value object type. * @param <T> value type. * @return newly created value object or null. * @throws IllegalArgumentException if <code>type</code> argument is null. * @throws ConverterException if there is no converter registered for value type or value parse fails. */ public <T> T getValue(Class<T> type) { Params.notNull(type, "Value type"); if(value == null) { return null; } return converter.asObject(value, type); }
/** * Set this primitive value from string value. * * @param value string value. * @throws JsonParserException if value is not a string. */ @Override public void set(Object value) { if (value == null) { instance = null; return; } if (!(value instanceof String)) { throw new JsonParserException("Invalid type. Expected java.lang.String but got |%s|", value.getClass()); } instance = converter.asObject((String) value, clazz); } }
/** * Set value to child node identified by given child index. Given string value is converted to list component type, see * {@link #componentType} using {@link Converter}. * * @param childIndex child index, * @param value string value, null and empty accepted, loaded from field. * @throws IllegalArgumentException if child index argument is not a numeric value. * @throws ConverterException if field value conversion fails. */ @Override public void setValue(String childIndex, Object value) throws IllegalArgumentException, ConverterException { Params.isNumeric(childIndex, "Child index"); int index = Integer.parseInt(childIndex); ensureCapacity(index); if (!String.class.equals(componentType)) { value = converter.asObject((String) value, componentType); } list.set(index, value); }
/** * Get files archive meta data converted to requested type. * * @param key meta data key, * @param type type to convert meta data value to. * @param <T> meta data type. * @return meta data value converted to type. * @throws InvalidFilesArchiveException if requested meta data key does not exists. */ public <T> T getMeta(String key, Class<T> type) { return ConverterRegistry.getConverter().asObject(getMeta(key), type); }
@Override public <T> T getValue(Class<T> type) { return ConverterRegistry.getConverter().asObject(getValue(), type); }
/** * Set value to child specified by given index. If child index exceeds array length this setter does nothing and value * is ignored. * <p> * Given string value is converted to array type, see {@link #componentType} using {@link Converter}. * * @param childIndex node child index, * @param value string value, null and empty accepted, loaded from field. * @throws IllegalArgumentException if child index argument is not a numeric value. * @throws ConverterException if field value conversion fails. */ @Override public void setValue(String childIndex, Object value) throws IllegalArgumentException, ConverterException { Params.isNumeric(childIndex, "Child index"); int index = Integer.parseInt(childIndex); if (index >= Array.getLength(array)) { log.debug("Array capacity exceeded. Ingore value |%s| for array index |%d|.", value, index); return; } if (!String.class.equals(componentType)) { value = converter.asObject((String) value, componentType); } Array.set(array, index, value); } }
/** * Get attribute value converted to requested type or default value if there is no attribute with requested name. If * given default value is null and attribute is not found this method still returns null, that is, the requested * default value. * * @param name attribute name, * @param type type to converter attribute value to, * @param defaultValue default value. * @param <T> type to convert attribute value to. * @return newly created value object instance or default value. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. * @throws IllegalArgumentException if <code>type</code> argument is null. * @throws ConverterException if there is no converter registered for value type or value parse fails. */ public <T> T getAttribute(String name, Class<T> type, T defaultValue) { Params.notNullOrEmpty(name, "Attribute name"); Params.notNull(type, "Attribute type"); String value = attributes.get(name); return value != null ? converter.asObject(value, type) : defaultValue; }
/** * Invoke setter method on given object instance. Setter name has format as accepted by * {@link Strings#getMethodAccessor(String, String)} and value string is converted to method parameter type using * {@link Converter} facility. For this reason set parameter type should have a converter registered. * <p> * This method has no means to determine method using {@link Class#getMethod(String, Class...)} because parameter * value is always string and setter parameter type is unknown. For this reason this method uses * {@link #findMethod(Class, String)}. Note that find method searches for named method on object super hierarchy too. * * @param object object instance, * @param name setter name, method name only without <code>set</code> prefix, dashed case accepted, * @param value value to set, string value that is converted to setter method parameter type. * @throws NoSuchMethodException if setter not found. * @throws Exception if invocation fail for whatever reason including method logic. */ public static void invokeSetter(Object object, String name, String value) throws NoSuchMethodException, Exception { String setterName = Strings.getMethodAccessor("set", name); Class<?> clazz = object.getClass(); Method method = findMethod(clazz, setterName); Class<?>[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length != 1) { throw new NoSuchMethodException(String.format("%s#%s", clazz.getName(), setterName)); } invoke(object, method, ConverterRegistry.getConverter().asObject((String)value, parameterTypes[0])); }
/** * Get named parameter throwing exception if not found. * * @param name requested parameter name, * @param type desired parameter type. * @param <T> parameter type. * @return named parameter instance. * @throws BugError if named parameter does not exist. */ protected <T> T getParameter(String name, Class<T> type) { if (parameters == null) { throw new BugError("Event stream |%s| parameters not configured.", this); } String value = parameters.get(name); if (value == null) { throw new BugError("Missing event stream parameter |%s| of expected type |%s|.", name, type); } return ConverterRegistry.getConverter().asObject(value, type); }
/** * Set value to wrapped object field identified by requested child name. Given string value is converted to wrapped * object type using {@link Converter}. * * @param childName wrapped object field name, * @param value string value loaded from form field, null and empty accepted. * @throws ConverterException if field value conversion fails. * @throws IllegalAccessException this exception is required by reflective field signature but never thrown. */ @Override public void setValue(String childName, Object value) throws ConverterException, IllegalAccessException { Field field = null; try { field = Classes.getFieldEx(object.getClass(), Strings.toMemberName(childName)); } catch (NoSuchFieldException e) { // best effort approach: form to object mapping is not so strictly - i.e. ignores missing fields log.debug("Missing field |%s| from object |%s|.", childName, object.getClass()); return; } if (value instanceof UploadedFile) { if (Types.isKindOf(field.getType(), File.class)) { field.set(object, ((UploadedFile) value).getFile()); } else { field.set(object, value); } } else { field.set(object, converter.asObject((String) value, field.getType())); } } }
value = ConverterRegistry.getConverter().asObject((String)value, type);
@Override public boolean evaluate(Object value, String operand) { if (Types.isNumber(value)) { Converter converter = ConverterRegistry.getConverter(); Double doubleValue = ((Number) value).doubleValue(); Double doubleOperand = 0.0; if (value instanceof Float) { // converting float to double may change last decimal digits // we need to ensure both value and operand undergo the same treatment, i.e. use Float#doubleValue() method // for both // for example float number 1.23F is converted to double to 1.2300000190734863 // if we convert string "1.23" to double we have 1.23 != 1.2300000190734863 Float floatOperand = converter.asObject(operand, Float.class); doubleOperand = floatOperand.doubleValue(); } else { doubleOperand = converter.asObject(operand, Double.class); } return compare(doubleValue, doubleOperand); } if (Types.isDate(value)) { Date dateValue = (Date) value; Date dateOperand = Dates.parse(operand); return compare(dateValue, dateOperand); } return false; }
/** * Get configuration object property converter to requested type or default value if there is no property with given * name. * * @param name property name. * @param type type to convert property value to, * @param defaultValue default value, possible null or empty. * @param <T> value type. * @return newly created value object or default value. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. * @throws IllegalArgumentException if <code>type</code> argument is null. * @throws ConverterException if there is no converter registered for value type or value parse fails. */ public <T> T getProperty(String name, Class<T> type, T defaultValue) { Params.notNullOrEmpty(name, "Property name"); Params.notNull(type, "Property type"); String value = getProperty(name); if(value != null) { return converter.asObject(value, type); } return defaultValue; }
@SuppressWarnings("unchecked") @Override public <T> T getValue(String name, Class<T> clazz) { FormField field = getFormField(name); return (T) (field != null ? ConverterRegistry.getConverter().asObject(field.getValue(), clazz) : Types.getEmptyValue(clazz)); }
/** * Variant for {@link #invokeSetter(Object, String, String)} but no exception if setter not found. * * @param object object instance, * @param name setter name, * @param value value to set. * @throws Exception if invocation fail for whatever reason including method logic. */ public static void invokeOptionalSetter(Object object, String name, String value) throws Exception { String setterName = Strings.getMethodAccessor("set", name); Class<?> clazz = object.getClass(); Method method = null; try { method = findMethod(clazz, setterName); } catch(NoSuchMethodException e) { log.debug("Setter |%s| not found in class |%s| or its super hierarchy.", setterName, clazz); return; } Class<?>[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length != 1) { log.debug("Setter |%s#%s(%s)| with invalid parameters number.", method.getDeclaringClass(), method.getName(), Strings.join(parameterTypes, ',')); return; } invoke(object, method, ConverterRegistry.getConverter().asObject((String)value, parameterTypes[0])); }
@SuppressWarnings("unchecked") @Override public <T> T asObject(String string, Class<T> valueType) { Params.notNull(valueType, "Value type"); if(string == null) { return null; } if(valueType == Object.class) { return (T)string; } if(valueType == String.class) { return (T)string; } Converter converter = getConverter(valueType); if(converter == null) { throw new ConverterException("No registered converter for |%s|.", valueType); } try { return converter.asObject(string, valueType); } catch(ConverterException e) { throw e; } catch(Throwable t) { throw new ConverterException(t); } }