|
| 1 | +package com.hoc.flowmvi.core |
| 2 | + |
| 3 | +/** |
| 4 | + * `NonEmptySet` is a data type used to model sets that guarantee to have at least one value. |
| 5 | + */ |
| 6 | +class NonEmptySet<out T> |
| 7 | +@Throws(IllegalArgumentException::class) |
| 8 | +private constructor(val set: Set<T>) : AbstractSet<T>() { |
| 9 | + init { |
| 10 | + require(set.isNotEmpty()) { "Set must not be empty" } |
| 11 | + require(set !is NonEmptySet<T>) { "Set must not be NonEmptySet" } |
| 12 | + } |
| 13 | + |
| 14 | + override val size: Int get() = set.size |
| 15 | + override fun iterator(): Iterator<T> = set.iterator() |
| 16 | + override fun isEmpty(): Boolean = false |
| 17 | + |
| 18 | + operator fun plus(l: NonEmptySet<@UnsafeVariance T>): NonEmptySet<T> = |
| 19 | + NonEmptySet(set + l.set) |
| 20 | + |
| 21 | + @Suppress("RedundantOverride") |
| 22 | + override fun equals(other: Any?): Boolean = super.equals(other) |
| 23 | + |
| 24 | + @Suppress("RedundantOverride") |
| 25 | + override fun hashCode(): Int = super.hashCode() |
| 26 | + |
| 27 | + override fun toString(): String = |
| 28 | + "NonEmptySet(${set.joinToString()})" |
| 29 | + |
| 30 | + companion object { |
| 31 | + /** |
| 32 | + * Creates a [NonEmptySet] from the given [Collection]. |
| 33 | + * @return null if [this] is empty. |
| 34 | + */ |
| 35 | + @JvmStatic |
| 36 | + fun <T> Collection<T>.toNonEmptySetOrNull(): NonEmptySet<T>? = |
| 37 | + if (isEmpty()) null else NonEmptySet(toSet()) |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a [NonEmptySet] from the given [Set]. |
| 41 | + * @return null if [this] is empty. |
| 42 | + */ |
| 43 | + @JvmStatic |
| 44 | + fun <T> Set<T>.toNonEmptySetOrNull(): NonEmptySet<T>? = (this as? NonEmptySet<T>) |
| 45 | + ?: if (isEmpty()) null else NonEmptySet(this) |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a [NonEmptySet] from the given values. |
| 49 | + */ |
| 50 | + @JvmStatic |
| 51 | + fun <T> of(element: T, vararg elements: T): NonEmptySet<T> = NonEmptySet( |
| 52 | + buildSet(capacity = 1 + elements.size) { |
| 53 | + add(element) |
| 54 | + addAll(elements) |
| 55 | + } |
| 56 | + ) |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates a [NonEmptySet] that contains only the specified [element]. |
| 60 | + */ |
| 61 | + @JvmStatic |
| 62 | + fun <T> of(element: T): NonEmptySet<T> = NonEmptySet(setOf(element)) |
| 63 | + } |
| 64 | +} |
0 commit comments