OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import java.util.ArrayList; | 7 import java.util.ArrayList; |
8 import java.util.Iterator; | 8 import java.util.Iterator; |
9 import java.util.List; | 9 import java.util.List; |
10 import java.util.NoSuchElementException; | 10 import java.util.NoSuchElementException; |
(...skipping 26 matching lines...) Expand all Loading... |
37 /** | 37 /** |
38 * Rewind the iterator back to the beginning. | 38 * Rewind the iterator back to the beginning. |
39 * | 39 * |
40 * If we need to iterate multiple times, we can avoid iterator object re
allocation by using | 40 * If we need to iterate multiple times, we can avoid iterator object re
allocation by using |
41 * this method. | 41 * this method. |
42 */ | 42 */ |
43 public void rewind(); | 43 public void rewind(); |
44 } | 44 } |
45 | 45 |
46 public final List<E> mObservers = new ArrayList<E>(); | 46 public final List<E> mObservers = new ArrayList<E>(); |
47 private int mIterationDepth = 0; | 47 private int mIterationDepth; |
48 private int mCount = 0; | 48 private int mCount; |
49 private boolean mNeedsCompact = false; | 49 private boolean mNeedsCompact; |
50 | 50 |
51 public ObserverList() {} | 51 public ObserverList() {} |
52 | 52 |
53 /** | 53 /** |
54 * Add an observer to the list. | 54 * Add an observer to the list. |
55 * <p/> | 55 * <p/> |
56 * An observer should not be added to the same list more than once. If an it
eration is already | 56 * An observer should not be added to the same list more than once. If an it
eration is already |
57 * in progress, this observer will be not be visible during that iteration. | 57 * in progress, this observer will be not be visible during that iteration. |
58 * | 58 * |
59 * @return true if the observer list changed as a result of the call. | 59 * @return true if the observer list changed as a result of the call. |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 private int capacity() { | 183 private int capacity() { |
184 return mObservers.size(); | 184 return mObservers.size(); |
185 } | 185 } |
186 | 186 |
187 private E getObserverAt(int index) { | 187 private E getObserverAt(int index) { |
188 return mObservers.get(index); | 188 return mObservers.get(index); |
189 } | 189 } |
190 | 190 |
191 private class ObserverListIterator implements RewindableIterator<E> { | 191 private class ObserverListIterator implements RewindableIterator<E> { |
192 private int mListEndMarker; | 192 private int mListEndMarker; |
193 private int mIndex = 0; | 193 private int mIndex; |
194 private boolean mIsExhausted = false; | 194 private boolean mIsExhausted; |
195 | 195 |
196 private ObserverListIterator() { | 196 private ObserverListIterator() { |
197 ObserverList.this.incrementIterationDepth(); | 197 ObserverList.this.incrementIterationDepth(); |
198 mListEndMarker = ObserverList.this.capacity(); | 198 mListEndMarker = ObserverList.this.capacity(); |
199 } | 199 } |
200 | 200 |
201 @Override | 201 @Override |
202 public void rewind() { | 202 public void rewind() { |
203 compactListIfNeeded(); | 203 compactListIfNeeded(); |
204 ObserverList.this.incrementIterationDepth(); | 204 ObserverList.this.incrementIterationDepth(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 } | 240 } |
241 | 241 |
242 private void compactListIfNeeded() { | 242 private void compactListIfNeeded() { |
243 if (!mIsExhausted) { | 243 if (!mIsExhausted) { |
244 mIsExhausted = true; | 244 mIsExhausted = true; |
245 ObserverList.this.decrementIterationDepthAndCompactIfNeeded(); | 245 ObserverList.this.decrementIterationDepthAndCompactIfNeeded(); |
246 } | 246 } |
247 } | 247 } |
248 } | 248 } |
249 } | 249 } |
OLD | NEW |