/** * Construct node object. * * @param object wrapped object. */ public ObjectNode(Object object) { this.converter = ConverterRegistry.getConverter(); this.object = object; }
/** * Construct list node for a given list component type. * * @param object wrapped list, * @param componentType list component type. */ public ListNode(Object object, Class<?> componentType) { this.converter = ConverterRegistry.getConverter(); this.list = (List<Object>) object; this.componentType = componentType; }
/** * Check if registry instance has registered converter for requested class. * * @param classConverter class to check for conversion support. * @return true if this converter registry has support for requested class. */ public boolean hasClassConverter(Class<?> classConverter) { return getConverter(classConverter) != null || String.class.equals(classConverter); }
/** * Construct array node. * * @param array wrapped array. */ public ArrayNode(Object array) { this.converter = ConverterRegistry.getConverter(); this.array = array; this.componentType = array.getClass().getComponentType(); }
@Override public void setProperty(String name, Object value) { if (!(value instanceof String)) { value = ConverterRegistry.getConverter().asString(value); } contextParameters.put(name, value); }
/** * Put meta data to this archive manifest. If <code>key</code> already exists is overridden. Meta <code>value</code> is * converted to string and can be any type for which there is a {@link Converter} registered. * * @param key meta data key, * @param value meta data value. */ public void putMeta(String key, Object value) { manifest.getMainAttributes().putValue(key, ConverterRegistry.getConverter().asString(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); }
/** * Implements equality test logic. This method converts value to string using {@link Converter} then compare it with * operand. As a consequence operand format must be compatible with value type. For example if value type is a * {@link Date} operand syntax should be ISO8601; please see {@link Converter} documentation for supported formats. */ @Override public boolean evaluate(Object value, String operand) { if (value == null) { return operand.equals("null"); } if (value instanceof Date) { return evaluateDates((Date) value, operand); } return ConverterRegistry.getConverter().asString(value).equals(operand); }
/** * 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); }
/** * Convert value object to string and delegates {@link #add(String, String)}. String conversion is performed by * {@link Converter} and may throw {@link ConverterException}. * * @param name cookie name, not null or empty, * @param value cookie value, not null or empty. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. * @throws IllegalArgumentException if <code>value</code> argument is null or empty. * @throws ConverterException if value object has no converter registered or object value serialization fails. */ public void add(String name, Object value) { add(name, ConverterRegistry.getConverter().asString(value)); }
@Override public <T> T getValue(Class<T> type) { return ConverterRegistry.getConverter().asObject(getValue(), type); }
/** * 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); }
/** * 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])); }
@Override public String asString(Object object) { if(object == null) { return null; } if(object instanceof String) { return (String)object; } Converter converter = getConverter(object.getClass()); if(converter == null) { throw new ConverterException("No registered converter for |%s|.", object.getClass()); } try { return converter.asString(object); } catch(ConverterException e) { throw e; } catch(Throwable t) { throw new ConverterException(t); } }
/** * Set variables value. If variable name already exists its value is overridden. Convert variable value to string before * storing to variables map. Null is not accepted for either variable name or its value. * <p> * This method uses {@link Converter} to convert variable value to string. If there is no converter able to handle given * variable value type this method rise exception. * * @param name variable name, * @param value variable value. * @throws IllegalArgumentException if variable name is null or empty or value is null. * @throws ConverterException if there is no converter able to handle given value type. */ public void put(String name, Object value) { Params.notNullOrEmpty(name, "Variable name"); Params.notNull(value, "Variable %s value", name); variables.put(name, ConverterRegistry.getConverter().asString(value)); }
@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])); }
@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; }
@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); } }