|
26 | 26 | * Persistence that uses memory |
27 | 27 | * |
28 | 28 | * In cases where reliability is not required across client or device |
29 | | - * restarts memory this memory peristence can be used. In cases where |
| 29 | + * restarts memory this memory persistence can be used. In cases where |
30 | 30 | * reliability is required like when clean session is set to false |
31 | 31 | * then a non-volatile form of persistence should be used. |
32 | 32 | * |
33 | 33 | */ |
34 | 34 | public class MemoryPersistence implements MqttClientPersistence { |
35 | 35 |
|
36 | | - private Hashtable data; |
| 36 | + private Hashtable<String, MqttPersistable> data; |
37 | 37 |
|
38 | 38 | /* (non-Javadoc) |
39 | 39 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#close() |
40 | 40 | */ |
41 | 41 | public void close() throws MqttPersistenceException { |
42 | | - data.clear(); |
| 42 | + if (data != null) { |
| 43 | + data.clear(); |
| 44 | + } |
43 | 45 | } |
44 | 46 |
|
45 | 47 | /* (non-Javadoc) |
46 | 48 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#keys() |
47 | 49 | */ |
48 | | - public Enumeration keys() throws MqttPersistenceException { |
| 50 | + public Enumeration<String> keys() throws MqttPersistenceException { |
| 51 | + checkIsOpen(); |
49 | 52 | return data.keys(); |
50 | 53 | } |
51 | 54 |
|
52 | 55 | /* (non-Javadoc) |
53 | 56 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#get(java.lang.String) |
54 | 57 | */ |
55 | 58 | public MqttPersistable get(String key) throws MqttPersistenceException { |
| 59 | + checkIsOpen(); |
56 | 60 | return (MqttPersistable)data.get(key); |
57 | 61 | } |
58 | 62 |
|
59 | 63 | /* (non-Javadoc) |
60 | 64 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#open(java.lang.String, java.lang.String) |
61 | 65 | */ |
62 | 66 | public void open(String clientId, String serverURI) throws MqttPersistenceException { |
63 | | - this.data = new Hashtable(); |
| 67 | + this.data = new Hashtable<String, MqttPersistable>(); |
64 | 68 | } |
65 | 69 |
|
66 | 70 | /* (non-Javadoc) |
67 | | - * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.client.mqttv3.MqttPersistable) |
| 71 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.mqttv5.client.MqttPersistable) |
68 | 72 | */ |
69 | 73 | public void put(String key, MqttPersistable persistable) throws MqttPersistenceException { |
| 74 | + checkIsOpen(); |
70 | 75 | data.put(key, persistable); |
71 | 76 | } |
72 | 77 |
|
73 | 78 | /* (non-Javadoc) |
74 | 79 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#remove(java.lang.String) |
75 | 80 | */ |
76 | 81 | public void remove(String key) throws MqttPersistenceException { |
| 82 | + checkIsOpen(); |
77 | 83 | data.remove(key); |
78 | 84 | } |
79 | 85 |
|
80 | 86 | /* (non-Javadoc) |
81 | 87 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#clear() |
82 | 88 | */ |
83 | 89 | public void clear() throws MqttPersistenceException { |
| 90 | + checkIsOpen(); |
84 | 91 | data.clear(); |
85 | 92 | } |
86 | 93 |
|
87 | 94 | /* (non-Javadoc) |
88 | 95 | * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#containsKey(java.lang.String) |
89 | 96 | */ |
90 | 97 | public boolean containsKey(String key) throws MqttPersistenceException { |
| 98 | + checkIsOpen(); |
91 | 99 | return data.containsKey(key); |
92 | 100 | } |
| 101 | + |
| 102 | + /** |
| 103 | + * Checks whether the persistence has been opened. |
| 104 | + * @throws MqttPersistenceException if the persistence has not been opened. |
| 105 | + */ |
| 106 | + private void checkIsOpen() throws MqttPersistenceException { |
| 107 | + if (data == null) { |
| 108 | + throw new MqttPersistenceException(); |
| 109 | + } |
| 110 | + } |
93 | 111 | } |
0 commit comments