How to get enum value of raw type from an enum class and a string in kotlin
Date : March 29 2020, 07:55 AM
I wish this help you I have the following code in java: , Here's a pure Kotlin version: @Suppress("UNCHECKED_CAST")
fun getEnumValue(enumClass: Class<*>, value: String): Enum<*> {
val enumConstants = enumClass.enumConstants as Array<out Enum<*>>
return enumConstants.first { it.name == value }
}
private enum class Hack
fun getEnumValue(enumClass: Class<*>, value: String): Enum<*> {
return helper<Hack>(enumClass, value)
}
private fun <T : Enum<T>>helper(enumClass: Class<*>, value: String): Enum<*> {
return java.lang.Enum.valueOf(enumClass as Class<T>, value)
}
enum class Color {
Red, Green, Blue
}
enumValueOf<Color>("Red")
|
Kotlin! How to create a inner enum class to get some property from outer class?
Date : March 29 2020, 07:55 AM
hop of those help? Enum constants need to exist without having to be initialized explicitly, whereas the surrounding class OutA needs to be created first. You cannot access non-static values from static contexts. Making the class a singleton, i.e. an object, will make the code compile though: object OutA {
var var1 = ""
enum class A2 {
;
init {
print(var1)
}
}
}
|
Replacing an Interface or Abstract Class with a Single Class and Enum Parameter (Kotlin)
Tag : oop , By : Caleb Ames
Date : March 29 2020, 07:55 AM
wish of those help for the suggestions and encouragement. From Yoni Gibbs' comment, Sealed Classes was the "trick" I was seeking, that is some combination of Enum and Polymorphism. In regards to Generics, it took two Types to encapsulate the underling storage object and how the pixels are represented. Here is the result that combines all the suggestions. //e.g. T=ByteArray, ShortArray
//e.g. E=Byte,Short
sealed class Frames<T,E>(val nrow:Int,val ncol:Int,val bitDepth:Int,val channelDepth:Int) {
val size=nrow*ncol //frame size
abstract val list:MutableList<T>
fun totalBytes() = list.size*size*bitDepth/8*channelDepth
abstract fun set(i:Int,data:E)
abstract fun get(i:Int): E
fun saveToDisk(filename:String)=saveByteArray(filename,toByteArray())
abstract fun toByteArray(isSmallEndian:Boolean=false):ByteArray
}
class Gray8Frame(nrow:Int,ncol:Int) :
Frames<ByteArray,Byte>(nrow,ncol,8,1) {
override val list= mutableListOf<ByteArray>()
override fun set(i: Int,data:Byte) {list[i/size][i%size]=data}
override fun get(i: Int)=list[i/size][i%size]
override fun toByteArray(isSmallEndian: Boolean)
= ByteArray(totalBytes()){get(it)}
}
class Gray16Frame(nrow:Int,ncol:Int) :
Frames<ShortArray,Short>(nrow,ncol,16,1) {
override val list= mutableListOf<ShortArray>()
override fun set(i: Int,data:Short) {list[i/size][i%size]=data}
override fun get(i: Int)=list[i/size][i%size]
override fun toByteArray(isSmallEndian: Boolean)
= list.flatMap { it.toByteList() }.toByteArray()
//implement short-->List<Byte>
}
class RGBFrame(nrow:Int,ncol:Int) :
Frames<ByteArray,List<Byte>>(nrow,ncol,8,3) {
override val list= mutableListOf<ByteArray>()
override fun set(i: Int,data:List<Byte>) {
list[i/size][3*i%size+0]=data[0]//Red
list[i/size][3*i%size+1]=data[1]//Green
list[i/size][3*i%size+2]=data[2]//Blue
}
override fun get(i: Int)=listOf(
list[i/size][3*i%size+0],//Red
list[i/size][3*i%size+1],//Green
list[i/size][3*i%size+2] //Blue
)
override fun toByteArray(isSmallEndian: Boolean)
= list.flatMap { it.asList() }.toByteArray()
}
fun saveByteArray(filename:String, byteArray: ByteArray) { } //save bytes here
|
Kotlin sealed class - how to sort by sealed class similar to sorting by enum
Date : March 29 2020, 07:55 AM
around this issue Maybe not completely what you are looking for, but maybe it is... While the sealed classes don't seem to have something like an ordinal, which you've already noticed, there is sealedSubclasses on the class itself (i.e. GoodCountries::class.sealedSubclasses). Also, it seems as if the order of the sealedSubclasses is the one of the defined classes, i.e. Brazil in this list always comes first, USA second, etc. The order is different if they aren't all nested (i.e. if some are outside, they are listed first). val entityList = listOf(Germany(), China(), USA(), Brazil(), Germany())
entityList.sortedBy { // or use sortedWith(compareBy {
GoodCountries::class.sealedSubclasses.indexOf(it::class)
}.forEach(::println) // or toList...
GoodCountries::class.sealedSubclasses
.asSequence()
.flatMap { klazzInOrder ->
entityList.asSequence().filter { it::class == klazzInOrder }
}
.forEach(::println)
println("Listing the sealed classes in the order of their declaration*")
GoodCountries::class.sealedSubclasses.forEach(::println)
println("Listing the sealed classes ordered by their simple name")
GoodCountries::class.sealedSubclasses.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.simpleName!! })
.forEach(::println)
// same result, but written differently
GoodCountries::class.sealedSubclasses.sortedBy { it.simpleName?.toLowerCase() }
.forEach(::println)
GoodCountries::class.sealedSubclasses.sortedWith(compareBy(nullsLast(String.CASE_INSENSITIVE_ORDER)) { it.simpleName })
.forEach(::println)
|
Access enum value inside a method of enum class in Kotlin
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can use this and this.ordinal which returns the ordinal of this enumeration constant Also if you do this: fun myFunc(): Any{
val array = MyEnum.values()
println(array[this.ordinal])
println(this)
return array[this.ordinal]
}
|