Android Room type convert multiple enum types

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I am writing a type converter for my Room database. I have a couple custom enum classes and I want to convert all of them to its ordinals when stored in the database. So, instead of writing the following for every single class, is there any way to simplify it (such as pass in a generic enum type)?

class Converter {      @TypeConverter     fun toOrdinal(type: TypeA): Int = type.ordinal      @TypeConverter     fun toTypeA(ordinal: Int): TypeA = TypeA.values().first { it.ordinal == ordinal }      @TypeConverter     fun toOrdinal(type: TypeB): Int = type.ordinal      @TypeConverter     fun toTypeB(ordinal: Int): TypeB = TypeB.values().first { it.ordinal == ordinal }      ... } 

回答1:

As discussed here, Room can't handle generic converters at the moment. I think the best you can do is create extensions that make writing these enum converters quicker:

@Suppress("NOTHING_TO_INLINE") private inline fun <T : Enum<T>> T.toInt(): Int = this.ordinal  private inline fun <reified T : Enum<T>> Int.toEnum(): T = enumValues<T>()[this] 

This would simplify each pair of converters to this code:

@TypeConverter fun myEnumToTnt(value: MyEnum) = value.toInt() @TypeConverter fun intToMyEnum(value: Int) = value.toEnum<MyEnum>() 

Or if you might be storing null values:

@TypeConverter fun myEnumToTnt(value: MyEnum?) = value?.toInt() @TypeConverter fun intToMyEnum(value: Int?) = value?.toEnum<MyEnum>() 


回答2:

You can use a composition interface to achieve this as you cannot write a single converter class for multiple object types. It is kind of hacky but might just work:

interface BaseType {     val arg0: String      fun asString() : String? {         return when(this) {             is TypeA -> "${TypeA::class.simpleName}$separatorParam$arg0"             is TypeB -> "${TypeB::class.simpleName}$separatorParam$arg0"             else -> null         }     }      companion object {         const val separatorParam = "::"     } }  enum class TypeA (override val arg0: String) : BaseType {     A_ONE("argument 1"),     A_TWO("argument 2");      companion object {         fun getValueTypes(arg0: String) : TypeA? = values().firstOrNull { it.arg0 == arg0 }     } }  enum class TypeB (override val arg0: String) : BaseType {     A_ONE("argument 1"),     A_TWO("argument 2");      companion object {         fun getValueTypes(arg0: String) : TypeB? = values().firstOrNull { it.arg0 == arg0 }     } }  class Converter {     @TypeConverter     fun fromBaseType(type: BaseType) : String? = type.asString()      @TypeConverter     fun toBaseType(param: String?) : BaseType? = param?.asBaseType()      private fun String.asBaseType() : BaseType? {         val stringArray = this.split(BaseType.separatorParam)         val className : String? = stringArray[0]         return when(className) {             TypeA::class.simpleName -> TypeA.getValueTypes(stringArray[1])             TypeB::class.simpleName -> TypeB.getValueTypes(stringArray[1])             else -> null         }     } } 

Then you need a function in your data class to provide you the actual TypeA or TypeB

data class MyDbModel(val baseType: BaseType) {     inline fun <reified T: BaseType> getTypeAs() : T? =              when(baseType) {                 is TypeA -> TypeA.getValueTypes(baseType.arg0) as? T                 is TypeB -> TypeB.getValueTypes(baseType.arg0) as? T                 else -> null             } }  fun foo() {     val model = MyDbModel(TypeA.A_ONE)     val type = model.getTypeAs<TypeA>() } 

The disadvantage of this is that it works only for unique arg0 within the specific enum, for that you could use ordinal or you could use generated ID like R.id.a_one as the first parameter and then the second parameter could be your string.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!