comparison src/name/blackcap/exifwasher/Whitelist.kt @ 3:19c381c536ec

Code to make it a proper Mac GUI app. Untested!
author David Barts <n5jrn@me.com>
date Wed, 08 Apr 2020 20:29:12 -0700
parents efd9fe2d70d7
children dc1f4359659d
comparison
equal deleted inserted replaced
2:efd9fe2d70d7 3:19c381c536ec
5 5
6 import java.util.regex.Pattern 6 import java.util.regex.Pattern
7 import kotlin.collections.mutableSetOf 7 import kotlin.collections.mutableSetOf
8 import kotlin.collections.mutableListOf 8 import kotlin.collections.mutableListOf
9 9
10 class Whitelist { 10 class Whitelist: Cloneable {
11 private val entire = mutableSetOf<String>() 11 private val entire = mutableSetOf<String>()
12 private val prefixes = mutableListOf<String>() 12 private val prefixes = mutableListOf<String>()
13 13
14 fun addEntire(s: String) = entire.add(s) 14 fun addEntire(s: String) = entire.add(s)
15 15
27 27
28 fun remove(s: String) = autoOp(s, ::removePrefix, ::removeEntire) 28 fun remove(s: String) = autoOp(s, ::removePrefix, ::removeEntire)
29 29
30 fun contains(s: String) = entire.contains(s) || prefixes.find { s.startsWith(it) } != null 30 fun contains(s: String) = entire.contains(s) || prefixes.find { s.startsWith(it) } != null
31 31
32 fun toList(): List<String> = mutableListOf<String>().also { 32 fun toList(): List<String> = prefixes + entire
33 it.addAll(entire)
34 it.addAll(prefixes)
35 it.sort()
36 }
37 33
38 override fun toString(): String = toList().joinToString(",") 34 override fun toString(): String = toList().joinToString(",")
39 35
36 override public fun clone() = this().also { new ->
37 entire.forEach { new.addEntire(it) }
38 prefixes.forEach { new.addPrefix(it) }
39 }
40
40 companion object { 41 companion object {
41 private val SPLITTER = Pattern.compile(",\\s*") 42 private val SPLITTER = Pattern.compile(",\\s*")
42 fun parse(raw: String) = Whitelist().also { 43 fun parse(raw: String) = this().also {
43 for (s in raw.split(SPLITTER)) { 44 for (s in raw.split(SPLITTER)) {
44 it.add(s) 45 it.add(s)
45 } 46 }
46 } 47 }
47 } 48 }