Cleaned up git
[iotcloud.git] / version1 / src / java / iotcloud / SlotBuffer.java
diff --git a/version1/src/java/iotcloud/SlotBuffer.java b/version1/src/java/iotcloud/SlotBuffer.java
new file mode 100644 (file)
index 0000000..14bc926
--- /dev/null
@@ -0,0 +1,99 @@
+package iotcloud;
+
+/**
+ * Circular buffer that holds the live set of slots.
+ * @author Brian Demsky
+ * @version 1.0
+ */
+
+class SlotBuffer {
+       static final int DEFAULT_SIZE = 128;
+
+       private Slot[] array;
+       private int head;
+       private int tail;
+       private long oldestseqn;
+
+       SlotBuffer() {
+               array=new Slot[DEFAULT_SIZE+1];
+               head=tail=0;
+               oldestseqn=0;
+       }
+
+       int size() {
+               if (head >= tail)
+                       return head - tail;
+               return (array.length + head) - tail;
+       }
+
+       int capacity() {
+               return array.length - 1;
+       }
+
+       void resize(int newsize) {
+               if (newsize == (array.length-1))
+                       return;
+               Slot[] newarray = new Slot[newsize+1];
+               int currsize = size();
+               int index = tail;
+               for(int i=0; i < currsize; i++) {
+                       newarray[i] = array[index];
+                       if ((++index) == array.length)
+                               index = 0;
+               }
+               array = newarray;
+               tail = 0;
+               head = currsize;
+       }
+
+       private void incrementHead() {
+               head++;
+               if (head >= array.length)
+                       head=0;
+       }
+
+       private void incrementTail() {
+               tail++;
+               if (tail >= array.length)
+                       tail=0;
+       }
+
+       void putSlot(Slot s) {
+               array[head]=s;
+               incrementHead();
+
+               if (oldestseqn==0)
+                       oldestseqn = s.getSequenceNumber();
+
+               if (head==tail) {
+                       incrementTail();
+                       oldestseqn++;
+               }
+       }
+
+       Slot getSlot(long seqnum) {
+               int diff=(int) (seqnum-oldestseqn);
+               int index=diff + tail;
+               if (index >= array.length) {
+                       if (head >= tail)
+                               return null;
+                       index-= array.length;
+               }
+
+               if (index >= array.length)
+                       return null;
+
+               if (head >= tail && index >= head)
+                       return null;
+
+               return array[index];
+       }
+
+       long getOldestSeqNum() {
+               return oldestseqn;
+       }
+
+       long getNewestSeqNum() {
+               return oldestseqn + size() - 1;
+       }
+}