annotate src/name/blackcap/exifwasher/Whitelist.kt @ 10:9bbe6d803eba

Fixed ShowDialog.
author David Barts <n5jrn@me.com>
date Fri, 10 Apr 2020 19:29:58 -0700
parents aafc9c127c7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * An exif key whitelist. Supports both prefixes and entire strings.
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 */
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 package name.blackcap.exifwasher
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.util.regex.Pattern
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import kotlin.collections.mutableSetOf
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import kotlin.collections.mutableListOf
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
9
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
10 class Whitelist: Cloneable {
2
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 private val entire = mutableSetOf<String>()
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 private val prefixes = mutableListOf<String>()
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
13
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 fun addEntire(s: String) = entire.add(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
15
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 fun addPrefix(s: String) = prefixes.add(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
17
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 private fun autoOp(s: String, pfx: (String) -> Boolean, ent: (String) -> Boolean): Boolean {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 return if (s.endsWith('*')) { pfx(s.dropLast(1)) } else { ent(s) }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
21
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 fun add(s: String) = autoOp(s, ::addPrefix, ::addEntire)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
23
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 fun removeEntire(s: String) = entire.remove(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
25
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 fun removePrefix(s: String) = prefixes.remove(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
27
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 fun remove(s: String) = autoOp(s, ::removePrefix, ::removeEntire)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
29
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 fun contains(s: String) = entire.contains(s) || prefixes.find { s.startsWith(it) } != null
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
31
6
aafc9c127c7b Fix many bugs; get settings (apparently) working.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
32 fun toList(): List<String> = prefixes.map { it + "*" } + entire
2
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
33
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 override fun toString(): String = toList().joinToString(",")
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
35
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
36 override public fun clone() = Whitelist().also { new ->
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
37 entire.forEach { new.addEntire(it) }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
38 prefixes.forEach { new.addPrefix(it) }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
39 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
40
2
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 companion object {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 private val SPLITTER = Pattern.compile(",\\s*")
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
43 fun parse(raw: String) = Whitelist().also {
2
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 for (s in raw.split(SPLITTER)) {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 it.add(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 }