From 8aa8fc3355325c58c772646b39a76d6d3399152b Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Tue, 14 Jan 2025 13:27:01 +0100 Subject: [PATCH] Add immutables library (#740) This PR introduces the Immutables library: https://immutables.github.io/ This library is widely used in Iceberg and many other projects. The ability to define immutable components in an easy way, without struggling with the nitty-gritty details (getters, builders, equals, hashCode, toString, etc.) is a nice productivity boost and improves code correctness by enforcing immutability by default. Unlike Java records, collections in immutable values are immutable as well. Immutable value also have extensive support for builders, which removes a lot of manual duplicated boilerplate code. This PR only adds the library, it is effectively a re-incarnation of #282. --- gradle/libs.versions.toml | 4 ++ gradle/projects.main.properties | 1 + tools/immutables/build.gradle.kts | 32 +++++++++++++++ .../polaris/immutables/PolarisImmutable.java | 39 +++++++++++++++++++ .../org.immutables.value.immutable | 20 ++++++++++ .../org.immutables.inhibit-classpath | 25 ++++++++++++ 6 files changed, 121 insertions(+) create mode 100644 tools/immutables/build.gradle.kts create mode 100644 tools/immutables/src/main/java/org/apache/polaris/immutables/PolarisImmutable.java create mode 100644 tools/immutables/src/main/resources/META-INF/annotations/org.immutables.value.immutable create mode 100644 tools/immutables/src/main/resources/META-INF/extensions/org.immutables.inhibit-classpath diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0daf5a88a..1b3a59708 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,6 +22,7 @@ hadoop = "3.4.1" iceberg = "1.7.1" junit = "5.11.4" quarkus = "3.17.6" +immutables = "2.10.1" picocli = "4.7.6" slf4j = "2.0.16" swagger = "1.6.14" @@ -54,6 +55,9 @@ hadoop-client-api = { module = "org.apache.hadoop:hadoop-client-api", version.re hadoop-client-runtime = { module = "org.apache.hadoop:hadoop-client-runtime", version.ref = "hadoop" } hadoop-common = { module = "org.apache.hadoop:hadoop-common", version.ref = "hadoop" } hadoop-hdfs-client = { module = "org.apache.hadoop:hadoop-hdfs-client", version.ref = "hadoop" } +immutables-builder = { module = "org.immutables:builder", version.ref = "immutables" } +immutables-value-annotations = { module = "org.immutables:value-annotations", version.ref = "immutables" } +immutables-value-processor = { module = "org.immutables:value-processor", version.ref = "immutables" } iceberg-bom = { module = "org.apache.iceberg:iceberg-bom", version.ref = "iceberg" } jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version = "2.18.2" } jakarta-annotation-api = { module = "jakarta.annotation:jakarta.annotation-api", version = "3.0.0" } diff --git a/gradle/projects.main.properties b/gradle/projects.main.properties index 2a6f86a0f..a61587932 100644 --- a/gradle/projects.main.properties +++ b/gradle/projects.main.properties @@ -29,6 +29,7 @@ polaris-eclipselink=extension/persistence/eclipselink polaris-jpa-model=extension/persistence/jpa-model polaris-tests=integration-tests aggregated-license-report=aggregated-license-report +polaris-immutables=tools/immutables polaris-container-spec-helper=tools/container-spec-helper polaris-version=tools/version diff --git a/tools/immutables/build.gradle.kts b/tools/immutables/build.gradle.kts new file mode 100644 index 000000000..67d8a4232 --- /dev/null +++ b/tools/immutables/build.gradle.kts @@ -0,0 +1,32 @@ +/* + * 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. + */ + +plugins { id("polaris-client") } + +val processor by configurations.creating + +processor.extendsFrom(configurations.api.get()) + +dependencies { + api(libs.immutables.builder) + api(libs.immutables.value.annotations) + + processor(libs.immutables.value.processor) + processor(project) +} diff --git a/tools/immutables/src/main/java/org/apache/polaris/immutables/PolarisImmutable.java b/tools/immutables/src/main/java/org/apache/polaris/immutables/PolarisImmutable.java new file mode 100644 index 000000000..6a16c98d1 --- /dev/null +++ b/tools/immutables/src/main/java/org/apache/polaris/immutables/PolarisImmutable.java @@ -0,0 +1,39 @@ +/* + * 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.polaris.immutables; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; +import org.immutables.value.Value; + +/** + * A Custom + * {@code @Value.Immutable} with a project-wide common style. + */ +@Documented +@Target(ElementType.TYPE) +@Value.Style( + defaults = @Value.Immutable(lazyhash = true), + forceJacksonPropertyNames = false, + clearBuilder = true, + depluralize = true, + toBuilder = "toBuilder", + get = {"get*", "is*"}) +public @interface PolarisImmutable {} diff --git a/tools/immutables/src/main/resources/META-INF/annotations/org.immutables.value.immutable b/tools/immutables/src/main/resources/META-INF/annotations/org.immutables.value.immutable new file mode 100644 index 000000000..3bcf09e4d --- /dev/null +++ b/tools/immutables/src/main/resources/META-INF/annotations/org.immutables.value.immutable @@ -0,0 +1,20 @@ +# +# 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. +# + +org.apache.polaris.immutables.PolarisImmutable diff --git a/tools/immutables/src/main/resources/META-INF/extensions/org.immutables.inhibit-classpath b/tools/immutables/src/main/resources/META-INF/extensions/org.immutables.inhibit-classpath new file mode 100644 index 000000000..f59ae3267 --- /dev/null +++ b/tools/immutables/src/main/resources/META-INF/extensions/org.immutables.inhibit-classpath @@ -0,0 +1,25 @@ +# +# 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. +# + +javax.annotation.Nullable +javax.annotation.CheckReturnValue; +javax.annotation.Nullable; +javax.annotation.ParametersAreNonnullByDefault; +javax.annotation.concurrent.Immutable; +javax.annotation.concurrent.NotThreadSafe;