summaryrefslogtreecommitdiff
path: root/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java
blob: bdf2ce81218b9acea4c0b8072e99f7b68d879fe3 (about) (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
package au.id.zancanaro.javacheck.object;

import java.util.*;

public class SubObject<T> {
    public final Map<String, List<T>> obj;

    public SubObject(Map<String, List<T>> obj) {
        this.obj = obj;
    }

    public SubObject<T> add(SubObject<T> other) {
        Map<String, List<T>> values = new HashMap<>(obj);
        for (Map.Entry<String, List<T>> entry : other.obj.entrySet()) {
            String key = entry.getKey();
            if (values.containsKey(key)) {
                List<T> result = new ArrayList<>(values.get(key));
                result.addAll(entry.getValue());
                values.put(key, Collections.unmodifiableList(result));
            } else {
                values.put(key, entry.getValue());
            }
        }
        return new SubObject<>(Collections.unmodifiableMap(values));
    }

    @Override
    public String toString() {
        return obj.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SubObject subObject = (SubObject) o;
        return obj.equals(subObject.obj);
    }

    @Override
    public int hashCode() {
        return obj.hashCode();
    }
}