You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CaseInsensitiveSet.kt 1.0KB

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. 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. operator fun contains(item: String) = items.any { caseMappingProvider().areEquivalent(it, item) }
  20. override operator fun iterator() = items.iterator().iterator()
  21. fun isEmpty() = items.isEmpty()
  22. }