This is an automated email from the ASF dual-hosted git repository. chesnay pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/flink.git
commit d4a0251a08ae1d5f931ab24dc898faea16e2a9a2 Author: Chesnay Schepler <[email protected]> AuthorDate: Wed Nov 3 13:59:26 2021 +0100 [FLINK-24749] Add CachingSupplier --- .../flink/util/function/CachingSupplier.java | 42 ++++++++++++++++++++++ .../flink/util/function/CachingSupplierTest.java | 39 ++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/flink-core/src/main/java/org/apache/flink/util/function/CachingSupplier.java b/flink-core/src/main/java/org/apache/flink/util/function/CachingSupplier.java new file mode 100644 index 0000000..d1bfce1 --- /dev/null +++ b/flink-core/src/main/java/org/apache/flink/util/function/CachingSupplier.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.util.function; + +import javax.annotation.Nullable; +import javax.annotation.concurrent.NotThreadSafe; + +import java.util.function.Supplier; + +/** A {@link Supplier} that returns a single, lazily instantiated, value. */ +@NotThreadSafe +public class CachingSupplier<T> implements Supplier<T> { + private final Supplier<T> backingSupplier; + private @Nullable T cachedValue; + + public CachingSupplier(Supplier<T> backingSupplier) { + this.backingSupplier = backingSupplier; + } + + @Override + public T get() { + if (cachedValue == null) { + cachedValue = backingSupplier.get(); + } + return cachedValue; + } +} diff --git a/flink-core/src/test/java/org/apache/flink/util/function/CachingSupplierTest.java b/flink-core/src/test/java/org/apache/flink/util/function/CachingSupplierTest.java new file mode 100644 index 0000000..8826887 --- /dev/null +++ b/flink-core/src/test/java/org/apache/flink/util/function/CachingSupplierTest.java @@ -0,0 +1,39 @@ +package org.apache.flink.util.function; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +class CachingSupplierTest { + + @Test + void testCaching() { + final AtomicInteger instantiationCounts = new AtomicInteger(); + final Supplier<Integer> backingSupplier = () -> instantiationCounts.incrementAndGet(); + final CachingSupplier<Integer> cachingSupplier = new CachingSupplier<>(backingSupplier); + + assertThat(cachingSupplier.get(), is(cachingSupplier.get())); + assertThat(instantiationCounts.get(), is(1)); + } +}
