Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.umutayb</groupId>
<artifactId>context-store</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>
<name>Context Store</name>
<description>Context Store is a reliable context storage solution where values can be stored, updated and accessed on the same thread!</description>
Expand Down
108 changes: 107 additions & 1 deletion src/main/java/context/ContextStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,113 @@ public static synchronized <K, V> V get(K key) {
* @see ContextStore#map
*/
public static synchronized <K, V> V get(K key, V defaultValue) {
return key != null && map.get().get(key) != null ? ((ConcurrentHashMap<K, V>) 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;}
}

/**
Expand Down
74 changes: 73 additions & 1 deletion src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}

Expand All @@ -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!");
}
}
6 changes: 5 additions & 1 deletion src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
test-property=test-value
test-secret=secret!
test-secret=secret!
test-int=15
test-bool=true
test-double=4.3
test-false-primitive=false!123