Skip to content

Commit

Permalink
Merge pull request #673 from HubSpot/optional-memoization-in-lazy-exp…
Browse files Browse the repository at this point in the history
…ressions

Make LazyExpression memoization disable-able
  • Loading branch information
jmaroeder authored May 27, 2021
2 parents bf40a88 + ecc7795 commit 52cef24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/java/com/hubspot/jinjava/interpret/LazyExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@
public class LazyExpression implements Supplier {
private final Supplier supplier;
private final String image;
private final Memoization memoization;
private Object jsonValue = null;

private LazyExpression(Supplier supplier, String image) {
public enum Memoization {
ON,
OFF
}

private LazyExpression(Supplier supplier, String image, Memoization memoization) {
this.supplier = supplier;
this.image = image;
this.memoization = memoization;
}

public static LazyExpression of(Supplier supplier, String image) {
return new LazyExpression(supplier, image);
return new LazyExpression(supplier, image, Memoization.ON);
}

public static LazyExpression of(
Supplier supplier,
String image,
Memoization memoization
) {
return new LazyExpression(supplier, image, memoization);
}

@Override
public Object get() {
if (jsonValue == null) {
if (jsonValue == null || memoization == Memoization.OFF) {
jsonValue = supplier.get();
}
return jsonValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.hubspot.jinjava.interpret;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.hubspot.jinjava.interpret.LazyExpression.Memoization;
import java.util.List;
import org.junit.Test;

public class LazyExpressionTest {
Expand All @@ -29,4 +32,31 @@ public void itSerializesNonEvaluatedValueToEmpty() throws JsonProcessingExceptio
);
assertThat(new ObjectMapper().writeValueAsString(expression)).isEqualTo("\"\"");
}

@Test
public void itMemoizesByDefault() {
List mock = mock(List.class);
LazyExpression expression = LazyExpression.of(mock::isEmpty, "");
expression.get();
expression.get();
verify(mock).isEmpty();
}

@Test
public void itAllowsMemoizationToBeDisabled() {
List mock = mock(List.class);
LazyExpression expression = LazyExpression.of(mock::isEmpty, "", Memoization.OFF);
expression.get();
expression.get();
verify(mock, times(2)).isEmpty();
}

@Test
public void itAllowsMemoizationToBeEnabled() {
List mock = mock(List.class);
LazyExpression expression = LazyExpression.of(mock::isEmpty, "", Memoization.ON);
expression.get();
expression.get();
verify(mock).isEmpty();
}
}

0 comments on commit 52cef24

Please sign in to comment.