m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/model/AttributesMap.java
blob: 3391417d26e18a59a45e3a94a231f567e6fb07f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * Copyright (c) 2014, University of Warsaw
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package pl.edu.mimuw.cloudatlas.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Represents a map from <code>Attribute</code> to <code>Value</code>. It cannot contain duplicate keys.
 */
public class AttributesMap implements Iterable<Entry<Attribute, Value>>, Cloneable, Serializable {
    private Map<Attribute, Value> map = new LinkedHashMap<Attribute, Value>();

    private void checkNulls(Attribute attribute, Value value) {
        if(attribute == null)
            throw new NullPointerException("The attribute cannot be null.");
        if(value == null)
            throw new NullPointerException(
                    "The value cannot be null. You may want create a Value object that contains null.");
    }

    /**
     * Adds a new <code>attribute</code>-<code>value</code> mapping. The
     * <code>attribute</code> cannot already exist in the map. To overwrite an existing attribute, use
     * {@link #addOrChange(Attribute, Value)} instead.
     *
     * @param attribute the attribute to add
     * @param value the value for <code>attribute</code>
     * @throws IllegalArgumentException if the <code>attribute</code> already exists in this map
     * @throws NullPointerException if either the <code>attribute</code> or the <code>value</code> is <code>null</code>;
     * for null value, create a <code>Value</code> object containing <code>null</code>
     * @see #addOrChange(Attribute, Value)
     * @see #add(String, Value)
     * @see #add(Entry)
     * @see #add(AttributesMap)
     */
    public void add(Attribute attribute, Value value) {
        if(getOrNull(attribute) != null)
            throw new IllegalArgumentException("Attribute \"" + attribute.getName()
                    + "\" already exists. Use method addOrChange(Attribute, Value) instead.");
        checkNulls(attribute, value);
        map.put(attribute, value);
    }

    /**
     * Adds a new attribute mapping to the specified <code>value</code>. Convenient version of
     * {@link #add(Attribute, Value)}.
     *
     * @param name the attribute name
     * @param value the attribute value
     * @see #add(Attribute, Value)
     * @see #addOrChange(String, Value)
     */
    public void add(String name, Value value) {
        add(new Attribute(name), value);
    }

    /**
     * Adds a new mapping to this map. Convenient version of {@link #add(Attribute, Value)}.
     *
     * @param entry a pair containing both an attribute and a value
     * @see #add(Attribute, Value)
     * @see #addOrChange(Entry)
     */
    public void add(Entry<Attribute, Value> entry) {
        add(entry.getKey(), entry.getValue());
    }

    /**
     * Adds all entries from another map to this map. This method uses {@link #add(Attribute, Value)}, so it throws an
     * exception when trying to overwrite an existing attribute.
     *
     * @param attributes the map to add
     * @see #add(Attribute, Value)
     * @see #addOrChange(AttributesMap)
     */
    public void add(AttributesMap attributes) {
        for(Entry<Attribute, Value> entry : attributes.map.entrySet())
            add(entry);
    }

    /**
     * Adds to this map a new <code>attribute</code> mapping to the specified <code>value</code>. Unlike
     * {@link #add(Attribute, Value)}, this method overwrites an existing attribute with the same name.
     *
     * @param attribute the attribute to add or overwrite
     * @param value the value for the <code>attribute</code>
     * @throws NullPointerException if either the <code>attribute</code> or the <code>value</code> is <code>null</code>;
     * for null value create a <code>Value</code> object containing <code>null</code>
     * @see #add(Attribute, Value)
     * @see #addOrChange(String, Value)
     * @see #addOrChange(Entry)
     * @see #addOrChange(AttributesMap)
     */
    public void addOrChange(Attribute attribute, Value value) {
        map.put(attribute, value);
        checkNulls(attribute, value);
    }

    /**
     * Adds a new attribute mapping to the specified <code>value</code> or overwrites an existing one. Convenient
     * version of {@link #addOrChange(Attribute, Value)}.
     *
     * @param name the attribute name
     * @param value the attribute value
     * @see #addOrChange(Attribute, Value)
     * @see #add(String, Value)
     */
    public void addOrChange(String name, Value value) {
        addOrChange(new Attribute(name), value);
    }

