edits
[iotcloud.git] / src / java / iotcloud / SlotBuffer.java
index b9758506768663c6fe1e9ff672aae00e990efd77..3d5a465d3a3a5f4f98e8a5391b9760efd1e699c1 100644 (file)
@@ -10,6 +10,8 @@ class SlotBuffer {
 
        SlotBuffer() {
                array=new Slot[DEFAULT_SIZE+1];
+               head=tail=0;
+               oldestseqn=0;
        }
 
        int size() {
@@ -18,6 +20,10 @@ class SlotBuffer {
                return (array.length + head) - tail;
        }
 
+       int capacity() {
+               return array.length - 1;
+       }
+
        void resize(int newsize) {
                if (newsize == (array.length-1))
                        return;
@@ -34,14 +40,27 @@ class SlotBuffer {
                head = 0;
        }
 
+       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;
-               head++;
+               incrementHead();
+
                if (oldestseqn==0)
                        oldestseqn = s.getSequenceNumber();
 
                if (head==tail) {
-                       tail++;
+                       incrementTail();
                        oldestseqn++;
                }
        }
@@ -49,14 +68,16 @@ class SlotBuffer {
        Slot getSlot(long seqnum) {
                int diff=(int) (seqnum-oldestseqn);
                int index=diff + tail;
-               if (index > array.length) {
+               if (index >= array.length) {
                        if (head >= tail)
                                return null;
                        index-= array.length;
                }
 
-               if (index >= array.length ||
-                               index >= head)
+               if (index >= array.length)
+                       return null;
+
+               if (head >= tail && index >= head)
                        return null;
 
                return array[index];
@@ -67,6 +88,6 @@ class SlotBuffer {
        }
 
        long getNewestSeqNum() {
-               return oldestseqn + size();
+               return oldestseqn + size() - 1;
        }
 }