revert changes joel made to java files so that they will compile
[iotcloud.git] / 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         static final byte TypeKeyValue = 1;
13         static final byte TypeLastMessage = 2;
14         static final byte TypeRejectedMessage = 3;
15         static final byte TypeTableStatus = 4;
16
17         /* Records whether the information is still live or has been
18                  superceded by a newer update.  */
19
20         private boolean islive = true;
21         private Slot parentslot;
22
23         Entry(Slot _parentslot) {
24                 parentslot = _parentslot;
25         }
26
27         /**
28          * Static method for decoding byte array into Entry objects.  First
29          * byte tells the type of entry.
30          */
31
32         static Entry decode(Slot slot, ByteBuffer bb) {
33                 byte type=bb.get();
34                 switch(type) {
35                 case TypeKeyValue:
36                         return KeyValue.decode(slot, bb);
37
38                 case TypeLastMessage:
39                         return LastMessage.decode(slot, bb);
40
41                 case TypeRejectedMessage:
42                         return RejectedMessage.decode(slot, bb);
43
44                 case TypeTableStatus:
45                         return TableStatus.decode(slot, bb);
46
47                 default:
48                         throw new Error("Unrecognized Entry Type: "+type);
49                 }
50         }
51
52         /**
53          * Returns true if the Entry object is still live.
54          */
55
56         boolean isLive() {
57                 return islive;
58         }
59
60         /**
61          * Flags the entry object as dead.  Also decrements the live count
62          * of the parent slot.
63          */
64
65         void setDead() {
66                 islive = false;
67                 parentslot.decrementLiveCount();
68         }
69
70         /**
71          * Serializes the Entry object into the byte buffer.
72          */
73
74         abstract void encode(ByteBuffer bb);
75
76         /**
77          * Returns the size in bytes the entry object will take in the byte
78          * array.
79          */
80
81         abstract int getSize();
82
83         /**
84          * Returns a byte encoding the type of the entry object.
85          */
86
87         abstract byte getType();
88
89         /**
90          * Returns a copy of the Entry that can be added to a different slot.
91          */
92         abstract Entry getCopy(Slot s);
93
94 }