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 */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding;
172c86cdbaf189e2b1774af7f64a2974de9321673fGeorge Mount
185cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.ArrayList;
195cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountimport java.util.Collection;
205cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
21c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount/**
22c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * An {@link ObservableList} implementation using ArrayList as an implementation.
23c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount */
245cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mountpublic class ObservableArrayList<T> extends ArrayList<T> implements ObservableList<T> {
25d37b47419005587c52ca4c099432327b7f06478dYigit Boyar    private transient ListChangeRegistry mListeners = new ListChangeRegistry();
265cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
275cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
28722fe711207a37783dfa7142284b0ebe5bd503fbGeorge Mount    public void addOnListChangedCallback(OnListChangedCallback listener) {
295cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners == null) {
305cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners = new ListChangeRegistry();
315cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
325cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        mListeners.add(listener);
335cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
345cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
355cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
36722fe711207a37783dfa7142284b0ebe5bd503fbGeorge Mount    public void removeOnListChangedCallback(OnListChangedCallback listener) {
375cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
385cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.remove(listener);
395cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
405cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
415cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
425cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
435cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean add(T object) {
445cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.add(object);
455cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyAdd(size() - 1, 1);
465cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return true;
475cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
485cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
495cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
505cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void add(int index, T object) {
515cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.add(index, object);
525cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyAdd(index, 1);
535cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
545cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
555cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
565cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean addAll(Collection<? extends T> collection) {
575cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        int oldSize = size();
585cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        boolean added = super.addAll(collection);
595cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (added) {
605cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyAdd(oldSize, size() - oldSize);
615cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
625cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return added;
635cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
645cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
655cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
665cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean addAll(int index, Collection<? extends T> collection) {
675cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        boolean added = super.addAll(index, collection);
685cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (added) {
695cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyAdd(index, collection.size());
705cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
715cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return added;
725cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
735cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
745cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
755cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public void clear() {
765cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        int oldSize = size();
775cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.clear();
785cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (oldSize != 0) {
795cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            notifyRemove(0, oldSize);
805cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
815cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
825cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
835cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
845cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public T remove(int index) {
855cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        T val = super.remove(index);
865cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyRemove(index, 1);
875cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return val;
885cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
895cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
905cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
915cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public boolean remove(Object object) {
925cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        int index = indexOf(object);
935cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (index >= 0) {
945cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            remove(index);
955cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            return true;
965cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        } else {
975cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            return false;
985cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
995cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1005cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1015cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
1025cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    public T set(int index, T object) {
1035cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        T val = super.set(index, object);
1045cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
1055cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.notifyChanged(this, index, 1);
1065cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1075cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        return val;
1085cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1095cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1105cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    @Override
1115cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    protected void removeRange(int fromIndex, int toIndex) {
1125cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        super.removeRange(fromIndex, toIndex);
1135cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        notifyRemove(fromIndex, toIndex - fromIndex);
1145cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1155cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1165cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    private void notifyAdd(int start, int count) {
1175cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
1185cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.notifyInserted(this, start, count);
1195cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1205cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1215cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount
1225cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    private void notifyRemove(int start, int count) {
1235cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        if (mListeners != null) {
1245cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount            mListeners.notifyRemoved(this, start, count);
1255cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount        }
1265cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount    }
1275cd681c345db8f606d7d5a8662e20e059f21a86cGeorge Mount}
128