Skip to content

IndexedRingBuffer.getInstance can infer type #2864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main/java/rx/internal/util/IndexedRingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@
*/
public final class IndexedRingBuffer<E> implements Subscription {

private static final ObjectPool<IndexedRingBuffer> POOL = new ObjectPool<IndexedRingBuffer>() {
private static final ObjectPool<IndexedRingBuffer<?>> POOL = new ObjectPool<IndexedRingBuffer<?>>() {

@Override
protected IndexedRingBuffer createObject() {
return new IndexedRingBuffer();
protected IndexedRingBuffer<?> createObject() {
return new IndexedRingBuffer<Object>();
}

};

public final static IndexedRingBuffer getInstance() {
return POOL.borrowObject();
@SuppressWarnings("unchecked")
public final static <T> IndexedRingBuffer<T> getInstance() {
return (IndexedRingBuffer<T>) POOL.borrowObject();
}

private final ElementSection<E> elements = new ElementSection<E>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
*/
public final class SubscriptionIndexedRingBuffer<T extends Subscription> implements Subscription {

@SuppressWarnings("unchecked")
private volatile IndexedRingBuffer<T> subscriptions = IndexedRingBuffer.getInstance();
private volatile int unsubscribed = 0;
@SuppressWarnings("rawtypes")
Expand Down
2 changes: 0 additions & 2 deletions src/perf/java/rx/internal/IndexedRingBufferPerf.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class IndexedRingBufferPerf {

@Benchmark
public void indexedRingBufferAdd(IndexedRingBufferInput input) throws InterruptedException, MissingBackpressureException {
@SuppressWarnings("unchecked")
IndexedRingBuffer<Integer> list = IndexedRingBuffer.getInstance();
for (int i = 0; i < input.size; i++) {
list.add(i);
Expand All @@ -40,7 +39,6 @@ public void indexedRingBufferAdd(IndexedRingBufferInput input) throws Interrupte

@Benchmark
public void indexedRingBufferAddRemove(IndexedRingBufferInput input) throws InterruptedException, MissingBackpressureException {
@SuppressWarnings("unchecked")
IndexedRingBuffer<Integer> list = IndexedRingBuffer.getInstance();
for (int i = 0; i < input.size; i++) {
list.add(i);
Expand Down
12 changes: 1 addition & 11 deletions src/test/java/rx/internal/util/IndexedRingBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class IndexedRingBufferTest {

@Test
public void add() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<LSubscription> list = IndexedRingBuffer.getInstance();
list.add(new LSubscription(1));
list.add(new LSubscription(2));
Expand All @@ -49,7 +48,6 @@ public void add() {

@Test
public void removeEnd() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<LSubscription> list = IndexedRingBuffer.getInstance();
list.add(new LSubscription(1));
int n2 = list.add(new LSubscription(2));
Expand All @@ -67,7 +65,6 @@ public void removeEnd() {

@Test
public void removeMiddle() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<LSubscription> list = IndexedRingBuffer.getInstance();
list.add(new LSubscription(1));
int n2 = list.add(new LSubscription(2));
Expand All @@ -82,7 +79,6 @@ public void removeMiddle() {

@Test
public void addRemoveAdd() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<String> list = IndexedRingBuffer.getInstance();
list.add("one");
list.add("two");
Expand Down Expand Up @@ -119,7 +115,6 @@ public void addRemoveAdd() {
@Test
public void addThousands() {
String s = "s";
@SuppressWarnings("unchecked")
IndexedRingBuffer<String> list = IndexedRingBuffer.getInstance();
for (int i = 0; i < 10000; i++) {
list.add(s);
Expand All @@ -145,7 +140,6 @@ public void addThousands() {

@Test
public void testForEachWithIndex() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<String> buffer = IndexedRingBuffer.getInstance();
buffer.add("zero");
buffer.add("one");
Expand Down Expand Up @@ -212,7 +206,6 @@ public Boolean call(String t1) {

@Test
public void testForEachAcrossSections() {
@SuppressWarnings("unchecked")
IndexedRingBuffer<Integer> buffer = IndexedRingBuffer.getInstance();
for (int i = 0; i < 10000; i++) {
buffer.add(i);
Expand All @@ -231,7 +224,6 @@ public void testForEachAcrossSections() {
@Test
public void longRunningAddRemoveAddDoesntLeakMemory() {
String s = "s";
@SuppressWarnings("unchecked")
IndexedRingBuffer<String> list = IndexedRingBuffer.getInstance();
for (int i = 0; i < 20000; i++) {
int index = list.add(s);
Expand All @@ -242,14 +234,13 @@ public void longRunningAddRemoveAddDoesntLeakMemory() {
list.forEach(newCounterAction(c));
assertEquals(0, c.get());
// System.out.println("Index is: " + list.index.get() + " when it should be no bigger than " + list.SIZE);
assertTrue(list.index.get() < list.SIZE);
assertTrue(list.index.get() < IndexedRingBuffer.SIZE);
// it should actually be 1 since we only did add/remove sequentially
assertEquals(1, list.index.get());
}

@Test
public void testConcurrentAdds() throws InterruptedException {
@SuppressWarnings("unchecked")
final IndexedRingBuffer<Integer> list = IndexedRingBuffer.getInstance();

Scheduler.Worker w1 = Schedulers.computation().createWorker();
Expand Down Expand Up @@ -300,7 +291,6 @@ public void call() {

@Test
public void testConcurrentAddAndRemoves() throws InterruptedException {
@SuppressWarnings("unchecked")
final IndexedRingBuffer<Integer> list = IndexedRingBuffer.getInstance();

final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());
Expand Down