@ToString on static enums

651 views
Skip to first unread message

George Stanchev

unread,
Aug 9, 2017, 5:38:39 PM8/9/17
to Project Lombok
Hi,

Currently we write code as this [1]. Is there any way to reduce boilerplate on such enums using Lombok? The @ToString doesn't look too useful...

George



[1]
public static enum MyEnum {
   ONE("ONE), TWO("TWO), THREE("THREE");
   private final String asString;
   MyEnum(String asString) { this.asString = asString; }
   @Override public String toString() { return asString; }
}

Martin Grajcar

unread,
Aug 9, 2017, 5:42:55 PM8/9/17
to project...@googlegroups.com
Not perfect, but shorter.

When you want to set the value manually:

@RequiredArgsConstructor
public static enum MyEnum {
   ONE("ONE), TWO("TWO), THREE("THREE");
   @Getter private final String toString;
}

When there's a rule:

public static enum MyEnum {
   ONE, TWO, THREE;
   MyEnum(String asString) { toString = name().substring(0, 1); }
   @Getter private final String toString;
}


--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

George Stanchev

unread,
Aug 9, 2017, 6:07:54 PM8/9/17
to Project Lombok
Thank you. Had to add @Accessors(fluent = true) to avoid the getToString() naming but other than that - it worked
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages