1e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal/*
2e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * Copyright (C) 2015 The Android Open Source Project
3e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal *
4e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * you may not use this file except in compliance with the License.
6e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * You may obtain a copy of the License at
7e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal *
8e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal *
10e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * Unless required by applicable law or agreed to in writing, software
11e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * See the License for the specific language governing permissions and
14e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * limitations under the License.
15e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal */
16e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
17e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyalpackage com.android.launcher3.util;
18e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
19e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyalimport android.util.LongSparseArray;
20e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
21e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyalimport java.util.Iterator;
22e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
23e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal/**
24e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal * Extension of {@link LongSparseArray} with some utility methods.
25e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal */
26e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyalpublic class LongArrayMap<E> extends LongSparseArray<E> implements Iterable<E> {
27e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
28e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    public boolean containsKey(long key) {
29e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        return indexOfKey(key) >= 0;
30e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    }
31e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
32e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    public boolean isEmpty() {
33e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        return size() <= 0;
34e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    }
35e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
36e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    @Override
37e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    public LongArrayMap<E> clone() {
38e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        return (LongArrayMap<E>) super.clone();
39e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    }
40e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
41e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    @Override
42e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    public Iterator<E> iterator() {
43e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        return new ValueIterator();
44e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    }
45e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
46316490e636aad788fcfbfc2e04dd4f0e145bdd00Sunny Goyal    @Thunk class ValueIterator implements Iterator<E> {
47e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
48e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        private int mNextIndex = 0;
49e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
50e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        @Override
51e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        public boolean hasNext() {
52e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            return mNextIndex < size();
53e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        }
54e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
55e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        @Override
56e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        public E next() {
57e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            return valueAt(mNextIndex ++);
58e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        }
59e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal
60e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        @Override
61e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        public void remove() {
62e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal            throw new UnsupportedOperationException();
63e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal        }
64e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal    }
65e2df0620c13b9dc7e63224153b98fdbb48546f9bSunny Goyal}
66