summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java
blob: 0a1f87da000320d68d95e15d7dc38d134ee8a3d7 (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
package com.p4square.ccbapi.model;

import java.util.*;

/**
 * A collection of CustomField derivatives with indexes to find a custom field and value by either name or label.
 *
 * This collection will only ever contain one value for any given name or label.
 */
public class CustomFieldCollection<T extends CustomField> implements Collection<T> {

    private final List<T> values;
    private final Map<String, T> fieldLabelToValue;
    private final Map<String, T> fieldNameToValue;

    public CustomFieldCollection() {
        this.values = new ArrayList<>();
        this.fieldLabelToValue = new HashMap<>();
        this.fieldNameToValue = new HashMap<>();
    }

    /**
     * Return the entry with given name (e.g. "udf_text_1").
     *
     * @param name A CCB field name.
     * @return The entry associated with the field.
     */
    public T getByName(final String name) {
        return fieldNameToValue.get(name);
    }

    /**
     * Return the entry with given label (e.g. "Favorite Book").
     *
     * @param label A CCB field label.
     * @return The entry associated with the field.
     */
    public T getByLabel(final String label) {
        return fieldLabelToValue.get(label);
    }

    public List<T> asList() {
        return Collections.unmodifiableList(values);
    }

    @Override
    public int size() {
        return values.size();
    }

    @Override
    public boolean isEmpty() {
        return values.isEmpty();
    }

    @Override
    public boolean contains(final Object o) {
        return values.contains(o);
    }

    @Override
    public Iterator<T> iterator() {
        return values.iterator();
    }

    @Override
    public Object[] toArray() {
        return values.toArray();
    }

    @Override
    public <T1> T1[] toArray(final T1[] a) {
        return values.toArray(a);
    }

    @Override
    public boolean add(final T t) {
        // Clean up overwritten indexes.
        final T previousValueByName = fieldNameToValue.get(t.getName());
        if (previousValueByName != null) {
            remove(previousValueByName);
        }

        final T previousValueByLabel = fieldLabelToValue.get(t.getLabel());
        if (previousValueByLabel != null) {
            remove(previousValueByLabel);
        }

        fieldNameToValue.put(t.getName(), t);
        fieldLabelToValue.put(t.getLabel(), t);
        return values.add(t);
    }

    @Override
    public boolean remove(final Object o) {
        if (values.remove(o)) {
            final T entry = (T) o;
            fieldNameToValue.remove(entry.getName());
            fieldLabelToValue.remove(entry.getLabel());
            return true;
        }
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return values.containsAll(c);
    }

    @Override
    public boolean addAll(final Collection<? extends T> c) {
        boolean result = false;
        for (T obj : c) {
            result |= add(obj);
        }
        return result;
    }

    @Override
    public boolean removeAll(final Collection<?> c) {
        boolean result = false;
        for (Object obj : c) {
            result |= remove(obj);
        }
        return result;
    }

    @Override
    public boolean retainAll(final Collection<?> c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        values.clear();
    }
}