From 9268c295fe4c094d3451caf20a275418836dda1e Mon Sep 17 00:00:00 2001 From: Jordan Zimmerman Date: Mon, 4 Oct 2021 09:56:04 +0100 Subject: [PATCH] Add RecordBuilderFull - Add `@RecordBuilderFull` which will have most optional features enabled - Change `@RecordBuilder.Template` to be `RetentionPolicy.CLASS` --- customizing.md | 6 +++ .../recordbuilder/core/RecordBuilder.java | 2 +- .../recordbuilder/core/RecordBuilderFull.java | 32 +++++++++++++++ .../recordbuilder/test/FullRecord.java | 26 +++++++++++++ .../test/TestRecordBuilderFull.java | 39 +++++++++++++++++++ 5 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilderFull.java create mode 100644 record-builder-test/src/main/java/io/soabase/recordbuilder/test/FullRecord.java create mode 100644 record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestRecordBuilderFull.java diff --git a/customizing.md b/customizing.md index f4fc644..dd0dae4 100644 --- a/customizing.md +++ b/customizing.md @@ -1,5 +1,11 @@ [◀︎ RecordBuilder](README.md) • Customizing RecordBuilder +# RecordBuilderFull + +Note: `@RecordBuilderFull` has most optional features enabled. It's an alternate +form of `@RecordBuilder` that uses the [templating mechanism](#create-a-custom-annotation) to +enable optional features. + # Customizing RecordBuilder RecordBuilder can be customized in a number of ways. The types of customizations will change over time. See diff --git a/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java index 8a90a69..2b15a67 100644 --- a/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java +++ b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilder.java @@ -166,7 +166,7 @@ public @interface RecordBuilder { boolean useImmutableCollections() default false; } - @Retention(RetentionPolicy.SOURCE) + @Retention(RetentionPolicy.CLASS) @Target(ElementType.ANNOTATION_TYPE) @Inherited @interface Template { diff --git a/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilderFull.java b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilderFull.java new file mode 100644 index 0000000..5259fe6 --- /dev/null +++ b/record-builder-core/src/main/java/io/soabase/recordbuilder/core/RecordBuilderFull.java @@ -0,0 +1,32 @@ +/** + * Copyright 2019 Jordan Zimmerman + * + * 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 io.soabase.recordbuilder.core; + +import java.lang.annotation.*; + +@RecordBuilder.Template(options = @RecordBuilder.Options( + interpretNotNulls = true, + useImmutableCollections = true +)) +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +@Inherited +/** + * An alternate form of {@code @RecordBuilder} that has most + * optional features turned on + */ +public @interface RecordBuilderFull { +} diff --git a/record-builder-test/src/main/java/io/soabase/recordbuilder/test/FullRecord.java b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/FullRecord.java new file mode 100644 index 0000000..a46b5eb --- /dev/null +++ b/record-builder-test/src/main/java/io/soabase/recordbuilder/test/FullRecord.java @@ -0,0 +1,26 @@ +/** + * Copyright 2019 Jordan Zimmerman + * + * 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 io.soabase.recordbuilder.test; + +import io.soabase.recordbuilder.core.RecordBuilderFull; + +import javax.validation.constraints.NotNull; +import java.util.List; +import java.util.Map; + +@RecordBuilderFull +public record FullRecord(@NotNull List numbers, @NotNull Map fullRecords) { +} diff --git a/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestRecordBuilderFull.java b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestRecordBuilderFull.java new file mode 100644 index 0000000..b0a7ec3 --- /dev/null +++ b/record-builder-test/src/test/java/io/soabase/recordbuilder/test/TestRecordBuilderFull.java @@ -0,0 +1,39 @@ +/** + * Copyright 2019 Jordan Zimmerman + * + * 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 io.soabase.recordbuilder.test; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.HashMap; + +class TestRecordBuilderFull { + @Test + void testNonNull() { + Assertions.assertThrows(NullPointerException.class, () -> FullRecordBuilder.builder().build()); + } + + @Test + void testImmutable() { + var record = FullRecordBuilder.builder() + .fullRecords(new HashMap<>()) + .numbers(new ArrayList<>()) + .build(); + Assertions.assertThrows(UnsupportedOperationException.class, () -> record.fullRecords().put(1, record)); + Assertions.assertThrows(UnsupportedOperationException.class, () -> record.numbers().add(1)); + } +}