public class

ObservableArrayMap<K, V>

extends ArrayMap<java.lang.Object, java.lang.Object>

implements ObservableMap<java.lang.Object, java.lang.Object>

 java.lang.Object

androidx.collection.SimpleArrayMap<java.lang.Object, java.lang.Object>

androidx.collection.ArrayMap<java.lang.Object, java.lang.Object>

↳androidx.databinding.ObservableArrayMap<K, V>

Gradle dependencies

compile group: 'androidx.databinding', name: 'library', version: '3.2.0-alpha11'

  • groupId: androidx.databinding
  • artifactId: library
  • version: 3.2.0-alpha11

Artifact androidx.databinding:library:3.2.0-alpha11 it located at Google repository (https://maven.google.com/)

Androidx class mapping:

androidx.databinding.ObservableArrayMap android.databinding.ObservableArrayMap

Summary

Constructors
publicObservableArrayMap()

Methods
public voidaddOnMapChangedCallback(ObservableMap.OnMapChangedCallback<ObservableMap, java.lang.Object, java.lang.Object> listener)

public voidclear()

Make the array map empty.

public java.lang.Objectput(java.lang.Object key, java.lang.Object value)

Add a new value to the array map.

public booleanremoveAll(java.util.Collection<java.lang.Object> collection)

Remove all keys in the array map that exist in the given collection.

public java.lang.ObjectremoveAt(int index)

Remove the key/value mapping at the given index.

public voidremoveOnMapChangedCallback(ObservableMap.OnMapChangedCallback<ObservableMap, java.lang.Object, java.lang.Object> listener)

public booleanretainAll(java.util.Collection<java.lang.Object> collection)

Remove all keys in the array map that do not exist in the given collection.

public java.lang.ObjectsetValueAt(int index, java.lang.Object value)

Set the value at a given index in the array.

from ArrayMap<K, V>containsAll, entrySet, keySet, putAll, values
from SimpleArrayMap<K, V>containsKey, containsValue, ensureCapacity, equals, get, getOrDefault, hashCode, indexOfKey, isEmpty, keyAt, putAll, putIfAbsent, remove, remove, replace, replace, size, toString, valueAt
from java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait

Constructors

public ObservableArrayMap()

Methods

public void addOnMapChangedCallback(ObservableMap.OnMapChangedCallback<ObservableMap, java.lang.Object, java.lang.Object> listener)

public void removeOnMapChangedCallback(ObservableMap.OnMapChangedCallback<ObservableMap, java.lang.Object, java.lang.Object> listener)

public void clear()

Make the array map empty. All storage is released.

public java.lang.Object put(java.lang.Object key, java.lang.Object value)

Add a new value to the array map.

Parameters:

key: The key under which to store the value. Must not be null. If this key already exists in the array, its value will be replaced.
value: The value to store for the given key.

Returns:

Returns the old value that was stored for the given key, or null if there was no such key.

public boolean removeAll(java.util.Collection<java.lang.Object> collection)

Remove all keys in the array map that exist in the given collection.

Parameters:

collection: The collection whose contents are to be used to remove keys.

Returns:

Returns true if any keys were removed from the array map, else false.

public boolean retainAll(java.util.Collection<java.lang.Object> collection)

Remove all keys in the array map that do not exist in the given collection.

Parameters:

collection: The collection whose contents are to be used to determine which keys to keep.

Returns:

Returns true if any keys were removed from the array map, else false.

public java.lang.Object removeAt(int index)

Remove the key/value mapping at the given index.

Parameters:

index: The desired index, must be between 0 and SimpleArrayMap.size()-1.

Returns:

Returns the value that was stored at this index.

public java.lang.Object setValueAt(int index, java.lang.Object value)

Set the value at a given index in the array.

Parameters:

index: The desired index, must be between 0 and SimpleArrayMap.size()-1.
value: The new value to store at this index.

Returns:

Returns the previous value at the given index.

Source

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.databinding;

import java.util.Collection;

import androidx.collection.ArrayMap;

public class ObservableArrayMap<K, V> extends ArrayMap<K, V> implements ObservableMap<K, V> {

    private transient MapChangeRegistry mListeners;

    @Override
    public void addOnMapChangedCallback(
            OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) {
        if (mListeners == null) {
            mListeners = new MapChangeRegistry();
        }
        mListeners.add(listener);
    }

    @Override
    public void removeOnMapChangedCallback(
            OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) {
        if (mListeners != null) {
            mListeners.remove(listener);
        }
    }

    @Override
    public void clear() {
        boolean wasEmpty = isEmpty();
        if (!wasEmpty) {
            super.clear();
            notifyChange(null);
        }
    }

    public V put(K k, V v) {
        V val = super.put(k, v);
        notifyChange(k);
        return v;
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        boolean removed = false;
        for (Object key : collection) {
            int index = indexOfKey(key);
            if (index >= 0) {
                removed = true;
                removeAt(index);
            }
        }
        return removed;
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        boolean removed = false;
        for (int i = size() - 1; i >= 0; i--) {
            Object key = keyAt(i);
            if (!collection.contains(key)) {
                removeAt(i);
                removed = true;
            }
        }
        return removed;
    }

    @Override
    public V removeAt(int index) {
        K key = keyAt(index);
        V value = super.removeAt(index);
        if (value != null) {
            notifyChange(key);
        }
        return value;
    }

    @Override
    public V setValueAt(int index, V value) {
        K key = keyAt(index);
        V oldValue = super.setValueAt(index, value);
        notifyChange(key);
        return oldValue;
    }

    private void notifyChange(Object key) {
        if (mListeners != null) {
            mListeners.notifyCallbacks(this, 0, key);
        }
    }
}