Skip to content

Commit e52d7fc

Browse files
committed
Implement runtime support for non-exhaustive enums in Java
1 parent ebace6b commit e52d7fc

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2015-2024 Kaitai Project: MIT license
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files (the
6+
* "Software"), to deal in the Software without restriction, including
7+
* without limitation the rights to use, copy, modify, merge, publish,
8+
* distribute, sublicense, and/or sell copies of the Software, and to
9+
* permit persons to whom the Software is furnished to do so, subject to
10+
* the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
package io.kaitai.struct;
24+
25+
/**
26+
* Interface, implemented by every enum class generated by Kaitai Struct Compiler,
27+
* which provides assess to the underlying integer value.
28+
*
29+
* Each generated enum is represented by it's own interface and pair of nested
30+
* classes: enum class and ordinal class:
31+
*
32+
* <code><pre>
33+
* // marker interface
34+
* public interface <kaitai-enum-name> extends IKaitaiEnum {
35+
* // marker class for values not explicitly listed in KSY
36+
* public final class Unknown extends IKaitaiEnum.Unknown implements <kaitai-enum-name> {
37+
* Unknown(long id) { super(id); }
38+
* }
39+
*
40+
* public enum Known implements <kaitai-enum-name> {
41+
* // ...values...
42+
*
43+
* Known(long id) { super(id); }
44+
*
45+
* @Override
46+
* public long id() { ... }
47+
* }
48+
* // returns either Known.<...> or Unknown(id)
49+
* // Each Unknown created only once
50+
* public static <kaitai-enum-name> byId(long id) {
51+
* // ...
52+
* }
53+
* }
54+
* </pre></code>
55+
*
56+
* @since 0.11
57+
*/
58+
public interface IKaitaiEnum {
59+
/**
60+
* Base class for all unknown variants of Kaitai-generated enums.
61+
*
62+
* If you want to determine is the variant that you have is known or not,
63+
* you can use {@code <variant> instanceof IKaitaiEnum.Unknown}. The {@link #name()}
64+
* of such variant is always {@code null}.
65+
*/
66+
public abstract class Unknown implements IKaitaiEnum {
67+
private final long id;
68+
protected Unknown(long id) { this.id = id; }
69+
70+
@Override
71+
public String name() { return null; }
72+
73+
@Override
74+
public long id() { return id; }
75+
}
76+
77+
/**
78+
* Returns a symbolic name of this enum variant or {@code null} if this
79+
* object represents {@link Unknown unknown} variant.
80+
*/
81+
public String name();
82+
83+
/** Returns the underlying number which represents this enum variant. */
84+
public long id();
85+
}

0 commit comments

Comments
 (0)