annotate src/name/blackcap/exifwasher/Whitelist.kt @ 2:efd9fe2d70d7

Rationalize exceptions, code whitelist, add command-line tool.
author David Barts <n5jrn@me.com>
date Wed, 01 Apr 2020 14:23:54 -0700
parents
children 19c381c536ec
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
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 class Whitelist {
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
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 fun toList(): List<String> = mutableListOf<String>().also {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 it.addAll(entire)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 it.addAll(prefixes)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 it.sort()
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 }
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
37
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 override fun toString(): String = toList().joinToString(",")
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
39
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 companion object {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 private val SPLITTER = Pattern.compile(",\\s*")
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 fun parse(raw: String) = Whitelist().also {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 for (s in raw.split(SPLITTER)) {
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 it.add(s)
efd9fe2d70d7 Rationalize exceptions, code whitelist, add command-line tool.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 }
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 }