Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CaseInsensitiveSet.kt 1000B

12345678910111213141516171819202122232425262728293031
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. /**
  4. * Maintains a set of strings that are compared case-insensitively according to the provided [CaseMapping]
  5. *
  6. * If the case mapping changes during the lifetime of this set, it is presumed that all items will also be
  7. * unique in the new case mapping. Otherwise, the behaviour of this class is undefined.
  8. */
  9. class CaseInsensitiveSet(private val caseMappingProvider: () -> CaseMapping) : Iterable<String> {
  10. private val items = HashSet<String>()
  11. operator fun plusAssign(item: String) {
  12. if (!contains(item)) {
  13. items += item
  14. }
  15. }
  16. operator fun minusAssign(item: String) {
  17. items.removeIf { caseMappingProvider().areEquivalent(it, item) }
  18. }
  19. operator fun contains(item: String) = items.any { caseMappingProvider().areEquivalent(it, item) }
  20. override operator fun iterator() = items.iterator()
  21. fun isEmpty() = items.isEmpty()
  22. }