From a953f6524cbf17bed1fc43de8cc99bb7a98b0168 Mon Sep 17 00:00:00 2001 From: Callum Rogers Date: Mon, 16 May 2022 13:33:41 +0100 Subject: [PATCH 1/2] Use gradle-utils LazilyConfiguredMapping --- gradle-baseline-java/build.gradle | 1 + .../BaselineJavaVersionsExtension.java | 1 + .../javaversions/LazilyConfiguredMapping.java | 96 ---------------- .../LazilyConfiguredMappingTest.java | 106 ------------------ versions.lock | 1 + versions.props | 1 + 6 files changed, 4 insertions(+), 202 deletions(-) delete mode 100644 gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMapping.java delete mode 100644 gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMappingTest.java diff --git a/gradle-baseline-java/build.gradle b/gradle-baseline-java/build.gradle index 5a75e8dcb..213558a9a 100644 --- a/gradle-baseline-java/build.gradle +++ b/gradle-baseline-java/build.gradle @@ -21,6 +21,7 @@ dependencies { implementation 'org.ow2.asm:asm' implementation 'com.googlecode.java-diff-utils:diffutils' implementation 'com.diffplug.gradle:goomph' + implementation 'com.palantir.gradle.utils:lazily-configured-mapping' runtimeOnly 'com.palantir.javaformat:gradle-palantir-java-format' diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaVersionsExtension.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaVersionsExtension.java index f893893c7..9b0cbbb3f 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaVersionsExtension.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/BaselineJavaVersionsExtension.java @@ -16,6 +16,7 @@ package com.palantir.baseline.plugins.javaversions; +import com.palantir.gradle.utils.lazilyconfiguredmapping.LazilyConfiguredMapping; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; import javax.inject.Inject; diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMapping.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMapping.java deleted file mode 100644 index 94efb8e04..000000000 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMapping.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. - * - * Licensed 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 com.palantir.baseline.plugins.javaversions; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Supplier; -import javax.annotation.concurrent.GuardedBy; -import org.gradle.api.Action; - -final class LazilyConfiguredMapping { - private final Supplier valueFactory; - - @GuardedBy("this") - private final List> values = new ArrayList<>(); - - @GuardedBy("this") - private final Map> computedValues = new HashMap<>(); - - @GuardedBy("this") - private boolean finalized = false; - - LazilyConfiguredMapping(Supplier valueFactory) { - this.valueFactory = valueFactory; - } - - public synchronized void put(LazyValues lazyValues) { - ensureNotFinalized(); - - values.add(lazyValues); - } - - public synchronized void put(K key, Action value) { - ensureNotFinalized(); - - put((requestedKey, _ignored) -> { - if (requestedKey.equals(key)) { - return Optional.of(value); - } - - return Optional.empty(); - }); - } - - private void ensureNotFinalized() { - if (finalized) { - throw new IllegalStateException(String.format( - "This %s has already been finalized as get() hase been called. " - + "No further elements can be added to it", - LazilyConfiguredMapping.class.getSimpleName())); - } - } - - public synchronized Optional get(K key, A additionalData) { - finalized = true; - - return computedValues.computeIfAbsent(key, _ignored -> { - V value = valueFactory.get(); - AtomicBoolean created = new AtomicBoolean(false); - values.forEach(lazyValues -> { - lazyValues.compute(key, additionalData).ifPresent(action -> { - created.set(true); - action.execute(value); - }); - }); - - if (created.get()) { - return Optional.of(value); - } - - return Optional.empty(); - }); - } - - public interface LazyValues { - Optional> compute(K key, A additionalData); - } -} diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMappingTest.java b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMappingTest.java deleted file mode 100644 index 82fa86c9a..000000000 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/plugins/javaversions/LazilyConfiguredMappingTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * (c) Copyright 2022 Palantir Technologies Inc. All rights reserved. - * - * Licensed 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 com.palantir.baseline.plugins.javaversions; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; - -import java.util.Optional; -import org.junit.jupiter.api.Test; - -class LazilyConfiguredMappingTest { - private final LazilyConfiguredMapping lazilyConfiguredMapping = - new LazilyConfiguredMapping<>(() -> new Extension(0)); - - @Test - void empty_mapping_returns_optional_empty() { - assertThat(lazilyConfiguredMapping.get("abc", 'a')).isEmpty(); - } - - @Test - void can_put_a_value_and_get_it_out_again() { - lazilyConfiguredMapping.put("foo", extension -> extension.number = 4); - - assertThat(lazilyConfiguredMapping.get("foo", 'a')).hasValue(new Extension(4)); - } - - @Test - void can_put_a_lazy_value_in_and_get_it_out_again() { - lazilyConfiguredMapping.put((key, additionalData) -> { - assertThat(additionalData).isEqualTo('b'); - return Optional.of(extension -> extension.number = Integer.parseInt(key)); - }); - - assertThat(lazilyConfiguredMapping.get("3", 'b')).hasValue(new Extension(3)); - assertThat(lazilyConfiguredMapping.get("9", 'b')).hasValue(new Extension(9)); - } - - @Test - void lazy_values_are_able_to_not_return_values() { - lazilyConfiguredMapping.put((_key, _additionalData) -> Optional.empty()); - - assertThat(lazilyConfiguredMapping.get("abc", 'a')).isEmpty(); - } - - @Test - void interspersing_putting_values_takes_the_last_set_value() { - lazilyConfiguredMapping.put("1", extension -> extension.number = 80); - lazilyConfiguredMapping.put( - (key, _ignored) -> Optional.of(extension -> extension.number = Integer.parseInt(key))); - lazilyConfiguredMapping.put("4", extension -> extension.number = 99); - - assertThat(lazilyConfiguredMapping.get("1", 'c')).hasValue(new Extension(1)); - assertThat(lazilyConfiguredMapping.get("3", 'c')).hasValue(new Extension(3)); - assertThat(lazilyConfiguredMapping.get("4", 'c')).hasValue(new Extension(99)); - } - - @Test - void throws_if_putting_values_after_being_finalized() { - lazilyConfiguredMapping.get("abc", 'c'); - - assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> { - lazilyConfiguredMapping.put("foo", extension -> {}); - }); - - assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> { - lazilyConfiguredMapping.put((_key, _additionalData) -> Optional.of(extension -> {})); - }); - } - - private static final class Extension { - public int number; - - Extension(int number) { - this.number = number; - } - - @Override - public boolean equals(Object obj) { - return number == ((Extension) obj).number; - } - - @Override - public int hashCode() { - return number; - } - - @Override - public String toString() { - return Integer.toString(number); - } - } -} diff --git a/versions.lock b/versions.lock index 95e67b68c..956288b5d 100644 --- a/versions.lock +++ b/versions.lock @@ -37,6 +37,7 @@ com.google.truth:truth:1.1.3 (2 constraints: 1126a31d) com.googlecode.concurrent-trees:concurrent-trees:2.6.1 (1 constraints: 761166da) com.googlecode.java-diff-utils:diffutils:1.3.0 (1 constraints: 0605f935) com.googlecode.javaewah:JavaEWAH:1.1.12 (1 constraints: 750eee5e) +com.palantir.gradle.utils:lazily-configured-mapping:0.1.0 (1 constraints: 0305ee35) com.palantir.javaformat:gradle-palantir-java-format:1.1.0 (1 constraints: 0405f335) com.palantir.javaformat:palantir-java-format-spi:1.1.0 (1 constraints: 711560be) com.palantir.safe-logging:preconditions:1.26.0 (6 constraints: 4b58cd5b) diff --git a/versions.props b/versions.props index 3777927e0..b727e6d1f 100644 --- a/versions.props +++ b/versions.props @@ -15,6 +15,7 @@ com.googlecode.java-diff-utils:diffutils = 1.3.0 com.puppycrawl.tools:checkstyle = 10.2 com.diffplug.gradle:goomph = 3.35.0 org.checkerframework:* = 3.22.0 +com.palantir.gradle.utils:* = 0.1.0 # test deps com.fasterxml.jackson.*:* = 2.13.2 From 74c30fbe2d5c28fc2cc8dd5456dc391fefd64e9a Mon Sep 17 00:00:00 2001 From: svc-changelog Date: Mon, 16 May 2022 12:48:01 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-2276.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-2276.v2.yml diff --git a/changelog/@unreleased/pr-2276.v2.yml b/changelog/@unreleased/pr-2276.v2.yml new file mode 100644 index 000000000..882ff80bb --- /dev/null +++ b/changelog/@unreleased/pr-2276.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: Use `LazilyConfiguredMapping` that was extracted out to gradle-utils. + links: + - https://github.com/palantir/gradle-baseline/pull/2276