src/Pure/General/properties.scala
author wenzelm
Wed, 06 Jan 2021 13:47:50 +0100
changeset 73081 120ffea2c244
parent 73031 f93f0597f4fb
child 73712 3eba8d4b624b
permissions -rw-r--r--
prefer OpenJDK from Azul: supports more versions and platforms; support arm64-darwin;
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
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    10
object Properties
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    11
{
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    12
  /* entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    13
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    14
  type Entry = (java.lang.String, java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    15
  type T = List[Entry]
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    16
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    17
  def defined(props: T, name: java.lang.String): java.lang.Boolean =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    18
    props.exists({ case (x, _) => x == name })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    19
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    20
  def get(props: T, name: java.lang.String): Option[java.lang.String] =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    21
    props.collectFirst({ case (x, y) if x == name => y })
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    22
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    23
  def put(props: T, entry: Entry): T =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    24
  {
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    25
    val (x, y) = entry
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    26
    def update(ps: T): T =
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    27
      ps match {
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    28
        case (p @ (x1, _)) :: rest =>
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    29
          if (x1 == x) (x1, y) :: rest else p :: update(rest)
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    30
        case Nil => Nil
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    31
      }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    32
    if (defined(props, x)) update(props) else entry :: props
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    33
  }
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    34
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    35
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    36
  /* external storage */
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    37
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    38
  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
    39
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    40
  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
    41
    cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    42
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    43
  def compress(ps: List[T],
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    44
    options: XZ.Options = XZ.options(),
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 72885
diff changeset
    45
    cache: XZ.Cache = XZ.Cache()): Bytes =
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    46
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    47
    if (ps.isEmpty) Bytes.empty
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    48
    else {
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    49
      Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    50
        compress(options = options, cache = cache)
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    51
    }
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    52
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    53
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    54
  def uncompress(bs: Bytes, cache: XML.Cache = XML.Cache.none): List[T] =
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    55
  {
72885
1b0f81e556a2 accommodate OpenJDK 15;
wenzelm
parents: 72137
diff changeset
    56
    if (bs.is_empty) Nil
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    57
    else {
68018
3747fe57eb67 support for XZ.Cache;
wenzelm
parents: 65932
diff changeset
    58
      val ps =
73031
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    59
        XML.Decode.list(XML.Decode.properties)(
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    60
          YXML.parse_body(bs.uncompress(cache = cache.xz).text))
f93f0597f4fb clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents: 73024
diff changeset
    61
      if (cache.no_cache) ps else ps.map(cache.props)
65857
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    62
    }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    63
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    64
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    65
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    66
  /* multi-line entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    67
65932
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    68
  def encode_lines(props: T): T = props.map({ case (a, b) => (a, Library.encode_lines(b)) })
db5e701b691a clarified modules;
wenzelm
parents: 65857
diff changeset
    69
  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
    70
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    71
  def lines_nonempty(x: java.lang.String, ys: List[java.lang.String]): Properties.T =
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    72
    if (ys.isEmpty) Nil else List((x, cat_lines(ys)))
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    73
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    74
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    75
  /* entry types */
64358
15c90b744481 more operations (see also properties.ML);
wenzelm
parents: 63805
diff changeset
    76
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    77
  class String(val name: java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    78
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    79
    def apply(value: java.lang.String): T = List((name, value))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    80
    def unapply(props: T): Option[java.lang.String] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    81
      props.find(_._1 == name).map(_._2)
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
    82
    def get(props: T): java.lang.String = unapply(props).getOrElse("")
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    83
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    84
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    85
  class Boolean(val name: java.lang.String)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    86
  {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    87
    def apply(value: scala.Boolean): T = List((name, Value.Boolean(value)))
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    88
    def unapply(props: T): Option[scala.Boolean] =
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    89
      props.find(_._1 == name) match {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    90
        case None => None
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    91
        case Some((_, value)) => Value.Boolean.unapply(value)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    92
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
    93
    def get(props: T): scala.Boolean = unapply(props).getOrElse(false)
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    94
  }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    95
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
    96
  class Int(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    97
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    98
    def apply(value: scala.Int): T = List((name, Value.Int(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    99
    def unapply(props: T): Option[scala.Int] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   100
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   101
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   102
        case Some((_, value)) => Value.Int.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   103
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   104
    def get(props: T): scala.Int = unapply(props).getOrElse(0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   105
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   106
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   107
  class Long(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   108
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   109
    def apply(value: scala.Long): T = List((name, Value.Long(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   110
    def unapply(props: T): Option[scala.Long] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   111
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   112
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   113
        case Some((_, value)) => Value.Long.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   114
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   115
    def get(props: T): scala.Long = unapply(props).getOrElse(0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   116
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   117
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   118
  class Double(val name: java.lang.String)
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
    def apply(value: scala.Double): T = List((name, Value.Double(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   121
    def unapply(props: T): Option[scala.Double] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   122
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   123
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   124
        case Some((_, value)) => Value.Double.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   125
      }
72137
ad277a335aa5 clarified signature;
wenzelm
parents: 71601
diff changeset
   126
    def get(props: T): scala.Double = unapply(props).getOrElse(0.0)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   127
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   128
}