diff src/main/kotlin/name/blackcap/passman/Entry.kt @ 7:f245b9a53495

Efficiency improvements.
author David Barts <n5jrn@me.com>
date Tue, 20 Sep 2022 21:54:32 -0700
parents ad997df1f560
children 698c4a3d758d
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Entry.kt	Tue Sep 20 20:52:21 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Entry.kt	Tue Sep 20 21:54:32 2022 -0700
@@ -1,11 +1,19 @@
 package name.blackcap.passman
 
 import java.util.*
+import kotlin.reflect.KProperty
+import kotlin.reflect.full.declaredMemberProperties
 
 class Entry(val name: String, val username: String, val password: CharArray, val notes: String,
             val created: Date? = null, val modified: Date? = null, val accessed: Date? = null) {
 
     companion object {
+        private val FIELD_MAP = mutableMapOf<String, KProperty.Getter<*>>().apply {
+            Entry::class.declaredMemberProperties.forEach{
+                this[it.name] = it.getter
+            }
+        }
+
         fun withPromptedPassword() = Entry(
                 name = _getName(),
                 username = _getUsername(),
@@ -57,6 +65,10 @@
         printDate("Accessed", accessed)
     }
 
+    fun getField(name: String): Any? {
+        return FIELD_MAP[name]!!.call(this)
+    }
+
     private fun printDate(tag: String, date: Date?) {
         kotlin.io.print("${tag}: ")
         if (date == null) {