/** * Gets the value at the specified key as a set * @param key the key of the value * @return the value * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the value is not a set */ @SuppressWarnings("unchecked") public <T> Set<T> getAsSet(final String key) { Object value = get(key); try { return (Set<T>)value; } catch (ClassCastException ex) { throw new CantCoerceException(key, value, "Set"); } }
/** * Gets the value at the specified key as a map * @param key the key of the value * @return the value * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the value is not a set */ @SuppressWarnings("unchecked") public <K, V> Map<K, V> getAsMap(final String key) { final Object value = get(key); if (value instanceof Map) { return (Map<K, V>)value; } else { throw new CantCoerceException(key, value, "Map"); } }
/** * Gets the value at the specified key as a float * @param key the key of the value * @return If the value is a {@link Number}, the value's {@link Number#floatValue()}. Otherwise, * its {@link Object#toString()} is called and the result is parsed as a float. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a float and can't be * converted to one */ public float getAsFloat(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).floatValue(); } try { return Float.parseFloat(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "float"); } }
/** * Gets the value at the specified key as a double * @param key the key of the value * @return If the value is a {@link Number}, the value's {@link Number#doubleValue()}. Otherwise, * its {@link Object#toString()} is called and the result is parsed as a double. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a double and can't be * converted to one */ public double getAsDouble(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).doubleValue(); } try { return Double.parseDouble(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "double"); } }
/** * Gets the value at the specified key as an integer * @param key the key of the value * @return If the value is a {@link Number}, the value's {@link Number#intValue()}. Otherwise, its * {@link Object#toString()} is called and the result is parsed as an integer. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not an integer and can't be * converted to one */ public int getAsInt(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).intValue(); } try { return Integer.parseInt(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "int"); } }
/** * Gets the value at the specified key as a character * @param key the key of the value * @return If the value is a {@link Character}, the value. If it is a {@link String} and is of * length 1, the character of the string. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a character and can't be * converted to one */ public char getAsChar(final String key) { final Object value = getNotNull(key); if (value instanceof Character) { return ((Character)value); } if (value instanceof String) { final String stringValue = (String)value; if (stringValue.length() == 1) { return stringValue.charAt(0); } } throw new CantCoerceException(key, value, "char"); }
/** * Gets the value at the specified key as a boolean * @param key the key to find * @return If the value is a {@link Boolean}, it is returned. If its {@link Object#toString()} * matches the words "true" or "false", one of those values is returned. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the value is not a boolean and can't be converted to one */ public boolean getAsBoolean(final String key) { final Object value = getNotNull(key); if (value instanceof Boolean) { return ((Boolean)value); } if ("true".equalsIgnoreCase(value.toString())) { return true; } if ("false".equalsIgnoreCase(value.toString())) { return false; } throw new CantCoerceException(key, value, "boolean"); }
/** * Gets the value at the specified key as a long * @param key the key of the value * @return If the value is a {@link Number}, the value's {@link Number#longValue()}. Otherwise, * its {@link Object#toString()} is called and the result is parsed as a long. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a long and can't be * converted to one */ public long getAsLong(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).longValue(); } try { return Long.parseLong(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "long"); } }
/** * Gets the value at the specified key as a short * @param key the key of the value * @return If the value is a {@link Number}, the value's {@link Number#shortValue()}. Otherwise, * its {@link Object#toString()} is called and the result is parsed as a short. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a short and can't be * converted to one */ public short getAsShort(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).shortValue(); } try { return Short.parseShort(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "short"); } }
/** * Gets the value at the specified key as a byte * @param key the key of the value * @return If the value is a {@link Number}, its byte value is returned. Otherwise, its * {@link Object#toString()} is called and the result is passed to * {@link Byte#parseByte(String)}. * @throws AttributeUndefinedException if the key doesn't exist * @throws CantCoerceException if the key exists, but the value is not a byte and can't be * converted to one */ public byte getAsByte(final String key) { final Object value = getNotNull(key); if (value instanceof Number) { return ((Number)value).byteValue(); } try { return Byte.parseByte(value.toString()); } catch (final NumberFormatException e) { throw new CantCoerceException(key, value, "byte"); } }
/** * Gets the value at the specified key as a list * @param key the key of the value * @return If the value is a list or null, the value. If it's an array, it will be converted to a * list, along with any sub-arrays. * @throws CantCoerceException if the key exists, but the value is not a list and can't be * converted to one */ @SuppressWarnings("unchecked") public <T> List<T> getAsList(final String key) { Object value = get(key); // TODO: Other methods throw AttributeUndefinedException if the value is null. Be consistent. if ((value == null) || (value instanceof List)) { return (List<T>)value; } // TODO: Remove coercion. If the client wants to get a list, they should put a list in. if (value instanceof Object[]) { final List<?> result = Arrays.asList((Object[])value); return Lists.transform(result, new Function<Object, T>() { public T apply(final Object in) { if (in instanceof Object[]) { return (T)convertArraysToLists(Arrays.asList((Object[])in)); } return (T)in; } }); } throw new CantCoerceException(key, value, "List"); }