Skip to content

Commit

Permalink
Merge pull request #138 from kevin-canadian/add-list-to-config-cache
Browse files Browse the repository at this point in the history
Add list() to ConfigCache
  • Loading branch information
lviggiano committed Aug 18, 2015
2 parents 6e49595 + c9b987e commit 1884975
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions owner/src/main/java/org/aeonbits/owner/ConfigCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

package org.aeonbits.owner;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -115,6 +117,19 @@ public static <T extends Config> T add(Object key, T instance) {
return (T) CACHE.putIfAbsent(key, instance);
}

/**
* Lists the key objects for all configuration instances present in the cache.
*
* @return a set containing the key objects for all instance in the cache.
*/
public static Set<Object> list() {
// Create a new Set to ensure that the caller does not modify the contents of our
// private map via the result of this call. The key objects themselves are the same
// as those contained in the private map, which means that if they are mutable, the
// caller will be able to affect the contents of the map (albeit only the keys).
return new HashSet<Object>(CACHE.keySet());
}

/**
* Removes all of the cached instances.
* The cache will be empty after this call returns.
Expand Down
13 changes: 13 additions & 0 deletions owner/src/test/java/org/aeonbits/owner/cache/CacheConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.mockito.Matchers;

import java.util.Map;
import java.util.Set;

import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -60,6 +61,18 @@ public void testGetOrCreateUsingNameKey() {
assertSame(first, second);
}

@Test
public void testList() {
ConfigCache.getOrCreate("MyConfig1", MyConfig.class);
ConfigCache.getOrCreate("MyConfig2", MyConfig.class);
ConfigCache.getOrCreate("MyConfig3", MyConfig.class);
Set<Object> keys = ConfigCache.list();
assertTrue(keys.contains("MyConfig1"));
assertTrue(keys.contains("MyConfig2"));
assertTrue(keys.contains("MyConfig3"));
assertTrue(3 == keys.size());
}

@Test
public void testRemoveUsingClassAsKey() {
MyConfig first = ConfigCache.getOrCreate(MyConfig.class);
Expand Down

0 comments on commit 1884975

Please sign in to comment.