8734bb1740d363c9dbec8fa4b313ae9b98bd8dda
[iotcloud.git] / src2 / 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 TypeLastMessage:
44                         return LastMessage.decode(slot, bb);
45
46                 case TypeRejectedMessage:
47                         return RejectedMessage.decode(slot, bb);
48
49                 case TypeTableStatus:
50                         return TableStatus.decode(slot, bb);
51
52                 default:
53                         throw new Error("Unrecognized Entry Type: " + type);
54                 }
55         }
56
57         /**
58          * Returns true if the Entry object is still live.
59          */
60
61         public boolean isLive() {
62                 return islive;
63         }
64
65         /**
66          * Flags the entry object as dead.  Also decrements the live count
67          * of the parent slot.
68          */
69
70         public void setDead() {
71
72                 if (!islive ) {
73                         return; // already dead
74                 }
75
76                 islive = false;
77                 parentslot.decrementLiveCount();
78         }
79
80         /**
81          * Serializes the Entry object into the byte buffer.
82          */
83
84         abstract void encode(ByteBuffer bb);
85
86         /**
87          * Returns the size in bytes the entry object will take in the byte
88          * array.
89          */
90
91         abstract int getSize();
92
93         /**
94          * Returns a byte encoding the type of the entry object.
95          */
96
97         abstract byte getType();
98
99         /**
100          * Returns a copy of the Entry that can be added to a different slot.
101          */
102         abstract Entry getCopy(Slot s);
103
104 }