annotate 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
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
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
32 fun toList(): List<String> = prefixes + 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
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
36 override public fun clone() = this().also { new ->
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*")
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents: 2
diff changeset
43 fun parse(raw: String) = this().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 }