Changed way Guard works, Sped up code
[iotcloud.git] / version2 / src / java / iotcloud / Entry.java
1 package iotcloud;
2 import java.nio.ByteBuffer;
3
4 /**
5  * Generic class that wraps all the different types of information
6  * that can be stored in a Slot.
7  * @author Brian Demsky <bdemsky@uci.edu>
8  * @version 1.0
9  */
10
11 abstract class Entry implements Liveness {
12
13
14         static final byte TypeCommit = 1;
15         static final byte TypeAbort = 2;
16         static final byte TypeTransaction = 3;
17         static final byte TypeNewKey = 4;
18         static final byte TypeLastMessage = 5;
19         static final byte TypeRejectedMessage = 6;
20         static final byte TypeTableStatus = 7;
21
22
23
24         /* Records whether the information is still live or has been
25                  superceded by a newer update.  */
26
27         private boolean islive = true;
28         private Slot parentslot;
29
30         public Entry(Slot _parentslot) {
31                 parentslot = _parentslot;
32         }
33
34         /**
35          * Static method for decoding byte array into Entry objects.  First
36          * byte tells the type of entry.
37          */
38
39         static Entry decode(Slot slot, ByteBuffer bb) {
40                 byte type = bb.get();
41                 switch (type) {
42
43                 case TypeCommit:
44                         return Commit.decode(slot, bb);
45
46                 case TypeAbort:
47                         return Abort.decode(slot, bb);
48
49                 case TypeTransaction:
50                         return Transaction.decode(slot, bb);
51
52                 case TypeNewKey:
53                         return NewKey.decode(slot, bb);
54
55                 case TypeLastMessage:
56                         return LastMessage.decode(slot, bb);
57
58                 case TypeRejectedMessage:
59                         return RejectedMessage.decode(slot, bb);
60
61                 case TypeTableStatus:
62                         return TableStatus.decode(slot, bb);
63
64                 default:
65                         throw new Error("Unrecognized Entry Type: " + type);
66                 }
67         }
68
69         /**
70          * Returns true if the Entry object is still live.
71          */
72
73         public boolean isLive() {
74                 return islive;
75         }
76
77         /**
78          * Flags the entry object as dead.  Also decrements the live count
79          * of the parent slot.
80          */
81
82         public void setDead() {
83
84                 if (!islive ) {
85                         return; // already dead
86                 }
87
88                 islive = false;
89                 parentslot.decrementLiveCount();
90         }
91
92         /**
93          * Serializes the Entry object into the byte buffer.
94          */
95
96         abstract void encode(ByteBuffer bb);
97
98         /**
99          * Returns the size in bytes the entry object will take in the byte
100          * array.
101          */
102
103         abstract int getSize();
104
105         /**
106          * Returns a byte encoding the type of the entry object.
107          */
108
109         abstract byte getType();
110
111         /**
112          * Returns a copy of the Entry that can be added to a different slot.
113          */
114         abstract Entry getCopy(Slot s);
115
116 }