Skip to content

Commit 0098013

Browse files
author
Arpit Bhardwaj
committed
Initial commit
0 parents  commit 0098013

5 files changed

Lines changed: 128 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Project exclude paths
2+
/target/

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>groupId</groupId>
8+
<artifactId>Generics</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.11</version>
16+
</dependency>
17+
</dependencies>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.7.0</version>
25+
<configuration>
26+
<source>1.8</source>
27+
<target>1.8</target>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ab.generics.basic;
2+
3+
public class CircularBuffer {
4+
private Object[] buffer;
5+
private int readCursor = 0;
6+
private int writeCursor = 0;
7+
8+
public CircularBuffer(int size) {
9+
this.buffer = new Object[size];
10+
}
11+
12+
public boolean offer(Object value){
13+
if(buffer[writeCursor] != null){
14+
return false;
15+
}
16+
buffer[writeCursor] = value;
17+
writeCursor = next(writeCursor);
18+
return true;
19+
}
20+
21+
public Object poll(){
22+
Object value = buffer[readCursor];
23+
if(value != null){
24+
buffer[readCursor] = null;
25+
readCursor = next(readCursor);
26+
}
27+
return value;
28+
}
29+
30+
public int next(int index){
31+
return (index+1)%buffer.length;
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ab.generics.basic;
2+
3+
public class TypeUnsafeExample {
4+
public static void main(String[] args) {
5+
CircularBuffer buffer = new CircularBuffer(10);
6+
buffer.offer("a");
7+
buffer.offer("bc");
8+
buffer.offer("d");
9+
10+
String value = concatenate(buffer);
11+
System.out.println(value);
12+
}
13+
14+
private static String concatenate(CircularBuffer buffer) {
15+
StringBuilder result = new StringBuilder();
16+
String value;
17+
while((value = (String) buffer.poll()) != null){
18+
result.append(value);
19+
}
20+
return result.toString();
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.ab.generics.basic;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class CircularBufferTest {
8+
9+
private CircularBuffer buffer = new CircularBuffer(2);
10+
11+
@Test
12+
public void shouldOfferPollableElement() {
13+
assertTrue(buffer.offer(1));
14+
assertEquals(1,buffer.poll());
15+
assertNull(buffer.poll());
16+
}
17+
18+
@Test
19+
public void shouldNotOfferWhenBufferIsFull() {
20+
assertTrue(buffer.offer(1));
21+
assertTrue(buffer.offer(2));
22+
assertFalse(buffer.offer(3));
23+
}
24+
@Test
25+
public void shouldNotPollWhenBufferIsEmpty(){
26+
assertNull(buffer.poll());
27+
}
28+
29+
@Test
30+
public void shouldRecycleBuffer(){
31+
assertTrue(buffer.offer(1));
32+
assertTrue(buffer.offer(2));
33+
assertEquals(1,buffer.poll());
34+
assertTrue(buffer.offer(3));
35+
assertEquals(2,buffer.poll());
36+
assertEquals(3,buffer.poll());
37+
}
38+
}

0 commit comments

Comments
 (0)