aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/android/util/ObservableKeyedArrayList.java
blob: 0be3c0056926f5efafa83e7c00adc4bef945b751 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright © 2018 Samuel Holland <samuel@sholland.org>
 * Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.util;

import android.databinding.ObservableArrayList;
import android.support.annotation.Nullable;

import com.wireguard.util.Keyed;

import java.util.Collection;
import java.util.ListIterator;
import java.util.Objects;

/**
 * ArrayList that allows looking up elements by some key property. As the key property must always
 * be retrievable, this list cannot hold {@code null} elements. Because this class places no
 * restrictions on the order or duplication of keys, lookup by key, as well as all list modification
 * operations, require O(n) time.
 */

public class ObservableKeyedArrayList<K, E extends Keyed<? extends K>>
        extends ObservableArrayList<E> implements ObservableKeyedList<K, E> {
    @Override
    public boolean add(@Nullable final E e) {
        if (e == null)
            throw new NullPointerException("Trying to add a null element");
        return super.add(e);
    }

    @Override
    public void add(final int index, @Nullable final E e) {
        if (e == null)
            throw new NullPointerException("Trying to add a null element");
        super.add(index, e);
    }

    @Override
    public boolean addAll(final Collection<? extends E> c) {
        if (c.contains(null))
            throw new NullPointerException("Trying to add a collection with null element(s)");
        return super.addAll(c);
    }

    @Override
    public boolean addAll(final int index, final Collection<? extends E> c) {
        if (c.contains(null))
            throw new NullPointerException("Trying to add a collection with null element(s)");
        return super.addAll(index, c);
    }

    @Override
    public boolean containsAllKeys(final Collection<K> keys) {
        for (final K key : keys)
            if (!containsKey(key))
                return false;
        return true;
    }

    @Override
    public boolean containsKey(final K key) {
        return indexOfKey(key) >= 0;
    }

    @Override @Nullable
    public E get(final K key) {
        final int index = indexOfKey(key);
        return index >= 0 ? get(index) : null;
    }

    @Override @Nullable
    public E getLast(final K key) {
        final int index = lastIndexOfKey(key);
        return index >= 0 ? get(index) : null;
    }

    @Override
    public int indexOfKey(final K key) {
        final ListIterator<E> iterator = listIterator();
        while (iterator.hasNext()) {
            final int index = iterator.nextIndex();
            if (Objects.equals(iterator.next().getKey(), key))
                return index;
        }
        return -1;
    }

    @Override
    public int lastIndexOfKey(final K key) {
        final ListIterator<E> iterator = listIterator(size());
        while (iterator.hasPrevious()) {
            final int index = iterator.previousIndex();
            if (Objects.equals(iterator.previous().getKey(), key))
                return index;
        }
        return -1;
    }

    @Override
    public E set(final int index, @Nullable final E e) {
        if (e == null)
            throw new NullPointerException("Trying to set a null key");
        return super.set(index, e);
    }
}