commonMain.dk.cachet.carp.common.application.EnumObjectMap.kt Maven / Gradle / Ivy
Go to download
Helper classes and base types relied upon by all subsystems. This library does not contain any domain logic.
The newest version!
package dk.cachet.carp.common.application
/**
* A helper class to construct iterable objects which hold [V] member definitions indexed on [K].
* This is similar to an enum, but removes the need for an intermediate enum type and generic type parameters are retained per member.
*
* Extend from this class as an object and assign members as follows: `val MEMBER = add( SomeMember() )`.
*/
open class EnumObjectMap private constructor(
private val map: MutableMap,
val keyOf: (V) -> K
) : Map by map
{
constructor(
/**
* Specifies how to retrieve the key for the specified element.
*/
keyOf: (V) -> K
) : this( mutableMapOf(), keyOf )
/**
* Add an element using the key which is extracted from [item] using [keyOf].
*
* @throws IllegalArgumentException in case the extracted from [item] is already present in this map.
*/
protected fun add( item: TAdd ): TAdd = item.also {
val key = keyOf( it )
require( !map.contains( key ) ) { "An item with the same key is already present." }
map[ key ] = it
}
}