/
Pref.scala
57 lines (49 loc) · 2.13 KB
/
Pref.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.gerweck.scala.util.prefs
import java.util.prefs.Preferences
/** A class that represents a preference stored in the Java preferences system.
*
* The expectation is that your application will make a global registry of
* your preferences as `val`s in a central object, and then you'll then
* import and use those as needed throughout the application.
*/
class Pref[A] protected (path: String)(implicit handler: Pref.AccessHandler[A], pref: Preferences) {
def apply(): A = handler.read(path)
def update(a: A): Unit = handler.write(path, a)
def reset(): Unit = handler.reset(path)
}
object Pref {
def apply[A](path: String)(implicit handler: PrefHandler[A], pref: Preferences): Pref[Option[A]] = {
new Pref(path)(new AccessHandler.Optional, pref)
}
def apply[A](path: String, default: A)(implicit handler: PrefHandler[A], pref: Preferences): Pref[A] = {
require(default != null, "Default values of null are not permitted")
new Pref(path)(new AccessHandler.Defaulted(default), pref)
}
sealed trait AccessHandler[A] extends Any {
protected[this] def handler: PrefHandler[_]
def read(path: String)(implicit prefs: Preferences): A
def write(path: String, a: A)(implicit prefs: Preferences): Unit
def reset(path: String)(implicit prefs: Preferences): Unit = handler.remove(path)
}
object AccessHandler {
final class Optional[A](protected[this] implicit val handler: PrefHandler[A])
extends AccessHandler[Option[A]] {
def read(path: String)(implicit prefs: Preferences): Option[A] = {
handler.read(path)
}
def write(path: String, a: Option[A])(implicit prefs: Preferences): Unit = a match {
case Some(value) => handler.write(path, value)
case None => handler.remove(path)
}
}
final class Defaulted[A](default: A)(protected[this] implicit val handler: PrefHandler[A])
extends AccessHandler[A] {
def read(path: String)(implicit prefs: Preferences): A = {
handler.read(path).getOrElse(default)
}
def write(path: String, a: A)(implicit prefs: Preferences): Unit = {
handler.write(path, a)
}
}
}
}