Skip to content

Commit f7bd80c

Browse files
cstamaslaeubi
andauthored
Add a method to allow LifecycleManager to free keys (#138)
Currently there is no way to ever remove a key from the map, this can lead to accumulation of memory as it is strongly referencing the class. This adds a new method so keys can be removed from the map Fixes #74 Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de> Co-authored-by: Christoph Läubrich <laeubi@laeubi-soft.de>
1 parent a5f51ed commit f7bd80c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

org.eclipse.sisu.inject/src/main/java/org/eclipse/sisu/bean/LifecycleManager.java

+43
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.ArrayDeque;
1414
import java.util.Deque;
15+
import java.util.Iterator;
1516
import java.util.Map;
1617
import java.util.concurrent.ConcurrentHashMap;
1718

@@ -37,16 +38,19 @@ public final class LifecycleManager
3738
// Public methods
3839
// ----------------------------------------------------------------------
3940

41+
@Override
4042
public boolean manage( final Class<?> clazz )
4143
{
4244
return buildLifecycle( clazz );
4345
}
4446

47+
@Override
4548
public PropertyBinding manage( final BeanProperty<?> property )
4649
{
4750
return null; // no custom property bindings
4851
}
4952

53+
@Override
5054
public boolean manage( final Object bean )
5155
{
5256
final BeanLifecycle lifecycle = lifecycleFor( bean );
@@ -61,6 +65,7 @@ public boolean manage( final Object bean )
6165
return true;
6266
}
6367

68+
@Override
6469
public boolean unmanage( final Object bean )
6570
{
6671
if ( removeStoppable( bean ) )
@@ -70,6 +75,7 @@ public boolean unmanage( final Object bean )
7075
return true;
7176
}
7277

78+
@Override
7379
public boolean unmanage()
7480
{
7581
for ( Object bean; ( bean = popStoppable() ) != null; )
@@ -156,4 +162,41 @@ private Object popStoppable()
156162
return stoppableBeans.pollLast();
157163
}
158164
}
165+
166+
/**
167+
* Flush the cache for each key that satisfies the given predicate
168+
*
169+
* @param remove a tester that can decide if this key needs to be flushed or
170+
* not.
171+
* @since TBD
172+
*/
173+
public void flushCacheFor( ClassTester remove )
174+
{
175+
for ( Iterator<Class<?>> iterator = lifecycles.keySet().iterator(); iterator.hasNext(); )
176+
{
177+
if ( remove.shouldFlush( iterator.next() ) )
178+
{
179+
iterator.remove();
180+
}
181+
}
182+
}
183+
184+
/**
185+
* Allows testing if a class should be flushed from the cache
186+
*
187+
* @since TBD
188+
*/
189+
public static interface ClassTester
190+
{
191+
192+
/**
193+
* Test if class should be flushed
194+
*
195+
* @param clz the class to test
196+
* @return <code>true</code> if class must be flushed, <code>false</code>
197+
* otherwise
198+
*/
199+
boolean shouldFlush( Class<?> clz );
200+
201+
}
159202
}

0 commit comments

Comments
 (0)