Add various utility classes
This commit is contained in:
40
java/base/src/main/java/base/util/ArrayCycle.java
Normal file
40
java/base/src/main/java/base/util/ArrayCycle.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package base.util;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class ArrayCycle<E> extends CopyOnWriteArrayList<E> {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
protected int index = 0;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ArrayCycle(E... elementArray) {
|
||||
if (elementArray != null) {
|
||||
for (E element : elementArray) {
|
||||
add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public E current() {
|
||||
return this.get(index);
|
||||
}
|
||||
|
||||
public synchronized E previous() {
|
||||
if (--index < 0) {
|
||||
index = Math.max(0, size() - 1);
|
||||
}
|
||||
return get(index);
|
||||
}
|
||||
|
||||
public synchronized E next() {
|
||||
if (++index >= size()) {
|
||||
index = 0;
|
||||
}
|
||||
return size() == 0 ? null : get(index);
|
||||
}
|
||||
|
||||
public E reset() {
|
||||
return get(index = 0);
|
||||
}
|
||||
}
|
||||
41
java/base/src/main/java/base/util/Buffer.java
Normal file
41
java/base/src/main/java/base/util/Buffer.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package base.util;
|
||||
|
||||
public class Buffer {
|
||||
protected byte[] elements;
|
||||
protected int capacity;
|
||||
protected int index;
|
||||
protected int size;
|
||||
|
||||
public Buffer(int capacity) {
|
||||
this.elements = new byte[capacity];
|
||||
this.capacity = capacity;
|
||||
index = 0;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public synchronized void add(byte... elements) {
|
||||
for (byte element : elements) {
|
||||
this.elements[index++ % capacity] = element;
|
||||
if (size < capacity) {
|
||||
++size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void write(byte[] elements, int offset, int length) {
|
||||
for (int i = offset; i < length; ++i) {
|
||||
this.elements[index++ % capacity] = elements[i];
|
||||
if (size < capacity) {
|
||||
++size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized byte[] get() {
|
||||
byte[] elements = new byte[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
elements[i] = this.elements[(index + i) % size];
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
6
java/base/src/main/java/base/util/Bufferable.java
Normal file
6
java/base/src/main/java/base/util/Bufferable.java
Normal file
@@ -0,0 +1,6 @@
|
||||
package base.util;
|
||||
|
||||
public interface Bufferable {
|
||||
public void load();
|
||||
public void unload();
|
||||
}
|
||||
74
java/base/src/main/java/base/util/BufferedArrayCycle.java
Normal file
74
java/base/src/main/java/base/util/BufferedArrayCycle.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package base.util;
|
||||
|
||||
public class BufferedArrayCycle<E extends Bufferable> extends ArrayCycle<E> {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
protected ArrayCycle<? extends Bufferable> buffer;
|
||||
protected int before;
|
||||
protected int after;
|
||||
protected int indexFirst;
|
||||
protected int indexLast;
|
||||
//protected int indexBuffer;
|
||||
//protected Bufferable[] bufferableArray;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BufferedArrayCycle(int before, int after) {
|
||||
this.before = before;
|
||||
this.after = after;
|
||||
indexFirst = 0;
|
||||
indexLast = 0;
|
||||
//bufferableArray = new Bufferable[before + after + 1];
|
||||
//buffer = new ArrayCycle<Bufferable>();
|
||||
|
||||
}
|
||||
|
||||
public E previous() {
|
||||
get(indexFirst).unload();
|
||||
indexFirst = previous(indexFirst);
|
||||
indexLast = previous(indexLast);
|
||||
get(indexLast).load();
|
||||
// eerste before weg
|
||||
|
||||
// eerste after wordt huidig
|
||||
|
||||
// voeg laatste after toe
|
||||
|
||||
return current();
|
||||
}
|
||||
|
||||
public E next() {
|
||||
|
||||
// eerste before weg
|
||||
|
||||
// eerste after wordt huidig
|
||||
|
||||
// voeg laatste after toe
|
||||
|
||||
return size() == 0 ? null : get(index);
|
||||
}
|
||||
|
||||
protected int previous(int index) {
|
||||
if (--index < 0) {
|
||||
index = Math.max(0, size() - 1);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
protected int next(int index) {
|
||||
System.out.println(index);
|
||||
if (++index >= size()) {
|
||||
index = 0;
|
||||
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
BufferedArrayCycle<Dummy> bac = new BufferedArrayCycle<Dummy>(2, 3);
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
bac.add(new Dummy(i));
|
||||
}
|
||||
bac.remove(0);
|
||||
System.out.println(bac.get(2).id);
|
||||
}
|
||||
}
|
||||
18
java/base/src/main/java/base/util/Dummy.java
Normal file
18
java/base/src/main/java/base/util/Dummy.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package base.util;
|
||||
|
||||
public class Dummy implements Bufferable {
|
||||
public int id;
|
||||
|
||||
public Dummy(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
System.out.println("Dummy #" + id + ": load()");
|
||||
}
|
||||
|
||||
public void unload() {
|
||||
System.out.println("Dummy #" + id + ": load()");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user