diff --git a/README.md b/README.md index 61d80a7..3f44d5c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Constants +# 🗜️ Constants [![](https://img.shields.io/maven-central/v/com.rezzedup.util/constants?color=ok&label=Maven%20Central)](https://search.maven.org/artifact/com.rezzedup.util/constants "Maven Central") [![](https://img.shields.io/badge/License-MPL--2.0-blue)](./LICENSE "Project License: MPL-2.0") @@ -8,14 +8,17 @@ Utilities for `static final` constants. Use 'em like enums. ```java +// Imagine you have constants of some kind of complicated generic type. public class Example { public static final ComplexObject STRING_CONSTANT = ComplexObject.builder("abc").example("xyz").enabled(true).build(); + // This one has an integer... the last one had a string! public static final ComplexObject INTEGER_CONSTANT = ComplexObject.builder(1).example(-1).enabled(true).build(); + // Well, this one won't be aggregated. @NotAggregated public static final ComplexObject FLOAT_CONSTANT = ComplexObject.builder(1.0F).example(-1.0F).enabled(false).build(); @@ -23,9 +26,14 @@ public class Example public static final ComplexObject DOUBLE_CONSTANT = ComplexObject.builder(1.0).example(-1.0).enabled(true).build(); + // And here we go! + // All the constants are collected into this immutable list, + // and their generic type is preserved. @AggregatedResult public static final List> VALUES = - Aggregates.list(Example.class, new TypeCapture>() {}); + Aggregates.fromThisClass() + .constantsOfType(new TypeCapture>() {}) + .toList(); } ``` diff --git a/src/test/java/com/rezzedup/util/constants/ReadMeExampleTests.java b/src/test/java/com/rezzedup/util/constants/ReadMeExampleTests.java new file mode 100644 index 0000000..2864042 --- /dev/null +++ b/src/test/java/com/rezzedup/util/constants/ReadMeExampleTests.java @@ -0,0 +1,115 @@ +/* + * Copyright © 2021, RezzedUp + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.rezzedup.util.constants; + +import com.rezzedup.util.constants.annotations.AggregatedResult; +import com.rezzedup.util.constants.annotations.NotAggregated; +import com.rezzedup.util.constants.types.TypeCapture; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import pl.tlinkowski.annotation.basic.NullOr; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +public class ReadMeExampleTests +{ + // Imagine you have constants of some kind of complicated generic type. + public static class Example + { + public static final ComplexObject STRING_CONSTANT = + ComplexObject.builder("abc").example("xyz").enabled(true).build(); + + // This one has an integer... the last one had a string! + public static final ComplexObject INTEGER_CONSTANT = + ComplexObject.builder(1).example(-1).enabled(true).build(); + + // Well, this one won't be aggregated. + @NotAggregated + public static final ComplexObject FLOAT_CONSTANT = + ComplexObject.builder(1.0F).example(-1.0F).enabled(false).build(); + + public static final ComplexObject DOUBLE_CONSTANT = + ComplexObject.builder(1.0).example(-1.0).enabled(true).build(); + + // And here we go! + // All the constants are collected into this immutable list, + // and their generic type is preserved. + @AggregatedResult + public static final List> VALUES = + Aggregates.fromThisClass() + .constantsOfType(new TypeCapture>() {}) + .toList(); + } + + public static class ComplexObject + { + public static Builder builder(T initial) + { + return new Builder<>(initial); + } + + private final T initial; + private final T example; + private final boolean enabled; + + private ComplexObject(T initial, T example, boolean enabled) + { + this.initial = initial; + this.example = example; + this.enabled = enabled; + } + + public T initial() { return initial; } + + public T example() { return example; } + + public boolean enabled() { return enabled; } + + public static class Builder + { + final T initial; + @NullOr T example; + boolean enabled; + + private Builder(T initial) + { + this.initial = initial; + } + + public Builder example(T example) + { + this.example = example; + return this; + } + + public Builder enabled(boolean enabled) + { + this.enabled = enabled; + return this; + } + + public ComplexObject build() + { + if (example == null) { throw new IllegalStateException("missing example"); } + return new ComplexObject<>(initial, example, enabled); + } + } + } + + @Nested + public class ExampleTest + { + @Test + public void hasThreeConstants() + { + assertEquals(3, Example.VALUES.size()); + } + } +}