edits
[iotcloud.git] / version2 / src / C / KeyValue.cc
1
2 /**
3  * KeyValue entry for Slot.
4  * @author Brian Demsky <bdemsky@uci.edu>
5  * @version 1.0
6  */
7
8 class KeyValue { /*extends Entry */
9         IoTString key;
10         IoTString value;
11
12         KeyValue(IoTString _key, IoTString _value) {
13                 key = _key;
14                 value = _value;
15         }
16
17         IoTString getKey() {
18                 return key;
19         }
20
21         IoTString getValue() {
22                 return value;
23         }
24
25         static KeyValue decode(ByteBuffer bb) {
26                 int keylength = bb.getInt();
27                 int valuelength = bb.getInt();
28                 char[] key = new char[keylength];
29                 bb.get(key);
30
31                 if (valuelength != 0) {
32                         char[] value = new char[valuelength];
33                         bb.get(value);
34                         return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
35                 }
36
37                 return new KeyValue(IoTString.shallow(key), NULL);
38         }
39
40         void encode(ByteBuffer bb) {
41                 bb.putInt(key.length());
42
43                 if (value != NULL) {
44                         bb.putInt(value.length());
45                 } else {
46                         bb.putInt(0);
47                 }
48
49                 bb.put(key.internalBytes());
50
51                 if (value != NULL) {
52                         bb.put(value.internalBytes());
53                 }
54         }
55
56         int getSize() {
57                 if (value != NULL) {
58                         return 2 * sizeof(int32_t) + key.length() + value.length();
59                 }
60
61                 return 2 * sizeof(int32_t) + key.length();
62         }
63
64         String toString() {
65                 if (value == NULL) {
66                         return "NULL";
67                 }
68                 return value.toString();
69         }
70
71         KeyValue getCopy() {
72                 return new KeyValue(key, value);
73         }
74 }