feat(renderer): Add getOrAcquire method and enhance get method in RendererWorkingSet implementation
This commit is contained in:
@@ -120,7 +120,6 @@ rendererWorkingSet.commit(myModule.getId(), true);
|
||||
var existingRect = rendererWorkingSet.get(myModule.getId(), "myRect");
|
||||
existingRect.setColor(Color.RED);
|
||||
existingRect.setPosition(150, 250);
|
||||
rendererWorkingSet.put(myModule.getId(), existingRect);
|
||||
|
||||
// Commit again to apply the changes
|
||||
rendererWorkingSet.commit(myModule.getId(), true);
|
||||
|
||||
@@ -14,6 +14,10 @@ public interface RendererWorkingSet {
|
||||
|
||||
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);
|
||||
|
||||
Map<String, RenderingBo> commit(String producerId, boolean clone);
|
||||
|
||||
@@ -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<>();
|
||||
|
||||
@@ -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;
|
||||
@@ -316,6 +317,49 @@ public class RendererWorkingSetImplTest {
|
||||
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