src/Pure/General/properties.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 73715 bf51c23f3f99
child 75794 1c3c31319974
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/properties.scala
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     3
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     4
Property lists.
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     5
*/
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     6
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     8
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    10
object Properties {
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    11
  /* entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    12
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    13
  type Entry = (java.lang.String, java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    14
  type T = List[Entry]
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    15
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    16
  object Eq {
73715
bf51c23f3f99 clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents: 73712
diff changeset
    17
    def apply(a: java.lang.String, b: java.lang.String): java.lang.String = a + "=" + b
bf51c23f3f99 clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents: 73712
diff changeset
    18
    def apply(entry: Entry): java.lang.String = apply(entry._1, entry._2)
73712
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    19
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    20
    def unapply(str: java.lang.String): Option[Entry] = {
73712
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    21
      val i = str.indexOf('=')
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    22
      if (i <= 0) None else Some((str.substring(0, i), str.substring(i + 1)))
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    23
    }
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    24
  }
3eba8d4b624b clarified signature;
wenzelm
parents: 73031
diff changeset
    25
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    26
  def defined(props: T, name: java.lang.String): java.lang.Boolean =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    27
    props.exists({ case (x, _) => x == name })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    28
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    29
  def get(props: T, name: java.lang.String): Option[java.lang.String] =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    30
    props.collectFirst({ case (x, y) if x == name => y })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    31
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    32
  def put(props: T, entry: Entry): T = {
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    33
    val (x, y) = entry
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    34
    def update(ps: T): T =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    35
      ps match {
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    36
        case (p @ (x1, _)) :: rest =>
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    37
          if (x1 == x) (x1, y) :: rest else p :: update(rest)
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    38
        case Nil => Nil
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    39
      }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    40
    if (defined(props, x)) update(props) else entry :: props
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    41
  }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    42
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    43
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    44
  /* external storage */
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    45
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    46
  def encode(ps: T): Bytes = Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    47
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    48
  def decode(bs: Bytes, cache: XML.Cache = XML.Cache.none): T =
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    49
    cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    50
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    51
  def compress(ps: List[T],
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    52
    options: XZ.Options = XZ.options(),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    53
    cache: XZ.Cache = XZ.Cache()
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    54
  ): Bytes = {
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    55
    if (ps.isEmpty) Bytes.empty
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    56
    else {
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    57
      Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    58
        compress(options = options, cache = cache)
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    59
    }
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    60
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    62
  def uncompress(bs: Bytes, cache: XML.Cache = XML.Cache.none): List[T] = {
72885
1b0f81e556a2 accommodate OpenJDK 15;
wenzelm
parents: 72137
diff changeset
    63
    if (bs.is_empty) Nil
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    64
    else {
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    65
      val ps =
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    66
        XML.Decode.list(XML.Decode.properties)(
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    67
          YXML.parse_body(bs.uncompress(cache = cache.xz).text))
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    68
      if (cache.no_cache) ps else ps.map(cache.props)
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    69
    }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    70
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    71
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    72
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    73
  /* multi-line entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    74
65932
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    75
  def encode_lines(props: T): T = props.map({ case (a, b) => (a, Library.encode_lines(b)) })
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    76
  def decode_lines(props: T): T = props.map({ case (a, b) => (a, Library.decode_lines(b)) })
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    77
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    78
  def lines_nonempty(x: java.lang.String, ys: List[java.lang.String]): Properties.T =
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    79
    if (ys.isEmpty) Nil else List((x, cat_lines(ys)))
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    80
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    81
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    82
  /* entry types */
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    83
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    84
  class String(val name: java.lang.String) {
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    85
    def apply(value: java.lang.String): T = List((name, value))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    86
    def unapply(props: T): Option[java.lang.String] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    87
      props.find(_._1 == name).map(_._2)
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
    88
    def get(props: T): java.lang.String = unapply(props).getOrElse("")
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    89
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    90
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
    91
  class Boolean(val name: java.lang.String) {
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    92
    def apply(value: scala.Boolean): T = List((name, Value.Boolean(value)))
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    93
    def unapply(props: T): Option[scala.Boolean] =
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    94
      props.find(_._1 == name) match {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    95
        case None => None
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    96
        case Some((_, value)) => Value.Boolean.unapply(value)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    97
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
    98
    def get(props: T): scala.Boolean = unapply(props).getOrElse(false)
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    99
  }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   100
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
   101
  class Int(val name: java.lang.String) {
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   102
    def apply(value: scala.Int): T = List((name, Value.Int(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   103
    def unapply(props: T): Option[scala.Int] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   104
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   105
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   106
        case Some((_, value)) => Value.Int.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   107
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   108
    def get(props: T): scala.Int = unapply(props).getOrElse(0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   109
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   110
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
   111
  class Long(val name: java.lang.String) {
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   112
    def apply(value: scala.Long): T = List((name, Value.Long(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   113
    def unapply(props: T): Option[scala.Long] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   114
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   115
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   116
        case Some((_, value)) => Value.Long.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   117
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   118
    def get(props: T): scala.Long = unapply(props).getOrElse(0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   119
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   120
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73715
diff changeset
   121
  class Double(val name: java.lang.String) {
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   122
    def apply(value: scala.Double): T = List((name, Value.Double(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   123
    def unapply(props: T): Option[scala.Double] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   124
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   125
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   126
        case Some((_, value)) => Value.Double.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   127
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   128
    def get(props: T): scala.Double = unapply(props).getOrElse(0.0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   129
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   130
}