A simple implementation of Bindings backed by a HashMap or some other specified Map . @author Mike Grogan @version 1.0 @since 1.6
| 21 | * @since 1.6 |
| 22 | */ |
| 23 | public class SimpleBindings implements Bindings { |
| 24 | |
| 25 | /** |
| 26 | * The <code>Map</code> field stores the attributes. |
| 27 | */ |
| 28 | private Map<String,Object> map; |
| 29 | |
| 30 | /** |
| 31 | * Constructor uses an existing <code>Map</code> to store the values. |
| 32 | * @param m The <code>Map</code> backing this <code>SimpleBindings</code>. |
| 33 | * @throws NullPointerException if m is null |
| 34 | */ |
| 35 | public SimpleBindings(Map<String,Object> m) { |
| 36 | if (m == null) { |
| 37 | throw new NullPointerException(); |
| 38 | } |
| 39 | this.map = m; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Default constructor uses a <code>HashMap</code>. |
| 44 | */ |
| 45 | public SimpleBindings() { |
| 46 | this(new HashMap<String,Object>()); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Sets the specified key/value in the underlying <code>map</code> field. |
| 51 | * |
| 52 | * @param name Name of value |
| 53 | * @param value Value to set. |
| 54 | * |
| 55 | * @return Previous value for the specified key. Returns null if key was previously |
| 56 | * unset. |
| 57 | * |
| 58 | * @throws <code>NullPointerException</code> if the name is null. |
| 59 | * @throws <code>IllegalArgumentException</code> if the name is empty. |
| 60 | */ |
| 61 | public Object put(String name, Object value) { |
| 62 | checkKey(name); |
| 63 | map.put(name,value); |
| 64 | return value; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * <code>putAll</code> is implemented using <code>Map.putAll</code>. |
| 69 | * |
| 70 | * @param toMerge The <code>Map</code> of values to add. |
| 71 | * |
| 72 | * @throws <code>NullPointerException</code> if some key in the specified <code>Map</code> |
| 73 | * is null. |
| 74 | * @throws <code>IllegalArgumentException</code> if some key in the specified <code>Map</code> is empty String. |
| 75 | */ |
| 76 | public void putAll(Map<? extends String, ? extends Object> toMerge) { |
| 77 | for (Map.Entry<? extends String, ? extends Object> entry : toMerge.entrySet()) { |
| 78 | String key = entry.getKey(); |
| 79 | checkKey(key); |
| 80 | put(key, entry.getValue()); |
nothing calls this directly
no outgoing calls
no test coverage detected