ObservableArrayMap.java revision 5cd681c345db8f606d7d5a8662e20e059f21a86c
15cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount/*
25cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * Copyright (C) 2015 The Android Open Source Project
35cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount *
45cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License");
55cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * you may not use this file except in compliance with the License.
65cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * You may obtain a copy of the License at
75cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount *
85cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount *      http://www.apache.org/licenses/LICENSE-2.0
95cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount *
105cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * Unless required by applicable law or agreed to in writing, software
115cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS,
125cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * See the License for the specific language governing permissions and
145cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount * limitations under the License.
155cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount */
165cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountpackage com.android.databinding.library;
175cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
185cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport android.support.v4.util.ArrayMap;
195cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport android.support.v4.util.SimpleArrayMap;
205cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
215cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.Collection;
225cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.Map;
235cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
245cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountpublic class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements ObservableMap<K, V> {
255cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
265cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    private MapChangeRegistry mListeners;
275cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
285cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
295cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void addOnMapChangedListener(
305cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            OnMapChangedListener<? extends ObservableMap<K, V>, K> listener) {
315cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners == null) {
325cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners = new MapChangeRegistry();
335cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
345cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        mListeners.add(listener);
355cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
365cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
375cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
385cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void removeOnMapChangedListener(
395cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            OnMapChangedListener<? extends ObservableMap<K, V>, K> listener) {
405cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
415cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.remove(listener);
425cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
435cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
445cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
455cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
465cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void clear() {
475cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyChange(null);
485cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
495cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
505cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public V put(K k, V v) {
515cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        V val = super.put(k, v);
525cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyChange(k);
535cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return v;
545cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
555cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
565cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
575cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void putAll(Map<? extends K, ? extends V> map) {
585cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.putAll(map);
595cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        for (K key : map.keySet()) {
605cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyChange(key);
615cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
625cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
635cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
645cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
655cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public V remove(Object o) {
665cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        V val = super.remove(o);
675cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyChange(o);
685cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return val;
695cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
705cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
715cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
725cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean removeAll(Collection<?> collection) {
735cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        boolean removed = false;
745cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        for (Object key : collection) {
755cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            int index = indexOfKey(key);
765cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            if (index >= 0) {
775cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount                removed = true;
785cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount                removeAt(index);
795cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            }
805cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
815cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return removed;
825cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
835cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
845cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
855cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean retainAll(Collection<?> collection) {
865cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        boolean removed = false;
875cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        for (int i = size(); i >= 0; i--) {
885cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            Object key = keyAt(i);
895cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            if (!collection.contains(key)) {
905cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount                removeAt(i);
915cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount                removed = true;
925cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            }
935cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
945cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return removed;
955cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
965cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
975cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
985cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void putAll(SimpleArrayMap<? extends K, ? extends V> array) {
995cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.putAll(array);
1005cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        for (int i = array.size(); i >= 0; i--) {
1015cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            K key = array.keyAt(i);
1025cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyChange(key);
1035cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1045cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1055cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1065cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
1075cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public V removeAt(int index) {
1085cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        K key = keyAt(index);
1095cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        V value = super.removeAt(index);
1105cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (value != null) {
1115cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyChange(key);
1125cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1135cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return value;
1145cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1155cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1165cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
1175cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public V setValueAt(int index, V value) {
1185cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        K key = keyAt(index);
1195cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        V oldValue = super.setValueAt(index, value);
1205cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyChange(key);
1215cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return oldValue;
1225cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1235cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1245cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    private void notifyChange(Object key) {
1255cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
1265cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.notifyCallbacks(this, 0, key);
1275cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1285cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1295cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount}
130