    /**
     * Adds a new mapping to this map or overwrites an existing one with the same attribute name. Convenient version of
     * {@link #addOrChange(Attribute, Value)}.
     *
     * @param entry a pair containing both an attribute and a value
     * @see #addOrChange(Attribute, Value)
     * @see #add(Entry)
     */
    public void addOrChange(Entry<Attribute, Value> entry) {
        addOrChange(entry.getKey(), entry.getValue());
    }

    /**
     * Adds all entries from another map to this map. If any attribute with the same name exists in this map, it will be
     * overwritten.
     *
     * @param attributes the map to add
     * @see #addOrChange(Attribute, Value)
     * @see #add(AttributesMap)
     */
    public void addOrChange(AttributesMap attributes) {
        for(Entry<Attribute, Value> entry : attributes.map.entrySet())
            addOrChange(entry);
    }

    private void checkAttribute(Attribute attribute) {
        if(attribute == null)
            throw new NullPointerException("The attribute cannot be null.");
    }

    /**
     * Gets the value mapped to the specified <code>attribute</code>. If such a mapping does not exist, this method throws
     * an exception. If this is not an expected behavior, use {@link #getOrNull(Attribute)} instead.
     *
     * @param attribute the attribute to obtain
     * @return the value mapped to <code>attribute</code>
     * @throws IllegalArgumentException if no value is mapped to <code>attribute</code>
     * @throws NullPointerException if <code>attribute</code> is <code>null</code>
     * @see #getOrNull(Attribute)
     * @see #get(String)
     */
    public Value get(Attribute attribute) {
        Value value = getOrNull(attribute);
        if(value == null)
            throw new IllegalArgumentException("Attribute " + attribute.getName()
                    + " does not exist. Use method getOrNull(Attribute) instead.");
        return value;
    }

    /**
     * Gets the value mapped to the specified attribute. Convenient version of {@link #get(Attribute)}.
     *
     * @param name name of the attribute
     * @return the value mapped to the specified attribute
     * @see #get(Attribute)
     * @see #getOrNull(String)
     */
    public Value get(String name) {
        return get(new Attribute(name));
    }

    /**
     * Gets the value mapped to the specified <code>attribute</code>. Unlike {@link #get(Attribute)}, this method
     * returns <code>null</code> if the requested mapping does not exist.
     *
     * @param attribute the attribute to obtain
     * @return the value mapped to <code>attribute</code> or <code>null</code> if it does not exist
     * @throws NullPointerException if the <code>attribute</code> is <code>null</code>
     * @see #get(Attribute)
     * @see #getOrNull(String)
     */
    public Value getOrNull(Attribute attribute) {
        checkAttribute(attribute);
        return map.get(attribute);
    }

    /**
     * Gets the value mapped to the specified attribute. Convenient version of {@link #getOrNull(Attribute)}.
     *
     * @param name name of the attribute
     * @return the value mapped to specified attribute or <code>null</code> if it does not exist
     * @see #getOrNull(Attribute)
     * @see #getOr(String)
     */
    public Value getOrNull(String name) {
        return getOrNull(new Attribute(name));
    }

    /**
     * Removes the specified <code>attribute</code> and its value from this map. If <code>attribute</code> does not
     * exist, this method doesn't do anything.
     *
     * @param attribute the attribute to remove
     * @throws NullPointerException if <code>attribute</code> is <code>null</code>
     * @see #remove(String)
     */
    public void remove(Attribute attribute) {
        checkAttribute(attribute);
        map.remove(attribute);
    }

    /**
     * Removes the specified attribute and its value from this map. Convenient version of {@link #remove(Attribute)}.
     *
     * @param name the name of the attribute to remove
     * @see #remove(Attribute)
     */
    public void remove(String name) {
        map.remove(new Attribute(name));
    }

    /**
     * Returns an iterator over all entries stored in this map.
     *
     * @return an iterator for this map
     * @see java.util.Iterator
     * @see java.lang.Iterable
     */
    @Override
    public Iterator<Entry<Attribute, Value>> iterator() {
        return map.entrySet().iterator();
    }

    /**
     * Creates a copy of this map. Since <code>Value</code> and <code>Attribute</code> are immutable classes, this
     * method does not clone them.
     *
     * @return a copy of this map containing identical entries
     */
    @Override
    public AttributesMap clone() {
        AttributesMap result = new AttributesMap();
        result.add(this);
        return result;
    }

    /**
     * Returns a string representation of this map listing all key-value pairs stored in it.
     *
     * @return a string representation of this object
     */
    @Override
    public String toString() {
        return map.toString();
    }
}