fix enums
This commit is contained in:
parent
45ec6d1428
commit
a99dab51b4
@ -76,7 +76,7 @@ val VarLongValueCodec = StreamCodec(DataInputStream::readVarLongLE, DataOutputSt
|
|||||||
val BinaryStringCodec = StreamCodec(DataInputStream::readBinaryString, DataOutputStream::writeBinaryString)
|
val BinaryStringCodec = StreamCodec(DataInputStream::readBinaryString, DataOutputStream::writeBinaryString)
|
||||||
|
|
||||||
class EnumValueCodec<V : Enum<V>>(clazz: Class<out V>, val writeByIndices: Boolean = false) : IStreamCodec<V> {
|
class EnumValueCodec<V : Enum<V>>(clazz: Class<out V>, val writeByIndices: Boolean = false) : IStreamCodec<V> {
|
||||||
private val values = clazz.enumConstants ?: throw ClassCastException("$clazz does not have enum constants. Not an enum?")
|
private val values = search(clazz) ?: throw ClassCastException("$clazz does not have enum constants. Not an enum?")
|
||||||
|
|
||||||
override fun read(stream: DataInputStream): V {
|
override fun read(stream: DataInputStream): V {
|
||||||
if (writeByIndices) {
|
if (writeByIndices) {
|
||||||
@ -102,6 +102,23 @@ class EnumValueCodec<V : Enum<V>>(clazz: Class<out V>, val writeByIndices: Boole
|
|||||||
override fun compare(a: V, b: V): Boolean {
|
override fun compare(a: V, b: V): Boolean {
|
||||||
return a === b
|
return a === b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
/**
|
||||||
|
* FIXME: enums with abstract methods which get compiled to subclasses, whose DO NOT expose "parent's" enum constants array
|
||||||
|
*
|
||||||
|
* is there an already existing solution?
|
||||||
|
*/
|
||||||
|
fun <V : Enum<V>> search(clazz: Class<out V>): Array<V>? {
|
||||||
|
var search: Class<*> = clazz
|
||||||
|
|
||||||
|
while (search.enumConstants == null && search.superclass != null) {
|
||||||
|
search = search.superclass
|
||||||
|
}
|
||||||
|
|
||||||
|
return search.enumConstants as? Array<V>
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun OutputStream.writeInt(value: Int) {
|
fun OutputStream.writeInt(value: Int) {
|
||||||
|
Loading…
Reference in New Issue
Block a user