forked from jboss-developer/ticket-monster
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a circular buffer for storing the log. Display start/stop log mes…
…sages.
- Loading branch information
1 parent
d87b4ad
commit 26ac070
Showing
5 changed files
with
62 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
demo/src/main/java/org/jboss/jdf/example/ticketmonster/util/CircularBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.jboss.jdf.example.ticketmonster.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CircularBuffer<T> { | ||
|
||
private List<T> buffer; | ||
|
||
private volatile int cursor = 0; | ||
|
||
private final int capacity; | ||
|
||
public CircularBuffer(int capacity) { | ||
buffer = Collections.synchronizedList(new ArrayList<T>()); | ||
this.capacity = capacity; | ||
} | ||
|
||
public void add(T item) { | ||
synchronized (buffer) { | ||
buffer.add (cursor++,item); | ||
} | ||
} | ||
|
||
public List<T> getContents() { | ||
synchronized (buffer) { | ||
if (cursor > capacity) { | ||
List<T> returnedItems = buffer.subList(cursor % capacity, capacity); | ||
returnedItems.addAll(buffer.subList(0, cursor % capacity)); | ||
return returnedItems; | ||
} else { | ||
return buffer.subList(0, cursor); | ||
} | ||
} | ||
} | ||
} |