/** * Gets the size of the collection mapped to the specified key. * * @param key the key to get size for * @return the size of the collection at the key, zero if key not in map */ public int size(final Object key) { final Collection<V> coll = getCollection(key); if (coll == null) { return 0; } return coll.size(); }
/** * Checks whether the collection at the specified key contains the value. * * @param key the key to search for * @param value the value to search for * @return true if the map contains the value */ public boolean containsValue(final Object key, final Object value) { final Collection<V> coll = getCollection(key); if (coll == null) { return false; } return coll.contains(value); }
/** * Removes a specific value from map. * <p> * The item is removed from the collection mapped to the specified key. * Other values attached to that key are unaffected. * <p> * If the last value for a key is removed, <code>null</code> will be returned * from a subsequent <code>get(key)</code>. * * @param key the key to remove from * @param value the value to remove * @return {@code true} if the mapping was removed, {@code false} otherwise */ @Override public boolean removeMapping(final Object key, final Object value) { final Collection<V> valuesForKey = getCollection(key); if (valuesForKey == null) { return false; } final boolean removed = valuesForKey.remove(value); if (removed == false) { return false; } if (valuesForKey.isEmpty()) { remove(key); } return true; }
/** * Adds a collection of values to the collection associated with * the specified key. * * @param key the key to store against * @param values the values to add to the collection at the key, null ignored * @return true if this map changed */ public boolean putAll(final K key, final Collection<V> values) { if (values == null || values.size() == 0) { return false; } boolean result = false; Collection<V> coll = getCollection(key); if (coll == null) { coll = createCollection(values.size()); // might produce a non-empty collection coll.addAll(values); if (coll.size() > 0) { // only add if non-zero size to maintain class state decorated().put(key, coll); result = true; // map definitely changed } } else { result = coll.addAll(values); } return result; }
/** * Adds the value to the collection associated with the specified key. * <p> * Unlike a normal <code>Map</code> the previous value is not replaced. * Instead the new value is added to the collection stored against the key. * * @param key the key to store against * @param value the value to add to the collection at the key * @return the value added if the map changed and null if the map did not change */ @Override @SuppressWarnings("unchecked") public Object put(final K key, final Object value) { boolean result = false; Collection<V> coll = getCollection(key); if (coll == null) { coll = createCollection(1); // might produce a non-empty collection coll.add((V) value); if (coll.size() > 0) { // only add if non-zero size to maintain class state decorated().put(key, coll); result = true; // map definitely changed } } else { result = coll.add((V) value); } return result ? value : null; }
/** * Gets the collection mapped to the specified key. * This method is a convenience method to typecast the result of <code>get(key)</code>. * * @param key the key used to retrieve the collection * @return the collection mapped to the key, null if no mapping */ public Collection getParameter(String key) { return parameters.getCollection(key); }
public Collection<Integer> getList(String key) { if(key.isEmpty()){ Collection<Integer> c = new ArrayList<>(1); c.add(1); return c; } Collection<Integer> r = this.map.getCollection(key); return (r==null) ? Collections.EMPTY_LIST : r; }
/** * Retrieve a parameter as a certain given class * * @param <T> the type of parameter returned. * @param key the key to retrieve the parameter from * @param clazz the type to retrieve. (If no parameter with that class is found, a <tt>null</tt> value is returned.) * @return the selected parameter, or null. */ public <T> T getParameterAsClass(String key, Class<T> clazz) { Collection c = parameters.getCollection(key); if (c == null || c.isEmpty()) { return null; } else { Object o = c.iterator().next(); if (clazz.isAssignableFrom(o.getClass())) { return (T) o; } else { return null; } } }