diff --git a/java/base/src/main/java/base/util/ArrayCycle.java b/java/base/src/main/java/base/util/ArrayCycle.java new file mode 100644 index 0000000..72df2ac --- /dev/null +++ b/java/base/src/main/java/base/util/ArrayCycle.java @@ -0,0 +1,40 @@ +package base.util; + +import java.util.concurrent.CopyOnWriteArrayList; + +public class ArrayCycle extends CopyOnWriteArrayList { + 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); + } +} diff --git a/java/base/src/main/java/base/util/Buffer.java b/java/base/src/main/java/base/util/Buffer.java new file mode 100644 index 0000000..31c890c --- /dev/null +++ b/java/base/src/main/java/base/util/Buffer.java @@ -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; + } +} diff --git a/java/base/src/main/java/base/util/Bufferable.java b/java/base/src/main/java/base/util/Bufferable.java new file mode 100644 index 0000000..5249edf --- /dev/null +++ b/java/base/src/main/java/base/util/Bufferable.java @@ -0,0 +1,6 @@ +package base.util; + +public interface Bufferable { + public void load(); + public void unload(); +} diff --git a/java/base/src/main/java/base/util/BufferedArrayCycle.java b/java/base/src/main/java/base/util/BufferedArrayCycle.java new file mode 100644 index 0000000..589bd09 --- /dev/null +++ b/java/base/src/main/java/base/util/BufferedArrayCycle.java @@ -0,0 +1,74 @@ +package base.util; + +public class BufferedArrayCycle extends ArrayCycle { + protected static final long serialVersionUID = 1L; + + protected ArrayCycle 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(); + + } + + 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 bac = new BufferedArrayCycle(2, 3); + for (int i = 1; i <= 10; ++i) { + bac.add(new Dummy(i)); + } + bac.remove(0); + System.out.println(bac.get(2).id); + } +} \ No newline at end of file diff --git a/java/base/src/main/java/base/util/Dummy.java b/java/base/src/main/java/base/util/Dummy.java new file mode 100644 index 0000000..e23ec10 --- /dev/null +++ b/java/base/src/main/java/base/util/Dummy.java @@ -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()"); + } + +}