summaryrefslogtreecommitdiff
path: root/src/test/java/au/id/zancanaro/javacheck/object/SubObject.java
blob: 11e5622cf8e68c96e02de66b49607cd4bfbfdb3d (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
45
46
47
48
49
50
package au.id.zancanaro.javacheck.object;

import java.util.*;

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

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

    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), this.value);
    }

    @Override
    public String toString() {
        return "{" + obj.toString() + ", " + this.value + "}";
    }

    @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) && value.equals(subObject.value);
    }

    @Override
    public int hashCode() {
        return obj.hashCode() + 37 * value.hashCode();
    }

    public static enum EnumValues {
        A, B, C
    }
}