/** * 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); } }
@Override public void setProperty(String name, Object value) { if (!(value instanceof String)) { value = ConverterRegistry.getConverter().asString(value); } contextParameters.put(name, value); }
/** * 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); } } }
/** * 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); }
/** * Set configuration object property. Convert <code>value</code> to string and set property. If property already * exists overwrite old value. If <code>value</code> argument is null this setter does nothing. * * @param name property name, * @param value property value, null ignored. * @throws IllegalArgumentException if <code>name</code> argument is null or empty. * @throws ConverterException if there is no converter registered for value type. */ public void setProperty(String name, Object value) { Params.notNullOrEmpty(name, "Property name"); if(value != null) { properties.setProperty(name, converter.asString(value)); } }
@Override public <T> T getProperty(String name, Class<T> type) { return ConverterRegistry.getConverter().asObject(contextParameters.getProperty(name), type); }
/** * Set this configuration object value. Value is converter to string before store it. If <code>value</code> argument * is null this setter will reset this configuration object value. * * @param value value to set, null accepted. * @throws IllegalArgumentException if <code>value</code> argument is an empty string. * @throws ConverterException if there is no converter registered for value type. */ public void setValue(Object value) { if(value instanceof String) { Params.notEmpty((String)value, "Value"); this.value = (String)value; } else { this.value = value != null ? converter.asString(value) : null; } }
/** * 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); }
/** * 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); }
/** * 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); } }
/** * 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)); }
/** * Get array instance initialized from JSON characters stream. * * @return array instance. */ @Override public Object instance() { if (instance == null) { Class<?> arrayClass = null; Type arrayType = getType(); if (arrayType instanceof Class) { arrayClass = (Class<?>) arrayType; } else { assert arrayType instanceof ParameterizedType; arrayClass = (Class<?>) ((ParameterizedType) arrayType).getRawType(); } instance = Array.newInstance(arrayClass, values.size()); for (int i = 0; i < values.size(); i++) { Object value = values.get(i); if (value == null) { Array.set(instance, i, null); } else if (value instanceof String) { Array.set(instance, i, converter.asObject((String) value, arrayClass)); } else { Array.set(instance, i, value); } } } return instance; }
/** * 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)); }
/** * 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); }
@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); } }
/** * 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); }
return ConverterRegistry.getConverter().asString(value);
@Override public <T> T getValue(Class<T> type) { return ConverterRegistry.getConverter().asObject(getValue(), type); }
throw new TemplateException("Invalid element |%s|. Operand for VALUE operator without formatter should be convertible to string.", element); value = ConverterRegistry.getConverter().asString(object);