Skip to content

Commit 65dc95f

Browse files
committed
Implement runtime support for non-exhaustive enums in Java
1 parent 20af3ac commit 65dc95f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
24+
package io.kaitai.struct;
25+
26+
/**
27+
* Interface, implemented by every enum class generated by Kaitai Struct compiler.
28+
*
29+
* @since 0.11
30+
*/
31+
public interface IKaitaiEnum {
32+
/**
33+
* Base class for all unknown variants of Kaitai-generated enums.
34+
*
35+
* If you want to determine is the variant that you have is known or not,
36+
* you can use {@code <variant> instanceof IKaitaiEnum.Unknown}.
37+
*
38+
* The descendants of this class have value semantic, like enums: their
39+
* {@code equals} method compares inner long values of the same class (so
40+
* {@code Unknown} values of different Kaitai enums are not equal) and
41+
* {@code hashCode} method returns {@code Long.hashCode(this.id())}.
42+
*/
43+
public abstract class Unknown implements IKaitaiEnum {
44+
protected final long id;
45+
protected Unknown(long id) { this.id = id; }
46+
47+
@Override
48+
public int hashCode() { return Long.hashCode(id); }
49+
50+
@Override
51+
public long id() { return id; }
52+
}
53+
54+
/** Returns the underlying number which represents this enum variant. */
55+
public long id();
56+
}

0 commit comments

Comments
 (0)