Skip to content

Commit e8f0cdf

Browse files
author
gaohaoxiang
committed
extend KeyValue
1 parent 23205fb commit e8f0cdf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

openmessaging-api/src/main/java/io/openmessaging/KeyValue.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
* @since OMS 1.0.0
3535
*/
3636
public interface KeyValue {
37+
38+
/**
39+
* Inserts or replaces {@code boolean} value for the specified key.
40+
*
41+
* @param key the key to be placed into this {@code KeyValue} object
42+
* @param value the value corresponding to <tt>key</tt>
43+
*/
44+
KeyValue put(String key, boolean value);
45+
3746
/**
3847
* Inserts or replaces {@code short} value for the specified key.
3948
*
@@ -74,6 +83,27 @@ public interface KeyValue {
7483
*/
7584
KeyValue put(String key, String value);
7685

86+
/**
87+
* Searches for the {@code boolean} property with the specified key in this {@code KeyValue} object. If the key is not
88+
* found in this property list, false is returned.
89+
*
90+
* @param key the property key
91+
* @return the value in this {@code KeyValue} object with the specified key value
92+
* @see #put(String, boolean)
93+
*/
94+
boolean getBoolean(String key);
95+
96+
/**
97+
* Searches for the {@code boolean} property with the specified key in this {@code KeyValue} object. If the key is not
98+
* found in this property list, false is returned.
99+
*
100+
* @param key the property key
101+
* @param defaultValue a default value
102+
* @return the value in this {@code KeyValue} object with the specified key value
103+
* @see #put(String, boolean)
104+
*/
105+
boolean getBoolean(String key, boolean defaultValue);
106+
77107
/**
78108
* Searches for the {@code short} property with the specified key in this {@code KeyValue} object. If the key is not
79109
* found in this property list, zero is returned.

openmessaging-api/src/main/java/io/openmessaging/internal/DefaultKeyValue.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@
3131
public class DefaultKeyValue implements KeyValue {
3232
private Map<String, String> properties;
3333

34+
@Override
35+
public KeyValue put(String key, boolean value) {
36+
properties.put(key, String.valueOf(value));
37+
return this;
38+
}
39+
40+
@Override
41+
public boolean getBoolean(String key) {
42+
if (!properties.containsKey(key)) {
43+
return false;
44+
}
45+
return Boolean.valueOf(properties.get(key));
46+
}
47+
48+
@Override
49+
public boolean getBoolean(String key, boolean defaultValue) {
50+
return properties.containsKey(key) ? getBoolean(key) : defaultValue;
51+
}
52+
3453
@Override
3554
public int getShort(String key) {
3655
return 0;

0 commit comments

Comments
 (0)