Skip to content

Latest commit

 

History

History
106 lines (83 loc) · 3.93 KB

README.md

File metadata and controls

106 lines (83 loc) · 3.93 KB

🗜️ Constants

javadoc

Utilities for static final constants. Use 'em like enums.

// Imagine you have constants of some kind of complicated generic type.
public class Example
{
    public static final ComplexObject<String> 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> INTEGER_CONSTANT =
        ComplexObject.builder(1).example(-1).enabled(true).build();
    
    // Well, this one won't be aggregated.
    @NotAggregated
    public static final ComplexObject<Float> FLOAT_CONSTANT =
        ComplexObject.builder(1.0F).example(-1.0F).enabled(false).build();
    
    public static final ComplexObject<Double> 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<ComplexObject<?>> VALUES =
        Aggregates.fromThisClass()
            .constantsOfType(new TypeCapture<ComplexObject<?>>() {})
            .toList();
}

Maven

<dependency>
    <groupId>com.rezzedup.util</groupId>
    <artifactId>constants</artifactId>
    <version><!--release--></version>
</dependency>

Versions

Find available versions on the releases page of this repository.

Maven Central: https://search.maven.org/artifact/com.rezzedup.util/constants

Note: Snapshot Versions

ℹ️ Snapshot releases are available at the following repository:

<repositories>
    <repository>
        <id>ossrh-snapshots</id>
        <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
Note: Shading

ℹ️ If you intend to shade this library, please consider relocating the packages to avoid potential conflicts with other projects. This library also utilizes nullness annotations, which may be undesirable in a shaded uber-jar. They can safely be excluded, and you are encouraged to do so.

Documentation

Javadoc: https://javadoc.io/doc/com.rezzedup.util/constants

Rationale

This library automates aggregating constants via reflection. Or, in other words, it allows you to collect constants matching specific type and name criteria in an orderly, enum-like way.

Now, why not just use enums? While enums are among the most useful constructs offered by the Java language, they are, however, deliberately limited in which data they're capable of representing. Since all enums share a common supertype, they cannot extend any other class by design. Some data just isn't suitable for enums, such as instances of existing non-enum classes (or, constants).

Use this library when you need the 'collected' nature of an enum in conjunction with the flexibility of full, unbridled objects.

In order to adequately support generics, this library also includes various utilities for handling types, such as: a "super" type token (TypeCapture), casting utility (Cast), primitive auto-boxing utility (Primitives), and more. While seemingly unrelated, these tools are included here to avoid introducing any extra dependencies.