-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate enums as interface + enum + class for unknown values
The new generated enum is represented as an interface with two nested classes: - enum class Known with the list of known enumeration values - class Unknown which represents any not-known value The enums `enum` would be generated as: ```java interface Enum extends IKaitaiEnum { public static class Unknown extends IKaitaiEnum.Unknown implements Enum { private Unknown(long id) { super(id); } } public enum Known implements Enum { Variant1(1), Variant2(2), Variant(3); private final long id; static HashMap<Long, Enum> variants = new HashMap<>(3); static { for (final Known e : Known.values()) { variants.put(e.id, e); } } private Known(long id) { this.id = id; } @OverRide public long id() { return this.id; } } public static Enum byId(final long id) { return Known.variants.computeIfAbsent(id, _id -> new Unknown(id)); } } ``` Unfortunately, it is not possible to generate enum what will implement nested interface due to cyclic reference. If that would be possible, we would be protected against name clashes. In the current implementation the names Known and Unknown can clash with enum name itself
- Loading branch information
Showing
2 changed files
with
52 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters