public Object get(final String key) { return attributes.get(key); }
public Object get(String key) { return _attributes.get(key); }
/** * Gets the value at the specified key as a string * @param key the key of the value * @return the result of the value's {@link Object#toString()} * @throws AttributeUndefinedException if the key doesn't exist */ public String getAsString(final String key) { final Object attribute = get(key); // TODO: Other methods throw AttributeUndefinedException if the value is null. Be consistent. return attribute == null ? null : attribute.toString(); }
/** * Gets the value at the specified key, checking for null * @param key the key to find * @return the value at the key * @throws AttributeUndefinedException if the key doesn't exist or if its value is null */ public Object getNotNull(final String key) { // TODO: If the documentation for this class is correct, AttributeMap does not support null // values. If that is true, this method should be removed. Object result = this.get(key); if (result == null) { throw new AttributeUndefinedException(key); } return result; }
/** * 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 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 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"); }