diff --git a/pom.xml b/pom.xml
index 9594d3e..5febbc8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
io.github.umutayb
context-store
- 1.0.0
+ 1.0.1
jar
Context Store
Context Store is a reliable context storage solution where values can be stored, updated and accessed on the same thread!
diff --git a/src/main/java/context/ContextStore.java b/src/main/java/context/ContextStore.java
index a418a17..710f23b 100644
--- a/src/main/java/context/ContextStore.java
+++ b/src/main/java/context/ContextStore.java
@@ -88,7 +88,113 @@ public static synchronized V get(K key) {
* @see ContextStore#map
*/
public static synchronized V get(K key, V defaultValue) {
- return key != null && map.get().get(key) != null ? ((ConcurrentHashMap) map.get()).get(key) : defaultValue;
+ return key != null && get(key) != null ? get(key): defaultValue;
+ }
+
+ /**
+ * Retrieves the boolean value associated with the specified key from the ContextStore.
+ * The method retrieves the value, converts it to a String, and uses Boolean.parseBoolean().
+ * Since Boolean.parseBoolean returns false for non-"true" strings (including null and invalid values),
+ * this method will return {@code false} if the key is not present, if the value is null,
+ * or if the value's string representation is anything other than "true" (case-insensitive).
+ *
+ * @param key The key whose associated boolean value is to be retrieved.
+ * @return The boolean value based on the key's string representation, or {@code false} if the key is not present or the value is not "true".
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized boolean getBoolean(Object key) {
+ Object value = get(key);
+ return Boolean.parseBoolean(value != null ? value.toString() : null);
+ }
+
+ /**
+ * Retrieves the boolean value associated with the specified key from the ContextStore.
+ * The method retrieves the value, converts it to a String, and uses Boolean.parseBoolean().
+ * If the key is not present in the map (and {@code get(key)} returns null), the
+ * {@code defaultValue} is returned. Otherwise, {@code Boolean.parseBoolean(value.toString())}
+ * is used, which returns {@code false} for non-"true" strings.
+ *
+ * @param key The key whose associated boolean value is to be retrieved.
+ * @param defaultValue The default boolean value to be returned if the key is not present.
+ * @return The boolean value based on the key's string representation, or the defaultValue if the key is not present.
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized boolean getBoolean(Object key, boolean defaultValue) {
+ Object value = get(key);
+ if (value == null)
+ return defaultValue;
+
+ return Boolean.parseBoolean(value.toString());
+ }
+
+ /**
+ * Retrieves the int value associated with the specified key from the ContextStore.
+ * The method attempts to parse the value (after converting to String) into an int.
+ * If the key is not present or parsing fails (NumberFormatException), the value {@code 0} is returned.
+ *
+ * @param key The key whose associated int value is to be retrieved.
+ * @return The int value associated with the specified key, or {@code 0} if the key is not present or parsing fails.
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized int getInt(Object key) {
+ return getInt(key, 0);
+ }
+
+ /**
+ * Retrieves the int value associated with the specified key from the ContextStore.
+ * The method attempts to parse the value (after converting to String) into an int.
+ * If parsing fails or the key is not present, the {@code defaultValue} is returned.
+ *
+ * @param key The key whose associated int value is to be retrieved.
+ * @param defaultValue The default int value to be returned if parsing fails or the key is not present.
+ * @return The int value associated with the specified key, or the defaultValue if parsing fails.
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized int getInt(Object key, int defaultValue) {
+ Object value = get(key);
+ if (value == null)
+ return defaultValue;
+
+ try {return Integer.parseInt(value.toString());}
+ catch (NumberFormatException ignored) {return defaultValue;}
+ }
+
+ /**
+ * Retrieves the double value associated with the specified key from the ContextStore.
+ * The method attempts to parse the value (after converting to String) into a double.
+ * If the key is not present or parsing fails (NumberFormatException), the value {@code 0.0} is returned.
+ *
+ * @param key The key whose associated double value is to be retrieved.
+ * @return The double value associated with the specified key, or {@code 0.0} if the key is not present or parsing fails.
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized double getDouble(Object key) {
+ return getDouble(key, 0.0);
+ }
+
+ /**
+ * Retrieves the double value associated with the specified key from the ContextStore.
+ * The method attempts to parse the value (after converting to String) into a double.
+ * If parsing fails or the key is not present, the {@code defaultValue} is returned.
+ *
+ * @param key The key whose associated double value is to be retrieved.
+ * @param defaultValue The default double value to be returned if parsing fails or the key is not present.
+ * @return The double value associated with the specified key, or the defaultValue if parsing fails.
+ * @throws IllegalArgumentException If the provided key is null.
+ * @see ContextStore#get(Object)
+ */
+ public static synchronized double getDouble(Object key, double defaultValue) {
+ Object value = get(key);
+ if (value == null)
+ return defaultValue;
+
+ try {return Double.parseDouble(value.toString());}
+ catch (NumberFormatException ignored) {return defaultValue;}
}
/**
diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java
index a49b34c..cbbd730 100644
--- a/src/test/java/AppTest.java
+++ b/src/test/java/AppTest.java
@@ -27,7 +27,7 @@ public void propertyReadingTest() {
@Test
public void defaultValueTest() {
- Assert.assertEquals("Default value mismatch!", "default-value", ContextStore.get("non-existent-property", "default-value"));
+ Assert.assertEquals("Value mismatch!", "default-value", ContextStore.get("non-existent-property", "default-value"));
log.info("defaultValueTest() pass!");
}
@@ -37,4 +37,76 @@ public void loadValueTest() {
Assert.assertEquals("New did not load!", "test-value-2", ContextStore.get("test-property"));
log.info("loadValueTest() pass!");
}
+
+ @Test
+ public void getBooleanTest() {
+ Assert.assertTrue("Value mismatch!", ContextStore.getBoolean("test-bool"));
+ log.info("getBooleanTest() pass!");
+ }
+
+ @Test
+ public void getBooleanDefaultTest() {
+ Assert.assertTrue("Value mismatch!", ContextStore.getBoolean("test-false-bool", true));
+ log.info("getBooleanDefaultTest() pass!");
+ }
+
+ @Test
+ public void getBooleanUnspecifiedDefaultTest() {
+ Assert.assertFalse("Value mismatch!", ContextStore.getBoolean("test-false-bool"));
+ log.info("getBooleanUnspecifiedDefaultTest() pass!");
+ }
+
+ @Test
+ public void getBooleanIncorrectDefaultTest() {
+ Assert.assertFalse("Value mismatch!", ContextStore.getBoolean("test-false-primitive"));
+ log.info("getBooleanIncorrectDefaultTest() pass!");
+ }
+
+ @Test
+ public void getIntTest() {
+ Assert.assertEquals("Value mismatch!", 15, ContextStore.getInt("test-int"));
+ log.info("getIntTest() pass!");
+ }
+
+ @Test
+ public void getIntDefaultTest() {
+ Assert.assertEquals("Value mismatch!", 10, ContextStore.getInt("test-false-int", 10));
+ log.info("getIntDefaultTest() pass!");
+ }
+
+ @Test
+ public void getIntUnspecifiedDefaultTest() {
+ Assert.assertEquals("Value mismatch!", 0, ContextStore.getInt("test-false-int"));
+ log.info("getIntUnspecifiedDefaultTest() pass!");
+ }
+
+ @Test
+ public void getIntIncorrectDefaultTest() {
+ Assert.assertEquals("Value mismatch!", 0, ContextStore.getInt("test-false-primitive"));
+ log.info("getIntIncorrectDefaultTest() pass!");
+ }
+
+ @Test
+ public void getDoubleTest() {
+ Assert.assertTrue("Value mismatch!", 4.3 == ContextStore.getDouble("test-double"));
+ log.info("getDoubleTest() pass!");
+ }
+
+ @Test
+ public void getDoubleDefaultTest() {
+ Assert.assertTrue("Value mismatch!", 10.0 == ContextStore.getDouble("test-false-double", 10.0));
+ log.info("getDoubleDefaultTest() pass!");
+ }
+
+ @Test
+ public void getDoubleUnspecifiedDefaultTest() {
+ Assert.assertTrue("Value mismatch!", 0.0 == ContextStore.getDouble("test-false-double"));
+ log.info("getDoubleUnspecifiedDefaultTest() pass!");
+ }
+
+ @Test
+ public void getDoubleIncorrectDefaultTest() {
+ Assert.assertTrue("Value mismatch!", 0.0 == ContextStore.getDouble("test-false-primitive"));
+ log.info("getDoubleIncorrectDefaultTest() pass!");
+ }
}
diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties
index f573d97..2723798 100644
--- a/src/test/resources/test.properties
+++ b/src/test/resources/test.properties
@@ -1,2 +1,6 @@
test-property=test-value
-test-secret=secret!
\ No newline at end of file
+test-secret=secret!
+test-int=15
+test-bool=true
+test-double=4.3
+test-false-primitive=false!123
\ No newline at end of file