|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present, RxJava Contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.reactivex.internal.util; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tracks the current underlying array size in a volatile field. |
| 21 | + * |
| 22 | + * @param <T> the element type |
| 23 | + * @since 2.0.7 |
| 24 | + */ |
| 25 | +public final class VolatileSizeArrayList<T> extends AtomicInteger implements List<T> { |
| 26 | + |
| 27 | + private static final long serialVersionUID = 3972397474470203923L; |
| 28 | + |
| 29 | + final ArrayList<T> list; |
| 30 | + |
| 31 | + public VolatileSizeArrayList() { |
| 32 | + list = new ArrayList<T>(); |
| 33 | + } |
| 34 | + |
| 35 | + public VolatileSizeArrayList(int initialCapacity) { |
| 36 | + list = new ArrayList<T>(initialCapacity); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public int size() { |
| 41 | + return get(); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean isEmpty() { |
| 46 | + return get() == 0; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean contains(Object o) { |
| 51 | + return list.contains(o); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Iterator<T> iterator() { |
| 56 | + return list.iterator(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Object[] toArray() { |
| 61 | + return list.toArray(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public <E> E[] toArray(E[] a) { |
| 66 | + return list.toArray(a); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean add(T e) { |
| 71 | + boolean b = list.add(e); |
| 72 | + lazySet(list.size()); |
| 73 | + return b; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean remove(Object o) { |
| 78 | + boolean b = list.remove(o); |
| 79 | + lazySet(list.size()); |
| 80 | + return b; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean containsAll(Collection<?> c) { |
| 85 | + return list.containsAll(c); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean addAll(Collection<? extends T> c) { |
| 90 | + boolean b = list.addAll(c); |
| 91 | + lazySet(list.size()); |
| 92 | + return b; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean addAll(int index, Collection<? extends T> c) { |
| 97 | + boolean b = list.addAll(index, c); |
| 98 | + lazySet(list.size()); |
| 99 | + return b; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean removeAll(Collection<?> c) { |
| 104 | + boolean b = list.removeAll(c); |
| 105 | + lazySet(list.size()); |
| 106 | + return b; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean retainAll(Collection<?> c) { |
| 111 | + boolean b = list.retainAll(c); |
| 112 | + lazySet(list.size()); |
| 113 | + return b; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void clear() { |
| 118 | + list.clear(); |
| 119 | + lazySet(0); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public T get(int index) { |
| 124 | + return list.get(index); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public T set(int index, T element) { |
| 129 | + return list.set(index, element); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void add(int index, T element) { |
| 134 | + list.add(index, element); |
| 135 | + lazySet(list.size()); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public T remove(int index) { |
| 140 | + T v = list.remove(index); |
| 141 | + lazySet(list.size()); |
| 142 | + return v; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public int indexOf(Object o) { |
| 147 | + return list.indexOf(o); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int lastIndexOf(Object o) { |
| 152 | + return list.lastIndexOf(o); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public ListIterator<T> listIterator() { |
| 157 | + return list.listIterator(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public ListIterator<T> listIterator(int index) { |
| 162 | + return list.listIterator(index); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public List<T> subList(int fromIndex, int toIndex) { |
| 167 | + return list.subList(fromIndex, toIndex); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean equals(Object obj) { |
| 172 | + if (obj instanceof VolatileSizeArrayList) { |
| 173 | + return list.equals(((VolatileSizeArrayList<?>)obj).list); |
| 174 | + } |
| 175 | + return list.equals(obj); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public int hashCode() { |
| 180 | + return list.hashCode(); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public String toString() { |
| 185 | + return list.toString(); |
| 186 | + } |
| 187 | +} |
0 commit comments