Skip to content

Commit

Permalink
Add a method to allow LifecycleManager to free keys (#138)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Christoph Läubrich <[email protected]>
  • Loading branch information
cstamas and laeubi authored May 31, 2024
1 parent a5f51ed commit f7bd80c
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -37,16 +38,19 @@ public final class LifecycleManager
// Public methods
// ----------------------------------------------------------------------

@Override
public boolean manage( final Class<?> clazz )
{
return buildLifecycle( clazz );
}

@Override
public PropertyBinding manage( final BeanProperty<?> property )
{
return null; // no custom property bindings
}

@Override
public boolean manage( final Object bean )
{
final BeanLifecycle lifecycle = lifecycleFor( bean );
Expand All @@ -61,6 +65,7 @@ public boolean manage( final Object bean )
return true;
}

@Override
public boolean unmanage( final Object bean )
{
if ( removeStoppable( bean ) )
Expand All @@ -70,6 +75,7 @@ public boolean unmanage( final Object bean )
return true;
}

@Override
public boolean unmanage()
{
for ( Object bean; ( bean = popStoppable() ) != null; )
Expand Down Expand Up @@ -156,4 +162,41 @@ private Object popStoppable()
return stoppableBeans.pollLast();
}
}

/**
* Flush the cache for each key that satisfies the given predicate
*
* @param remove a tester that can decide if this key needs to be flushed or
* not.
* @since TBD
*/
public void flushCacheFor( ClassTester remove )
{
for ( Iterator<Class<?>> iterator = lifecycles.keySet().iterator(); iterator.hasNext(); )
{
if ( remove.shouldFlush( iterator.next() ) )
{
iterator.remove();
}
}
}

/**
* Allows testing if a class should be flushed from the cache
*
* @since TBD
*/
public static interface ClassTester
{

/**
* Test if class should be flushed
*
* @param clz the class to test
* @return <code>true</code> if class must be flushed, <code>false</code>
* otherwise
*/
boolean shouldFlush( Class<?> clz );

}
}

0 comments on commit f7bd80c

Please sign in to comment.