/** * Convenient way to retrieve converter instance, see {@link #getConverterInstance()}. * * @return converter instance. */ public static Converter getConverter() { return instance.getConverterInstance(); }
/** * Construct node object. * * @param object wrapped object. */ public ObjectNode(Object object) { this.converter = ConverterRegistry.getConverter(); this.object = object; }
/** * Convenient way to test for converter support, see {@link #hasClassConverter(Class)}. Note that this predicate * variant accepts {@link Type} as parameter to simplify integration with user code but always returns false if type * is not a {@link Class}. * * @param valueType value type to check for conversion support. * @return true if value type has registered converter. */ public static boolean hasType(Type valueType) { return valueType instanceof Class ? instance.hasClassConverter((Class<?>)valueType) : false; }
@Override public <T> T getProperty(String name, Class<T> type) { return ConverterRegistry.getConverter().asObject(contextParameters.getProperty(name), type); }
@Override public void setProperty(String name, Object value) { if (!(value instanceof String)) { value = ConverterRegistry.getConverter().asString(value); } contextParameters.put(name, value); }
@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); } }
@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); } }
/** * An object is <em>strict object</em> if is not value type, that is, there is no converter for it. * * @param object object instance to check. * @return true if <code>object</code> is <em>strict object</em>. */ private boolean isStrictObject(Object object) { return !ConverterRegistry.hasType(object.getClass()); } }
/** * Return the first character from given string. * * @throws ConverterException if given string has more than one single character. */ @Override public <T> T asObject(String string, Class<T> valueType) throws BugError { // at this point value type is guaranteed to be char or Character if (string.length() > 1) { throw new ConverterException("Trying to convert a larger string into a single character."); } return (T) (Character) string.charAt(0); }
/** * 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); }
/** * 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)); }
/** * 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; }
/** * 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); }
/** * 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)); }
/** * 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); }
@Override public <T> T getValue(Class<T> type) { return ConverterRegistry.getConverter().asObject(getValue(), 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 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 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)); }