From b9eb1329a6dbec7b75c21d8e0eb13134121db6bb Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 19 Mar 2016 02:05:33 -0700 Subject: Initial commit for the CCB API Client. The client currently supports the following APIs: * individual_profiles * individual_profile_from_id * individual_profile_from_login_password * individual_profile_from_micr * custom_field_labels --- .../ccbapi/model/CustomFieldCollection.java | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java (limited to 'src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java') diff --git a/src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java b/src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java new file mode 100644 index 0000000..0a1f87d --- /dev/null +++ b/src/main/java/com/p4square/ccbapi/model/CustomFieldCollection.java @@ -0,0 +1,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 implements Collection { + + private final List values; + private final Map fieldLabelToValue; + private final Map 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 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 iterator() { + return values.iterator(); + } + + @Override + public Object[] toArray() { + return values.toArray(); + } + + @Override + public 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 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(); + } +} -- cgit v1.2.3