Skip to content

Commit

Permalink
Removes realizeCacheFor from VintageValueProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Nov 22, 2024
1 parent f03e2c8 commit e9749eb
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
public class VintageValueProvider implements ValueProvider {

private final ValueProvider valueProvider;
private final CachedValueProvider cache;
private final FactoryCache factoryCache;
private final PrefabValueFactory<?> fallbackFactory;

Expand All @@ -43,7 +42,6 @@ public VintageValueProvider(
Objenesis objenesis
) {
this.valueProvider = valueProvider;
this.cache = cache;
this.factoryCache = factoryCache;
this.fallbackFactory = new FallbackFactory<>(objenesis);
}
Expand All @@ -64,7 +62,11 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, Attributes attributes) {
* @return The "red" prefabricated value.
*/
public <T> T giveRed(TypeTag tag) {
return this.<T>giveTuple(tag).getRed();
return giveRed(tag, Attributes.unlabeled());
}

public <T> T giveRed(TypeTag tag, Attributes attributes) {
return this.<T>giveTuple(tag, attributes).getRed();
}

/**
Expand All @@ -77,7 +79,11 @@ public <T> T giveRed(TypeTag tag) {
* @return The "blue" prefabricated value.
*/
public <T> T giveBlue(TypeTag tag) {
return this.<T>giveTuple(tag).getBlue();
return giveBlue(tag, Attributes.unlabeled());
}

public <T> T giveBlue(TypeTag tag, Attributes attributes) {
return this.<T>giveTuple(tag, attributes).getBlue();
}

/**
Expand All @@ -90,7 +96,11 @@ public <T> T giveBlue(TypeTag tag) {
* @return A shallow copy of the "red" prefabricated value.
*/
public <T> T giveRedCopy(TypeTag tag) {
return this.<T>giveTuple(tag).getRedCopy();
return giveRedCopy(tag, Attributes.unlabeled());
}

public <T> T giveRedCopy(TypeTag tag, Attributes attributes) {
return this.<T>giveTuple(tag, attributes).getRedCopy();
}

/**
Expand Down Expand Up @@ -145,32 +155,8 @@ private boolean arraysAreDeeplyEqual(Object x, Object y) {
return Arrays.deepEquals(new Object[] { x }, new Object[] { y });
}

/**
* Makes sure that values for the specified type are present in the cache, but doesn't return
* them.
*
* @param <T> The desired type.
* @param tag A description of the desired type, including generic parameters.
* @param attributes Keeps track of recursion in the type.
*/
public <T> void realizeCacheFor(TypeTag tag, Attributes attributes) {
if (!cache.contains(tag, attributes.label)) {
Tuple<T> tuple = createTuple(tag, attributes);
cache.put(tag, attributes.label, tuple);
}
}

private <T> Tuple<T> giveTuple(TypeTag tag) {
return giveTuple(tag, Attributes.unlabeled());
}

@SuppressWarnings("unchecked")
private <T> Tuple<T> giveTuple(TypeTag tag, Attributes attributes) {
realizeCacheFor(tag, attributes);
return (Tuple<T>) cache.provide(tag, attributes).get();
}

private <T> Tuple<T> createTuple(TypeTag tag, Attributes attributes) {
if (attributes.typeStack.contains(tag)) {
throw new RecursionException(attributes.typeStack);
}
Expand All @@ -190,8 +176,6 @@ private <T> Tuple<T> createTuple(TypeTag tag, Attributes attributes) {
return factory.createValues(tag, this, attributes);
}

@SuppressWarnings("unchecked")
Tuple<T> result = (Tuple<T>) fallbackFactory.createValues(tag, this, attributes);
return result;
return (Tuple<T>) fallbackFactory.createValues(tag, this, attributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ protected TypeTag determineAndCacheActualTypeTag(
if (bottomType != null && result.getType().equals(Object.class)) {
result = new TypeTag(bottomType);
}
valueProvider.realizeCacheFor(result, attributes);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ public Tuple<T> createValues(
) {
Attributes clone = attributes.cloneAndAdd(tag);
TypeTag sourceTag = copyGenericTypesInto(source, tag);
valueProvider.realizeCacheFor(sourceTag, clone);

S redSource = valueProvider.giveRed(sourceTag);
S blueSource = valueProvider.giveBlue(sourceTag);
S redCopySource = valueProvider.giveRedCopy(sourceTag);
S redSource = valueProvider.giveRed(sourceTag, clone);
S blueSource = valueProvider.giveBlue(sourceTag, clone);
S redCopySource = valueProvider.giveRedCopy(sourceTag, clone);

return Tuple.of(copy.apply(redSource), copy.apply(blueSource), copy.apply(redCopySource));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public Tuple<T> createValues(
Map red = new HashMap<>();
Map blue = new HashMap<>();
Map redCopy = new HashMap<>();
red.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag));
blue.put(valueProvider.giveBlue(keyTag), valueProvider.giveBlue(valueTag));
redCopy.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag));
red.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone));
blue.put(valueProvider.giveBlue(keyTag, clone), valueProvider.giveBlue(valueTag, clone));
redCopy.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone));

