beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lc...@apache.org
Subject [1/2] beam git commit: Update ValueProvider.java
Date Thu, 29 Dec 2016 18:40:27 GMT
Repository: beam
Updated Branches:
  refs/heads/master 6f4031adc -> 135d09f5e


Update ValueProvider.java


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/055beedb
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/055beedb
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/055beedb

Branch: refs/heads/master
Commit: 055beedb900bb1e076e1e50419c3c6122b18cb69
Parents: 6f4031a
Author: sammcveety <sam.mcveety@gmail.com>
Authored: Tue Dec 27 06:56:53 2016 -0800
Committer: Luke Cwik <lcwik@google.com>
Committed: Thu Dec 29 10:39:19 2016 -0800

----------------------------------------------------------------------
 .../apache/beam/sdk/options/ValueProvider.java  |  6 ++++-
 .../beam/sdk/options/ValueProviderTest.java     | 24 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/055beedb/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java
index 93fcaf8..030eed5 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ValueProvider.java
@@ -108,6 +108,7 @@ public interface ValueProvider<T> extends Serializable {
 
     private final ValueProvider<X> value;
     private final SerializableFunction<X, T> translator;
+    private transient volatile T cachedValue;
 
     NestedValueProvider(ValueProvider<X> value, SerializableFunction<X, T> translator)
{
       this.value = checkNotNull(value);
@@ -125,7 +126,10 @@ public interface ValueProvider<T> extends Serializable {
 
     @Override
     public T get() {
-      return translator.apply(value.get());
+      if (cachedValue == null) {
+        cachedValue = translator.apply(value.get());
+      }
+      return cachedValue;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/beam/blob/055beedb/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java
index ea5cc54..5b76d53 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ValueProviderTest.java
@@ -19,11 +19,13 @@ package org.apache.beam.sdk.options;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.List;
 import org.apache.beam.sdk.options.ValueProvider.NestedValueProvider;
 import org.apache.beam.sdk.options.ValueProvider.RuntimeValueProvider;
@@ -289,4 +291,26 @@ public class ValueProviderTest {
         StaticValueProvider.of("foo"), new NonSerializableTranslator());
     SerializableUtils.ensureSerializable(nvp);
   }
+
+  private static class IncrementAtomicIntegerTranslator
+      implements SerializableFunction<AtomicInteger, Integer> {
+    @Override
+    public Integer apply(AtomicInteger from) {
+      return from.incrementAndGet();
+    }
+  }
+
+  @Test
+  public void testNestedValueProviderCached() throws Exception {
+    AtomicInteger increment = new AtomicInteger();
+    ValueProvider<Integer> nvp = NestedValueProvider.of(
+        StaticValueProvider.of(increment), new IncrementAtomicIntegerTranslator());
+    Integer originalValue = nvp.get();
+    Integer cachedValue = nvp.get();
+    Integer incrementValue = increment.incrementAndGet();
+    Integer secondCachedValue = nvp.get();
+    assertEquals(originalValue, cachedValue);
+    assertEquals(secondCachedValue, cachedValue);
+    assertNotEquals(originalValue, incrementValue);
+  }
 }


Mime
View raw message