Enum Enhancer

Library to provide helper methods for Java enums using Annotation processing

License

License

GroupId

GroupId

io.github.isharipov
ArtifactId

ArtifactId

enum-enhancer
Last Version

Last Version

0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Enum Enhancer
Library to provide helper methods for Java enums using Annotation processing
Project URL

Project URL

https://github.com/iSharipov/enum-enhancer
Source Code Management

Source Code Management

https://github.com/iSharipov/enum-enhancer

Download enum-enhancer

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.isharipov/enum-enhancer/ -->
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>enum-enhancer</artifactId>
    <version>0.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.isharipov/enum-enhancer/
implementation 'io.github.isharipov:enum-enhancer:0.1'
// https://jarcasting.com/artifacts/io.github.isharipov/enum-enhancer/
implementation ("io.github.isharipov:enum-enhancer:0.1")
'io.github.isharipov:enum-enhancer:jar:0.1'
<dependency org="io.github.isharipov" name="enum-enhancer" rev="0.1">
  <artifact name="enum-enhancer" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.isharipov', module='enum-enhancer', version='0.1')
)
libraryDependencies += "io.github.isharipov" % "enum-enhancer" % "0.1"
[io.github.isharipov/enum-enhancer "0.1"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.13
com.google.testing.compile : compile-testing jar 0.18

Project Modules

There are no modules declared in this project.

EnumEnhancer

Webp net-resizeimage

Java CI with Maven Total alerts Language grade: Java Maven Central
LinkedIn

What is EnumEnhancer

EnumEnhancer is a Java annotation processor for the generation of utility class for Java Enum class.

To enhance enum annotate it like this:

@EnumEnhancer
public enum Quarter {
    Q1(1),
    Q2(2),
    Q3(3),
    Q4(4);

    private final int quarter;

    Quarter(int quarter) {
        this.quarter = quarter;
    }

    public int getQuarter() {
        return quarter;
    }
}

Result

public class Quarter_ {
    public static final String Q1 = "Q1";
    public static final String Q2 = "Q2";
    public static final String Q3 = "Q3";
    public static final String Q4 = "Q4";

    public static Quarter fromQuarter(int quarter) {
        for (Quarter q : Quarter.values()) {
            if (q.getQuarter() == quarter) {
                return q;
            }
        }
        throw new IllegalArgumentException("No constant found");
    }

    public static <E> java.util.Map<Quarter, E> associate(java.util.function.Function<Quarter, E> transform) {
        return java.util.stream.Stream.of(Quarter.values()).collect(java.util.stream.Collectors.toMap(quarter -> quarter, transform));
    }

    public static java.util.List<Quarter> enumList() {
        return new java.util.ArrayList<>(java.util.Arrays.asList(Quarter.values()));
    }

    public static java.util.Map<String, Quarter> enumMap() {
        return java.util.stream.Stream.of(Quarter.values()).collect(java.util.stream.Collectors.toMap(Enum::name, quarter -> quarter));
    }
}

At compile time EnumEnhancer will generate a utility class. The generated class uses plain Java fields and methods, no reflection is involved.

EnumEnhancer can be applied

๐Ÿ”ฅ Polymorphic Deserialization using Jackson
We can't use enum's name's because of - An annotation argument must be a compile-time constant

Instead of using this

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "quarter",
        visible = true
)
@JsonSubTypes({
    @JsonSubTypes.Type(value = FirstQuarterDto.class,  name = Quarter.QuarterValue.Q1),
    @JsonSubTypes.Type(value = SecondQuarterDto.class, name = Quarter.QuarterValue.Q2),
    @JsonSubTypes.Type(value = ThirdQuarterDto.class,  name = Quarter.QuarterValue.Q3),
    @JsonSubTypes.Type(value = FourthQuarterDto.class, name = Quarter.QuarterValue.Q4)
})

where Quarter.QuarterValue.Q1-4 are:

public enum Quarter {
    Q1(1, QuarterValue.Q1),
    Q2(2, QuarterValue.Q2),
    Q3(3, QuarterValue.Q3),
    Q4(4, QuarterValue.Q4);

    public static class QuarterValue{
        public static String Q1 = "Q1";
        public static String Q2 = "Q2";
        public static String Q3 = "Q3";
        public static String Q4 = "Q4";
    }
}

use the class Quarter_:

@JsonSubTypes({
    @JsonSubTypes.Type(value = FirstQuarterDto.class,  name = Quarter_.Q1),
    @JsonSubTypes.Type(value = SecondQuarterDto.class, name = Quarter_.Q2),
    @JsonSubTypes.Type(value = ThirdQuarterDto.class,  name = Quarter_.Q3),
    @JsonSubTypes.Type(value = FourthQuarterDto.class, name = Quarter_.Q4)
})   

๐Ÿ”ฅ Getting enum associated with
Sometimes we need a Map with enum and associated values

Map<Quarter, Integer> associated = Quarter_.associate(Quarter::getQuarter);
Map<Quarter, String> associated = Quarter_.associate(quarter -> {
    String months;
    switch (quarter){
        case Q1: months = "January, February, and March";    break;
        case Q2: months = "April, May, and June ";           break;
        case Q3: months =  "July, August, and September";    break;
        case Q4: months = "October, November, and December"; break;
        default:
            throw new IllegalStateException("Unexpected value: " + quarter);
    }
    return months;
});    

๐Ÿ”ฅ Getting List or Map of enum

List<Quarter> quarters = Quarter_.enumList();
Map<String, Quarter> quarters = Quarter_.enumMap();

๐Ÿ”ฅ Getting enum by constructor parameter
Processor generates fromParameter method for each constructor parameter

Quarter quarter = Quarter_.fromQuarter(1);

Using EnumEnhancer

EnumEnhancer works in command line builds and IDEs.

Gradle

buildscript {
    dependencies {
        classpath("net.ltgt.gradle:gradle-apt-plugin:0.21")
    }
}     
apply plugin: 'net.ltgt.apt'

dependencies {
    compileOnly("io.github.isharipov:enum-enhancer:0.1")
    annotationProcessor("io.github.isharipov:enum-enhancer:0.1")
}

Maven

<dependencies>
    <dependency>
        <groupId>io.github.isharipov</groupId>
        <artifactId>enum-enhancer</artifactId>
        <version>0.1</version>
        <scope>compile</scope>
    </dependency>
<dependencies>

Requirements

EnumEnhancer requires Java 1.8 or later.

Versions

Version
0.1