ObservableArrayMap.java revision 5cc4e7270573c2c1a101fdade15287b405cf7d9c
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 182c86cdbaf189e2b1774af7f64a2974de9321673fGeorge Mountimport android.binding.ObservableMap; 192c86cdbaf189e2b1774af7f64a2974de9321673fGeorge Mountimport android.binding.OnMapChangedListener; 205cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport android.support.v4.util.ArrayMap; 215cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport android.support.v4.util.SimpleArrayMap; 225cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 235cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.Collection; 245cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.Map; 255cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 265cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountpublic class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements ObservableMap<K, V> { 275cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 285cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount private MapChangeRegistry mListeners; 295cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 305cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 315cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public void addOnMapChangedListener( 325cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount OnMapChangedListener<? extends ObservableMap<K, V>, K> listener) { 335cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (mListeners == null) { 345cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount mListeners = new MapChangeRegistry(); 355cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 365cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount mListeners.add(listener); 375cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 385cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 395cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 405cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public void removeOnMapChangedListener( 415cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount OnMapChangedListener<? extends ObservableMap<K, V>, K> listener) { 425cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (mListeners != null) { 435cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount mListeners.remove(listener); 445cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 455cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 465cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 475cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 485cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public void clear() { 495cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount boolean wasEmpty = isEmpty(); 505cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount if (!wasEmpty) { 515cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount super.clear(); 525cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount notifyChange(null); 535cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount } 545cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 555cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 565cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public V put(K k, V v) { 575cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount V val = super.put(k, v); 585cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount notifyChange(k); 595cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount return v; 605cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 615cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 625cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 635cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public boolean removeAll(Collection<?> collection) { 645cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount boolean removed = false; 655cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount for (Object key : collection) { 665cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount int index = indexOfKey(key); 675cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (index >= 0) { 685cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount removed = true; 695cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount removeAt(index); 705cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 715cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 725cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount return removed; 735cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 745cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 755cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 765cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public boolean retainAll(Collection<?> collection) { 775cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount boolean removed = false; 785cc4e7270573c2c1a101fdade15287b405cf7d9cGeorge Mount for (int i = size() - 1; i >= 0; i--) { 795cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount Object key = keyAt(i); 805cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (!collection.contains(key)) { 815cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount removeAt(i); 825cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount removed = true; 835cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 845cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 855cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount return removed; 865cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 875cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 885cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 895cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public V removeAt(int index) { 905cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount K key = keyAt(index); 915cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount V value = super.removeAt(index); 925cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (value != null) { 935cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount notifyChange(key); 945cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 955cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount return value; 965cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 975cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 985cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount @Override 995cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount public V setValueAt(int index, V value) { 1005cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount K key = keyAt(index); 1015cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount V oldValue = super.setValueAt(index, value); 1025cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount notifyChange(key); 1035cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount return oldValue; 1045cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 1055cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount 1065cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount private void notifyChange(Object key) { 1075cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount if (mListeners != null) { 1085cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount mListeners.notifyCallbacks(this, 0, key); 1095cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 1105cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount } 1115cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount} 112