diff --git a/chromecast/base/BUILD.gn b/chromecast/base/BUILD.gn index 0432ad157109e..94d72550c3fe1 100644 --- a/chromecast/base/BUILD.gn +++ b/chromecast/base/BUILD.gn @@ -313,7 +313,6 @@ if (is_android) { "$java_src_dir/org/chromium/chromecast/base/BiFunction.java", "$java_src_dir/org/chromium/chromecast/base/BiPredicate.java", "$java_src_dir/org/chromium/chromecast/base/Both.java", - "$java_src_dir/org/chromium/chromecast/base/CircularBuffer.java", "$java_src_dir/org/chromium/chromecast/base/Controller.java", "$java_src_dir/org/chromium/chromecast/base/Consumer.java", "$java_src_dir/org/chromium/chromecast/base/Function.java", @@ -359,7 +358,6 @@ if (is_android) { java_files = [ "$java_test_dir/org/chromium/chromecast/base/BothTest.java", "$java_test_dir/org/chromium/chromecast/base/ControllerTest.java", - "$java_test_dir/org/chromium/chromecast/base/CircularBufferTest.java", "$java_test_dir/org/chromium/chromecast/base/ItertoolsTest.java", "$java_test_dir/org/chromium/chromecast/base/ObservableAndTest.java", "$java_test_dir/org/chromium/chromecast/base/ObservableAndThenTest.java", diff --git a/chromecast/base/java/src/org/chromium/chromecast/base/CircularBuffer.java b/chromecast/base/java/src/org/chromium/chromecast/base/CircularBuffer.java deleted file mode 100644 index 8e0c33d92d377..0000000000000 --- a/chromecast/base/java/src/org/chromium/chromecast/base/CircularBuffer.java +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2017 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.chromecast.base; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * An Iterable object that stores up to a fixed amount of objects, and overwrites the least-recently - * inserted object if it exceeds its capacity. Appending, removing, and iterating are all constant - * time. - * - * This class is intended as a fast Iterable Deque with a fixed capacity. LinkedList generates lint - * warnings because every item requires a heap allocation for the Node, ArrayList requires rewriting - * the list when the head is removed, and ArrayDeque is not fixed-size. - * - * Currently, the only supported use case is appending items to the buffer, and then iterating the - * buffer. Concurrent modification while iterating, or appending items after iterating, is - * undefined behavior as of now. - * - * @param <T> The type that elements of the buffer should be instances of. - */ -public class CircularBuffer<T> implements Iterable<T> { - private final List<T> mData; - private final int mSize; - private boolean mAtCapacity; - private int mHeadPosition; - private int mTailPosition; - - public CircularBuffer(int size) { - mData = new ArrayList<T>(size); - mSize = size; - mAtCapacity = false; - mHeadPosition = 0; - mTailPosition = 0; - } - - public void add(T item) { - if (mSize == 0) { - return; - } - if (mAtCapacity) { - mData.set(mHeadPosition, item); - mHeadPosition = increment(mHeadPosition); - } else { - mData.add(item); - } - mTailPosition = increment(mTailPosition); - if (mTailPosition == mHeadPosition) { - mAtCapacity = true; - } - } - - @Override - public Iterator<T> iterator() { - return new Iterator<T>() { - @Override - public boolean hasNext() { - return mAtCapacity || mHeadPosition != mTailPosition; - } - - @Override - public T next() { - T result = mData.get(mHeadPosition); - mHeadPosition = increment(mHeadPosition); - mAtCapacity = false; - return result; - } - }; - } - - private int increment(int position) { - return (position + 1) % mSize; - } -} diff --git a/chromecast/base/java/test/org/chromium/chromecast/base/CircularBufferTest.java b/chromecast/base/java/test/org/chromium/chromecast/base/CircularBufferTest.java deleted file mode 100644 index ec52b9bdb5dee..0000000000000 --- a/chromecast/base/java/test/org/chromium/chromecast/base/CircularBufferTest.java +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2017 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.chromecast.base; - -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.emptyIterable; -import static org.junit.Assert.assertThat; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.BlockJUnit4ClassRunner; - -/** - * Tests for CircularBuffer. - * - * Currently, the only supported use case is appending items to the buffer, and then iterating the - * buffer. Concurrent modification while iterating, or appending items after iterating, is - * undefined behavior as of now. - */ -@RunWith(BlockJUnit4ClassRunner.class) -public class CircularBufferTest { - @Test - public void testBufferWithNoCapacity() { - CircularBuffer<String> buffer = new CircularBuffer<>(0); - buffer.add("a"); - assertThat(buffer, emptyIterable()); - } - - @Test - public void testBufferWithPartialCapacity() { - CircularBuffer<String> buffer = new CircularBuffer<>(4); - buffer.add("a"); - buffer.add("b"); - buffer.add("c"); - assertThat(buffer, contains("a", "b", "c")); - } - - @Test - public void testBufferWithFullCapacity() { - CircularBuffer<String> buffer = new CircularBuffer<>(4); - buffer.add("zero"); - buffer.add("one"); - buffer.add("two"); - buffer.add("three"); - assertThat(buffer, contains("zero", "one", "two", "three")); - } - - @Test - public void testBufferThatOverflowsCapacityWrapsAroundAndErasesOldestElements() { - CircularBuffer<String> buffer = new CircularBuffer<>(4); - buffer.add("1"); - buffer.add("2"); - buffer.add("3"); - buffer.add("4"); // Hits capacity; subsequent additions overwrite oldest elements. - buffer.add("5"); // erases "1" - buffer.add("6"); // erases "2" - assertThat(buffer, contains("3", "4", "5", "6")); - } - - @Test - public void testBufferThatOverflowsTwice() { - CircularBuffer<String> buffer = new CircularBuffer<>(2); - buffer.add("a"); - buffer.add("b"); - buffer.add("c"); - buffer.add("d"); - buffer.add("e"); - // Since the capacity is 2, return the 2 most-recently added items. - assertThat(buffer, contains("d", "e")); - } -}