Phone app (based on Ali's Control for iotcloud benchmark) to control alarm in the...
[iot2.git] / benchmarks / other / PhoneInterface / Control / app / src / main / java / iotcloud / KeyValue.java
1 package iotcloud;
2 import java.nio.ByteBuffer;
3
4 /**
5  * KeyValue entry for Slot.
6  * @author Brian Demsky <bdemsky@uci.edu>
7  * @version 1.0
8  */
9
10 class KeyValue { /*extends Entry */
11         private IoTString key;
12         private IoTString value;
13
14         public KeyValue(IoTString _key, IoTString _value) {
15                 key = _key;
16                 value = _value;
17         }
18
19         public IoTString getKey() {
20                 return key;
21         }
22
23         public IoTString getValue() {
24                 return value;
25         }
26
27         static KeyValue decode(ByteBuffer bb) {
28                 int keylength = bb.getInt();
29                 int valuelength = bb.getInt();
30                 byte[] key = new byte[keylength];
31                 bb.get(key);
32
33                 if (valuelength != 0) {
34                         byte[] value = new byte[valuelength];
35                         bb.get(value);
36                         return new KeyValue(IoTString.shallow(key), IoTString.shallow(value));
37                 }
38
39                 return new KeyValue(IoTString.shallow(key), null);
40         }
41
42         public void encode(ByteBuffer bb) {
43                 bb.putInt(key.length());
44
45                 if (value != null) {
46                         bb.putInt(value.length());
47                 } else {
48                         bb.putInt(0);
49                 }
50
51                 bb.put(key.internalBytes());
52
53                 if (value != null) {
54                         bb.put(value.internalBytes());
55                 }
56         }
57
58         public int getSize() {
59                 if (value != null) {
60                         //return 2 * Integer.BYTES + key.length() + value.length();
61                         return 2 * Integer.SIZE/8 + key.length() + value.length();
62                 }
63
64                 //return 2 * Integer.BYTES + key.length();
65                 return 2 * Integer.SIZE/8 + key.length();
66         }
67
68         public String toString() {
69                 if (value == null) {
70                         return "null";
71                 }
72                 return value.toString();
73         }
74
75         public KeyValue getCopy() {
76                 return new KeyValue(key, value);
77         }
78 }