[Chromecast] Remove CircularBuffer.
This is no longer used. Bug: None Test: build cast_shell_apk, run junit tests Change-Id: I13f98d7b6c3b03d3d76bc5578e7436d854b0153d Reviewed-on: https://chromium-review.googlesource.com/1236281 Reviewed-by: Luke Halliwell <halliwell@chromium.org> Commit-Queue: Simeon Anfinrud <sanfin@chromium.org> Cr-Commit-Position: refs/heads/master@{#594089}
This commit is contained in:

committed by
Commit Bot

parent
a9df9ee91c
commit
9ed6012945
chromecast/base
BUILD.gn
java
@ -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",
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user