Skip to content
Snippets Groups Projects
Commit badc61da authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Further cleanup

parent b5f7e0d3
No related branches found
No related tags found
No related merge requests found
...@@ -47,19 +47,16 @@ data class Flags<E>( ...@@ -47,19 +47,16 @@ data class Flags<E>(
companion object { companion object {
inline fun <reified T> of(int: Int): Flags<T> inline fun <reified T> of(int: Int): Flags<T>
where T : Flag<T>, T : Enum<T> = Flags( where T : Flag<T>, T : Enum<T> =
int, Flags(int, enumValues())
enumValues())
inline fun <reified T> of(vararg flags: T): Flags<T> inline fun <reified T> of(vararg flags: T): Flags<T>
where T : Flag<T>, T : Enum<T> = where T : Flag<T>, T : Enum<T> =
Flags(flags.map(Flag<T>::bit).distinct().sum(), Flags(flags.map(Flag<T>::bit).distinct().sum(), enumValues())
enumValues())
inline fun <reified T> of(flags: Iterable<T>): Flags<T> inline fun <reified T> of(flags: Iterable<T>): Flags<T>
where T : Flag<T>, T : Enum<T> = where T : Flag<T>, T : Enum<T> =
Flags(flags.map(Flag<T>::bit).distinct().sum(), Flags(flags.map(Flag<T>::bit).distinct().sum(), enumValues())
enumValues())
} }
interface Factory<E> where E : Flag<E>, E : Enum<E> { interface Factory<E> where E : Flag<E>, E : Enum<E> {
...@@ -72,59 +69,58 @@ data class Flags<E>( ...@@ -72,59 +69,58 @@ data class Flags<E>(
infix fun <T> Flags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : Flag<T> { infix fun <T> Flags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : Flag<T> {
// an Undefined flag is a special case. // an Undefined flag is a special case.
if (value == 0 || (value > 0 && which.bit == 0)) return false if (value == 0) return false
return value and which.bit == which.bit return value and which.bit == which.bit
} }
infix fun <T> Flags<T>.or(other: Int): Flags<T> infix fun <T> Flags<T>.or(other: Int): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value or other) Flags(value or other)
infix fun <T> Flags<T>.or(other: Flag<T>): Flags<T> infix fun <T> Flags<T>.or(other: Flag<T>): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value or other.bit) Flags(value or other.bit)
infix fun <T> Flags<T>.or(other: Flags<T>): Flags<T> infix fun <T> Flags<T>.or(other: Flags<T>): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value or other.value) Flags(value or other.value)
infix fun <T> Flags<T>.and(other: Int): Flags<T> infix fun <T> Flags<T>.and(other: Int): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value and other) Flags(value and other)
infix fun <T> Flags<T>.and(other: Flag<T>): Flags<T> infix fun <T> Flags<T>.and(other: Flag<T>): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value and other.bit) Flags(value and other.bit)
infix fun <T> Flags<T>.and(other: Flags<T>): Flags<T> infix fun <T> Flags<T>.and(other: Flags<T>): Flags<T>
where T : kotlin.Enum<T>, T : Flag<T> = Flags( where T : kotlin.Enum<T>, T : Flag<T> =
value and other.value) Flags(value and other.value)
infix operator fun <T> Flags<T>.plus(other: Int): Flags<T> infix operator fun <T> Flags<T>.plus(other: Int): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value or other) Flags(value or other)
infix operator fun <T> Flags<T>.plus(other: Flag<T>): Flags<T> infix operator fun <T> Flags<T>.plus(other: Flag<T>): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value or other.bit) Flags(value or other.bit)
infix operator fun <T> Flags<T>.plus(other: Flags<T>): Flags<T> infix operator fun <T> Flags<T>.plus(other: Flags<T>): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value or other.value) Flags(value or other.value)
infix operator fun <T> Flags<T>.minus(other: Int): Flags<T> infix operator fun <T> Flags<T>.minus(other: Int): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value and other.inv()) Flags(value and other.inv())
infix operator fun <T> Flags<T>.minus(other: Flag<T>): Flags<T> infix operator fun <T> Flags<T>.minus(other: Flag<T>): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value and other.bit.inv()) Flags(value and other.bit.inv())
infix operator fun <T> Flags<T>.minus(other: Flags<T>): Flags<T> infix operator fun <T> Flags<T>.minus(other: Flags<T>): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value and other.value.inv()) Flags(value and other.value.inv())
infix fun <T> Flags<T>.unset(which: T): Flags<T> infix fun <T> Flags<T>.unset(which: T): Flags<T>
where T : Enum<T>, T : Flag<T> = Flags( where T : Enum<T>, T : Flag<T> =
value xor which.bit) Flags(value xor which.bit)
...@@ -47,8 +47,8 @@ data class LongFlags<E>( ...@@ -47,8 +47,8 @@ data class LongFlags<E>(
companion object { companion object {
inline fun <reified T> of(int: Long): LongFlags<T> inline fun <reified T> of(int: Long): LongFlags<T>
where T : LongFlag<T>, T : Enum<T> = LongFlags( where T : LongFlag<T>, T : Enum<T> =
int, LongFlags(int,
enumValues()) enumValues())
inline fun <reified T> of(vararg flags: LongFlag<T>): LongFlags<T> inline fun <reified T> of(vararg flags: LongFlag<T>): LongFlags<T>
...@@ -71,59 +71,58 @@ data class LongFlags<E>( ...@@ -71,59 +71,58 @@ data class LongFlags<E>(
infix fun <T> LongFlags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : LongFlag<T> { infix fun <T> LongFlags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : LongFlag<T> {
// an Undefined flag is a special case. // an Undefined flag is a special case.
if (value == 0.toLong() || (value > 0 && which.bit == 0.toLong())) return false if (value == 0L) return false
return value and which.bit == which.bit return value and which.bit == which.bit
} }
infix fun <T> LongFlags<T>.or(other: Long): LongFlags<T> infix fun <T> LongFlags<T>.or(other: Long): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other) LongFlags(value or other)
infix fun <T> LongFlags<T>.or(other: LongFlag<T>): LongFlags<T> infix fun <T> LongFlags<T>.or(other: LongFlag<T>): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other.bit) LongFlags(value or other.bit)
infix fun <T> LongFlags<T>.or(other: LongFlags<T>): LongFlags<T> infix fun <T> LongFlags<T>.or(other: LongFlags<T>): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other.value) LongFlags(value or other.value)
infix fun <T> LongFlags<T>.and(other: Long): LongFlags<T> infix fun <T> LongFlags<T>.and(other: Long): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other) LongFlags(value and other)
infix fun <T> LongFlags<T>.and(other: LongFlag<T>): LongFlags<T> infix fun <T> LongFlags<T>.and(other: LongFlag<T>): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other.bit) LongFlags(value and other.bit)
infix fun <T> LongFlags<T>.and(other: LongFlags<T>): LongFlags<T> infix fun <T> LongFlags<T>.and(other: LongFlags<T>): LongFlags<T>
where T : kotlin.Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other.value) LongFlags(value and other.value)
infix operator fun <T> LongFlags<T>.plus(other: Long): LongFlags<T> infix operator fun <T> LongFlags<T>.plus(other: Long): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other) LongFlags(value or other)
infix operator fun <T> LongFlags<T>.plus(other: LongFlag<T>): LongFlags<T> infix operator fun <T> LongFlags<T>.plus(other: LongFlag<T>): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other.bit) LongFlags(value or other.bit)
infix operator fun <T> LongFlags<T>.plus(other: LongFlags<T>): LongFlags<T> infix operator fun <T> LongFlags<T>.plus(other: LongFlags<T>): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value or other.value) LongFlags(value or other.value)
infix operator fun <T> LongFlags<T>.minus(other: Long): LongFlags<T> infix operator fun <T> LongFlags<T>.minus(other: Long): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other.inv()) LongFlags(value and other.inv())
infix operator fun <T> LongFlags<T>.minus(other: LongFlag<T>): LongFlags<T> infix operator fun <T> LongFlags<T>.minus(other: LongFlag<T>): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other.bit.inv()) LongFlags(value and other.bit.inv())
infix operator fun <T> LongFlags<T>.minus(other: LongFlags<T>): LongFlags<T> infix operator fun <T> LongFlags<T>.minus(other: LongFlags<T>): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value and other.value.inv()) LongFlags(value and other.value.inv())
infix fun <T> LongFlags<T>.unset(which: T): LongFlags<T> infix fun <T> LongFlags<T>.unset(which: T): LongFlags<T>
where T : Enum<T>, T : LongFlag<T> = LongFlags( where T : Enum<T>, T : LongFlag<T> =
value xor which.bit) LongFlags(value xor which.bit)
...@@ -52,19 +52,16 @@ data class ShortFlags<E>( ...@@ -52,19 +52,16 @@ data class ShortFlags<E>(
companion object { companion object {
inline fun <reified T> of(int: Short): ShortFlags<T> inline fun <reified T> of(int: Short): ShortFlags<T>
where T : ShortFlag<T>, T : Enum<T> = ShortFlags( where T : ShortFlag<T>, T : Enum<T> =
int, ShortFlags(int, enumValues())
enumValues())
inline fun <reified T> of(vararg flags: ShortFlag<T>): ShortFlags<T> inline fun <reified T> of(vararg flags: ShortFlag<T>): ShortFlags<T>
where T : ShortFlag<T>, T : Enum<T> = where T : ShortFlag<T>, T : Enum<T> =
ShortFlags(flags.map(ShortFlag<T>::bit).distinct().sum().toShort(), ShortFlags(flags.map(ShortFlag<T>::bit).distinct().sum().toShort(), enumValues())
enumValues())
inline fun <reified T> of(flags: Iterable<T>): ShortFlags<T> inline fun <reified T> of(flags: Iterable<T>): ShortFlags<T>
where T : ShortFlag<T>, T : Enum<T> = where T : ShortFlag<T>, T : Enum<T> =
ShortFlags(flags.map(ShortFlag<T>::bit).distinct().sum().toShort(), ShortFlags(flags.map(ShortFlag<T>::bit).distinct().sum().toShort(), enumValues())
enumValues())
} }
interface Factory<E> where E : ShortFlag<E>, E : Enum<E> { interface Factory<E> where E : ShortFlag<E>, E : Enum<E> {
...@@ -82,53 +79,53 @@ infix fun <T> ShortFlags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : Sh ...@@ -82,53 +79,53 @@ infix fun <T> ShortFlags<T>.hasFlag(which: T): Boolean where T : Enum<T>, T : Sh
} }
infix fun <T> ShortFlags<T>.or(other: Short): ShortFlags<T> infix fun <T> ShortFlags<T>.or(other: Short): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value or other) ShortFlags(value or other)
infix fun <T> ShortFlags<T>.or(other: ShortFlag<T>): ShortFlags<T> infix fun <T> ShortFlags<T>.or(other: ShortFlag<T>): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value or other.bit) ShortFlags(value or other.bit)
infix fun <T> ShortFlags<T>.or(other: ShortFlags<T>): ShortFlags<T> infix fun <T> ShortFlags<T>.or(other: ShortFlags<T>): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value or other.value) ShortFlags(value or other.value)
infix fun <T> ShortFlags<T>.and(other: Short): ShortFlags<T> infix fun <T> ShortFlags<T>.and(other: Short): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value and other) ShortFlags(value and other)
infix fun <T> ShortFlags<T>.and(other: ShortFlag<T>): ShortFlags<T> infix fun <T> ShortFlags<T>.and(other: ShortFlag<T>): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value and other.bit) ShortFlags(value and other.bit)
infix fun <T> ShortFlags<T>.and(other: ShortFlags<T>): ShortFlags<T> infix fun <T> ShortFlags<T>.and(other: ShortFlags<T>): ShortFlags<T>
where T : kotlin.Enum<T>, T : ShortFlag<T> = ShortFlags( where T : kotlin.Enum<T>, T : ShortFlag<T> =
value and other.value) ShortFlags(value and other.value)
infix operator fun <T> ShortFlags<T>.plus(other: Short): ShortFlags<T> infix operator fun <T> ShortFlags<T>.plus(other: Short): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value or other) ShortFlags(value or other)
infix operator fun <T> ShortFlags<T>.plus(other: ShortFlag<T>): ShortFlags<T> infix operator fun <T> ShortFlags<T>.plus(other: ShortFlag<T>): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value or other.bit) ShortFlags(value or other.bit)
infix operator fun <T> ShortFlags<T>.plus(other: ShortFlags<T>): ShortFlags<T> infix operator fun <T> ShortFlags<T>.plus(other: ShortFlags<T>): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value or other.value) ShortFlags(value or other.value)
infix operator fun <T> ShortFlags<T>.minus(other: Short): ShortFlags<T> infix operator fun <T> ShortFlags<T>.minus(other: Short): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value and other.inv()) ShortFlags(value and other.inv())
infix operator fun <T> ShortFlags<T>.minus(other: ShortFlag<T>): ShortFlags<T> infix operator fun <T> ShortFlags<T>.minus(other: ShortFlag<T>): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value and other.bit.inv()) ShortFlags(value and other.bit.inv())
infix operator fun <T> ShortFlags<T>.minus(other: ShortFlags<T>): ShortFlags<T> infix operator fun <T> ShortFlags<T>.minus(other: ShortFlags<T>): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value and other.value.inv()) ShortFlags(value and other.value.inv())
infix fun <T> ShortFlags<T>.unset(which: T): ShortFlags<T> infix fun <T> ShortFlags<T>.unset(which: T): ShortFlags<T>
where T : Enum<T>, T : ShortFlag<T> = ShortFlags( where T : Enum<T>, T : ShortFlag<T> =
value xor which.bit) ShortFlags(value xor which.bit)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment