This is the old documentation of lombok-pg. The new version can be found in the wiki of the github repository. Take me to the new Version then!

@EnumId

Overview

There are situations where enum.valueOf() just isn't enough, where you need other fields to be indentifier for all the different enum values. Doing that usually involved a fair amount of boilerplate. Now it's just one simple annotation.

With Lombok

01 import lombok.EnumId;
02 import lombok.RequiredArgsConstructor;
03 import lombok.Getter;
04 
05 public class EnumIdExample {
06   @RequiredArgsConstructor
07   public enum Status {
08     WAITING(0),
09     READY(1),
10     SKIPPED(-1),
11     COMPLETED(5);
12     
13     @EnumId
14     @Getter
15     private final int code;
16   }
17 }

Vanilla Java

01 class EnumIdExample {
02   public enum Status {
03     WAITING(0),
04     READY(1),
05     SKIPPED(-1),
06     COMPLETED(5);
07     
08     private static final java.util.Map<java.lang.Integer, Status> $CODE_LOOKUP = new java.util.HashMap<java.lang.Integer, Status>();
09     static {
10       for (Status status : Status.values()) {
11         $CODE_LOOKUP.put(status.code, status);
12       }
13     }
14     
15     private final int code;
16     
17     private Status(final int code) {
18       this.code = code;
19     }
20     
21     public int getCode() {
22       return this.code;
23     }
24     
25     public static Status findByCode(final int code) {
26       if ($CODE_LOOKUP.containsKey(code)) {
27         return $CODE_LOOKUP.get(code);
28       }
29       throw new java.lang.IllegalArgumentException(java.lang.String.format("Enumeration \'Status\' has no value \'%s\'", code));
30     }
31   }
32 }

Small print

Smallprint