/trunk/src/test/java/org/mentacontainer/impl/MentaContainerTest.java |
---|
70,6 → 70,32 |
Assert.assertTrue(s1.equals(s2)); |
} |
@Test |
public void testCheckAndClear() { |
Container c = new MentaContainer(); |
c.ioc("myStr", String.class, Scope.SINGLETON).addInitValue("hello"); |
Assert.assertEquals(false, c.check("myStr")); |
String s1 = c.get("myStr"); |
Assert.assertEquals(true, c.check("myStr")); |
String s2 = c.get("myStr"); |
Assert.assertTrue(s1 == s2); |
c.clear(Scope.SINGLETON); |
Assert.assertEquals(false, c.check("myStr")); |
String s3 = c.get("myStr"); |
Assert.assertTrue(s3 != s2); |
} |
private static class MyThread extends Thread { |
private final Container c; |
/trunk/src/main/java/org/mentacontainer/impl/MentaContainer.java |
---|
46,6 → 46,42 |
} |
} |
public <T> T clear(String key) { |
Scope scope = scopes.get(key); |
if (scope == Scope.SINGLETON) { |
return (T) singletonsCache.remove(key); |
} else if (scope == Scope.THREAD) { |
ThreadLocal<Object> t = threadLocalsCache.get(key); |
if (t != null) { |
Object o = t.get(); |
if (o != null) { |
t.set(null); |
return (T) o; |
} |
} |
return null; |
} else if (scope == Scope.NONE) { |
return null; // always... |
} else { |
throw new UnsupportedOperationException("Scope not supported: " + scope); |
} |
} |
public <T> T get(String key) { |
if (!beans.containsKey(key)) return null; |
232,9 → 268,9 |
return MentaContainer.this.get(key); |
} |
public boolean contains(String key) { |
public boolean hasValue(String key) { |
return MentaContainer.this.contains(key); |
return MentaContainer.this.check(key); |
} |
}; |
251,8 → 287,27 |
return this; |
} |
public boolean contains(String key) { |
public boolean check(String key) { |
return beans.containsKey(key); |
Scope scope = scopes.get(key); |
if (scope == Scope.NONE) { |
return false; // always... |
} else if (scope == Scope.SINGLETON) { |
return singletonsCache.containsKey(key); |
} else if (scope == Scope.THREAD) { |
ThreadLocal<Object> t = threadLocalsCache.get(key); |
return t.get() != null; |
} else { |
throw new UnsupportedOperationException("This scope is not supported: " + scope); |
} |
} |
} |
/trunk/src/main/java/org/mentacontainer/Container.java |
---|
120,12 → 120,14 |
public Container populate(Object bean); |
/** |
* Check whether the container is configured to provide a bean with name 'key'. |
* Check whether the container currently has a value for this key. For example, |
* if it is a singleton AND someone has requested it, the container will have it cached. |
* The method is useful to check for an instance without forcing her creation. |
* |
* @param key The key representing the bean inside the container. |
* @return true if the container contains this bean. |
* @return true if the container has an instance cached in the scope for this key |
*/ |
public boolean contains(String key); |
public boolean check(String key); |
/** |
* Clear all cached instances for that scope. If you have a thread pool for example you will |
134,4 → 136,12 |
* @param scope The scope to be cleared. |
*/ |
public void clear(Scope scope); |
/** |
* Clear a single key from cache and return the instance that was cached. |
* |
* @param key The key representing the bean inside the container. |
* @return The value that was cached and it is not anymore (was cleared) |
*/ |
public <T> T clear(String key); |
} |
/trunk/src/main/java/org/mentacontainer/util/InjectionUtils.java |
---|
590,7 → 590,7 |
public Object get(String key); |
public boolean contains(String key); |
public boolean hasValue(String key); |
} |
public static void getObject(Object target, Provider provider, boolean tryField, String prefix, boolean tryToConvert, boolean convertBoolean, boolean allowRecursion) |
641,7 → 641,7 |
String var = iter.next(); |
boolean hasValue = provider.contains(var); |
boolean hasValue = provider.hasValue(var); |
Object value = provider.get(var); |
756,7 → 756,7 |
String var = iter.next(); |
boolean hasValue = provider.contains(var); |
boolean hasValue = provider.hasValue(var); |
Object value = provider.get(var); |