return Tuple.of(factory.apply(red), factory.apply(blue), factory.apply(redCopy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public Tuple<T> createValues(
Collection red = new HashSet<>();
Collection blue = new HashSet<>();
Collection redCopy = new HashSet<>();
red.add(valueProvider.giveRed(entryTag));
blue.add(valueProvider.giveBlue(entryTag));
redCopy.add(valueProvider.giveRed(entryTag));
red.add(valueProvider.giveRed(entryTag, clone));
blue.add(valueProvider.giveBlue(entryTag, clone));
redCopy.add(valueProvider.giveRed(entryTag, clone));

return new Tuple<>(factory.apply(red), factory.apply(blue), factory.apply(redCopy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,13 @@ private Tuple<T> giveArrayInstances(
Class<T> type = tag.getType();
Class<?> componentType = type.getComponentType();
TypeTag componentTag = new TypeTag(componentType);
valueProvider.realizeCacheFor(componentTag, attributes);

T red = (T) Array.newInstance(componentType, 1);
Array.set(red, 0, valueProvider.giveRed(componentTag));
Array.set(red, 0, valueProvider.giveRed(componentTag, attributes));
T blue = (T) Array.newInstance(componentType, 1);
Array.set(blue, 0, valueProvider.giveBlue(componentTag));
Array.set(blue, 0, valueProvider.giveBlue(componentTag, attributes));
T redCopy = (T) Array.newInstance(componentType, 1);
Array.set(redCopy, 0, valueProvider.giveRed(componentTag));
Array.set(redCopy, 0, valueProvider.giveRed(componentTag, attributes));

return new Tuple<>(red, blue, redCopy);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ public Tuple<T> createValues(
// values.
// But don't do it in the Blue map, or they may cancel each other out again.

Object redKey = valueProvider.giveRed(keyTag);
Object blueKey = valueProvider.giveBlue(keyTag);
Object blueValue = valueProvider.giveBlue(valueTag);
Object redKey = valueProvider.giveRed(keyTag, clone);
Object blueKey = valueProvider.giveBlue(keyTag, clone);
Object blueValue = valueProvider.giveBlue(valueTag, clone);

T red = createEmpty.get();
red.put(redKey, blueValue);

T blue = createEmpty.get();
if (!redKey.equals(blueKey)) { // This happens with single-element enums
blue.put(valueProvider.giveBlue(keyTag), blueValue);
blue.put(valueProvider.giveBlue(keyTag, clone), blueValue);
}

T redCopy = createEmpty.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public Tuple<T> createValues(
for (int i = 0; i < n; i++) {
TypeTag paramTag = determineAndCacheActualTypeTag(i, tag, valueProvider, clone);

Object redValue = valueProvider.giveRed(paramTag);
Object blueValue = valueProvider.giveBlue(paramTag);
Object redValue = valueProvider.giveRed(paramTag, clone);
Object blueValue = valueProvider.giveBlue(paramTag, clone);
if (redValue.equals(blueValue)) { // This happens with single-element enums
useEmpty = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@ public Tuple<T> createValues(
T red = factory.get();
T blue = factory.get();
T redCopy = factory.get();
red.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag));
blue.put(valueProvider.giveBlue(keyTag), valueProvider.giveBlue(valueTag));
redCopy.put(valueProvider.giveRed(keyTag), valueProvider.giveBlue(valueTag));
red.put(valueProvider.giveRed(keyTag, clone), valueProvider.giveBlue(valueTag, clone));
blue.put(
valueProvider.giveBlue(keyTag, clone),
valueProvider.giveBlue(valueTag, clone)
);
redCopy.put(
valueProvider.giveRed(keyTag, clone),
valueProvider.giveBlue(valueTag, clone)
);

return Tuple.of(red, blue, redCopy);
}
Expand Down Expand Up @@ -202,19 +208,19 @@ public Tuple<T> createValues(
T blue = factory.get();
T redCopy = factory.get();
red.put(
valueProvider.giveRed(columnTag),
valueProvider.giveRed(rowTag),
valueProvider.giveBlue(valueTag)
valueProvider.giveRed(columnTag, clone),
valueProvider.giveRed(rowTag, clone),
valueProvider.giveBlue(valueTag, clone)
);
blue.put(
valueProvider.giveBlue(columnTag),
valueProvider.giveBlue(rowTag),
valueProvider.giveBlue(valueTag)
valueProvider.giveBlue(columnTag, clone),
valueProvider.giveBlue(rowTag, clone),
valueProvider.giveBlue(valueTag, clone)
);
redCopy.put(
valueProvider.giveRed(columnTag),
valueProvider.giveRed(rowTag),
valueProvider.giveBlue(valueTag)
valueProvider.giveRed(columnTag, clone),
valueProvider.giveRed(rowTag, clone),
valueProvider.giveBlue(valueTag, clone)
);

return Tuple.of(red, blue, redCopy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ public Tuple<T> createValues(

T red = ci.instantiate(
classes(parameterRawType),
objects(valueProvider.giveRed(singleParameterTag))
objects(valueProvider.giveRed(singleParameterTag, attributes))
);
T blue = ci.instantiate(
classes(parameterRawType),
objects(valueProvider.giveBlue(singleParameterTag))
objects(valueProvider.giveBlue(singleParameterTag, attributes))
);
T redCopy = ci.instantiate(
classes(parameterRawType),
objects(valueProvider.giveRed(singleParameterTag))
objects(valueProvider.giveRed(singleParameterTag, attributes))
);

return Tuple.of(red, blue, redCopy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ public void simple() {
assertFalse(red.equals(blue));
}

@Test
public void createSecondTimeIsNoOp() {
Point red = valueProvider.giveRed(POINT_TAG);
Point blue = valueProvider.giveBlue(POINT_TAG);

assertSame(red, valueProvider.giveRed(POINT_TAG));
assertSame(blue, valueProvider.giveBlue(POINT_TAG));
}

@Test
public void createEnum() {
assertNotNull(valueProvider.giveRed(ENUM_TAG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,11 @@ public class VintageValueProviderTest {

@BeforeEach
public void setUp() {
factoryCache.put(String.class, new AppendingStringTestFactory());
factoryCache.put(String.class, values("r", "b", new String("r")));
factoryCache.put(int.class, values(42, 1337, 42));
vp = new VintageValueProvider(TestValueProviders.empty(), cache, factoryCache, objenesis);
}

@Test
public void sanityTestFactoryIncreasesStringLength() {
AppendingStringTestFactory f = new AppendingStringTestFactory();
assertEquals("r", f.createValues(null, null, null).getRed());
assertEquals("rr", f.createValues(null, null, null).getRed());
assertEquals("rrr", f.createValues(null, null, null).getRed());
}

@Test
public void provide() {
Tuple<Point> actual = vp.provideOrThrow(POINT_TAG, Attributes.unlabeled());
Expand All @@ -59,36 +51,17 @@ public void giveRedFromFactory() {
assertEquals("r", vp.giveRed(STRING_TAG));
}

@Test
public void giveRedFromCache() {
vp.giveRed(STRING_TAG);
assertEquals("r", vp.giveRed(STRING_TAG));
}

@Test
public void giveBlueFromFactory() {
assertEquals("b", vp.giveBlue(STRING_TAG));
}

@Test
public void giveBlueFromCache() {
vp.giveBlue(STRING_TAG);
assertEquals("b", vp.giveBlue(STRING_TAG));
}

@Test
public void giveRedCopyFromFactory() {
assertEquals("r", vp.giveRedCopy(STRING_TAG));
assertNotSame(vp.giveRed(STRING_TAG), vp.giveRedCopy(STRING_TAG));
}

@Test
public void giveRedCopyFromCache() {
vp.giveRedCopy(STRING_TAG);
assertEquals("r", vp.giveRedCopy(STRING_TAG));
assertNotSame(vp.giveRed(STRING_TAG), vp.giveRedCopy(STRING_TAG));
}

@Test
public void giveRedFromFallbackFactory() {
Point actual = vp.giveRed(POINT_TAG);
Expand Down Expand Up @@ -194,28 +167,6 @@ public int hashCode() {
}
}

private static class AppendingStringTestFactory implements PrefabValueFactory<String> {

private String red;
private String blue;

public AppendingStringTestFactory() {
red = "";
blue = "";
}

@Override
public Tuple<String> createValues(
TypeTag tag,
VintageValueProvider valueProvider,
Attributes attributes
) {
red += "r";
blue += "b";
return new Tuple<>(red, blue, new String(red));
}
}

@SuppressWarnings("rawtypes")
private static final class ListTestFactory implements PrefabValueFactory<List> {

Expand Down
Loading

0 comments on commit e9749eb

Please sign in to comment.