feat(renderer): Add getOrAcquire method and enhance get method in RendererWorkingSet implementation
This commit is contained in:
@@ -13,6 +13,10 @@ public interface RendererWorkingSet {
|
||||
void put(String producerId, List<RenderingBo> work);
|
||||
|
||||
RenderingBo get(String producerId, String id);
|
||||
|
||||
<T extends RenderingBo> T get(String producerId, String id, Class<T> type);
|
||||
|
||||
<T extends RenderingBo> T getOrAcquire(String producerId, String id, Class<T> type);
|
||||
|
||||
Map<String, RenderingBo> getUncommittedWork(String producerId);
|
||||
|
||||
|
||||
@@ -54,6 +54,24 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
return this.getWorkingSetByProducer(producerId).next().get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends RenderingBo> T get(final String producerId, final String id, final Class<T> type) {
|
||||
final RenderingBo bo = this.getWorkingSetByProducer(producerId).next().get(id);
|
||||
if (bo == null) {
|
||||
return null;
|
||||
}
|
||||
return type.cast(bo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends RenderingBo> T getOrAcquire(final String producerId, final String id, final Class<T> type) {
|
||||
final var bo = this.get(producerId, id, type);
|
||||
if (bo != null) {
|
||||
return bo;
|
||||
}
|
||||
return this.rendererBoPool.acquire(id, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, RenderingBo> commit(final String producerId, final boolean clone) {
|
||||
final AtomicReference<Map<String, RenderingBo>> previousWork = new AtomicReference<>();
|
||||
@@ -88,7 +106,7 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
this.releaseBoClasses(rendererWork.next());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void getLiveSet(final List<RenderingBo> combinedLiveSet, final Predicate<String> filter) {
|
||||
this.workingSet.forEach((producer, rendererWork) -> {
|
||||
@@ -106,7 +124,7 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
if (work == null || work.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
final var releasedBoClasses = this.obtainSetFromPool();
|
||||
for (final RenderingBo renderingBO : work.values()) {
|
||||
if (releasedBoClasses.add(renderingBO.getClass())) {
|
||||
@@ -118,7 +136,7 @@ public class RendererWorkingSetImpl implements RendererWorkingSet {
|
||||
this.returnMapToPool(work);
|
||||
this.returnSetToPool(releasedBoClasses);
|
||||
}
|
||||
|
||||
|
||||
private Map<String, RenderingBo> obtainMapFromPool() {
|
||||
var map = this.mapsPool.poll();
|
||||
if (map == null) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -315,7 +316,50 @@ public class RendererWorkingSetImplTest {
|
||||
assertTrue(filteredLive.containsAll(List.of(boA1, boA2)));
|
||||
assertFalse(filteredLive.contains(boB1));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void getWithTypeReturnsTypedInstanceAndNullForUnknown() {
|
||||
final var boA = new TestBoA();
|
||||
boA.setId("bo1");
|
||||
this.workingSet.put(this.moduleA.getId(), boA);
|
||||
|
||||
final var retrieved = this.workingSet.get(this.moduleA.getId(), "bo1", TestBoA.class);
|
||||
assertSame(boA, retrieved);
|
||||
|
||||
final var notFound = this.workingSet.get(this.moduleA.getId(), "nonexistent", TestBoA.class);
|
||||
assertTrue(notFound == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWithTypeThrowsClassCastForWrongType() {
|
||||
final var boA = new TestBoA();
|
||||
boA.setId("bo1");
|
||||
this.workingSet.put(this.moduleA.getId(), boA);
|
||||
|
||||
assertThrows(ClassCastException.class, () -> this.workingSet.get(this.moduleA.getId(), "bo1", TestBoB.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOrAcquireReturnsUncommittedWhenPresent() {
|
||||
final var boA = new TestBoA();
|
||||
boA.setId("bo1");
|
||||
this.workingSet.put(this.moduleA.getId(), boA);
|
||||
|
||||
final var retrieved = this.workingSet.getOrAcquire(this.moduleA.getId(), "bo1", TestBoA.class);
|
||||
assertSame(boA, retrieved);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOrAcquireAcquiresFromPoolWhenMissing() {
|
||||
final var pooledBo = new TestBoA();
|
||||
pooledBo.setId("pooled");
|
||||
when(this.renderingBoPool.acquire("pooled", TestBoA.class)).thenReturn(pooledBo);
|
||||
|
||||
final var retrieved = this.workingSet.getOrAcquire(this.moduleA.getId(), "pooled", TestBoA.class);
|
||||
assertSame(pooledBo, retrieved);
|
||||
verify(this.renderingBoPool, times(1)).acquire("pooled", TestBoA.class);
|
||||
}
|
||||
|
||||
private static abstract class TestBoBase implements RenderingBo {
|
||||
|
||||
private final AtomicInteger closeStaticCalls = new AtomicInteger();
|
||||
|
||||
Reference in New Issue
Block a user