Commit 240999c
committed
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 itself1 parent 983060d commit 240999c
File tree
2 files changed
+52
-15
lines changed2 files changed
+52
-15
lines changedLines changed: 51 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
524 | 524 | | |
525 | 525 | | |
526 | 526 | | |
527 | | - | |
| 527 | + | |
528 | 528 | | |
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
532 | | - | |
533 | | - | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
534 | 535 | | |
535 | 536 | | |
536 | | - | |
537 | | - | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
538 | 541 | | |
539 | 542 | | |
540 | | - | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
541 | 553 | | |
| 554 | + | |
| 555 | + | |
542 | 556 | | |
543 | 557 | | |
544 | 558 | | |
| |||
551 | 565 | | |
552 | 566 | | |
553 | 567 | | |
| 568 | + | |
554 | 569 | | |
555 | 570 | | |
556 | 571 | | |
| |||
690 | 705 | | |
691 | 706 | | |
692 | 707 | | |
693 | | - | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
694 | 720 | | |
695 | 721 | | |
696 | 722 | | |
| |||
705 | 731 | | |
706 | 732 | | |
707 | 733 | | |
708 | | - | |
709 | | - | |
710 | | - | |
| 734 | + | |
711 | 735 | | |
712 | 736 | | |
713 | | - | |
| 737 | + | |
714 | 738 | | |
715 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
716 | 749 | | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
717 | 755 | | |
718 | 756 | | |
719 | | - | |
720 | 757 | | |
721 | 758 | | |
722 | 759 | | |
723 | | - | |
724 | 760 | | |
| 761 | + | |
725 | 762 | | |
726 | 763 | | |
727 | 764 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
0 commit comments