Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CaseInsensitiveSet.kt 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. internal operator fun plusAssign(item: String) {
  12. if (!contains(item)) {
  13. items += item
  14. }
  15. }
  16. internal operator fun minusAssign(item: String) {
  17. items.removeIf { caseMappingProvider().areEquivalent(it, item) }
  18. }
  19. /**
  20. * Determines if this set contains the given item, case-insensitively.
  21. */
  22. operator fun contains(item: String) = items.any { caseMappingProvider().areEquivalent(it, item) }
  23. /**
  24. * Returns a read-only iterator over items in this set.
  25. */
  26. override operator fun iterator() = items.iterator().iterator()
  27. /**
  28. * Returns true if this set is empty.
  29. */
  30. fun isEmpty() = items.isEmpty()
  31. }