src/Pure/General/properties.scala
author wenzelm
Sun, 21 May 2017 23:41:46 +0200
changeset 65895 744878d72021
parent 65857 5d29d93766ef
child 65932 db5e701b691a
permissions -rw-r--r--
more general workaround for failed sessions (again, see also 2edb89630a80, ed7b5cd3a7f2);
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
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    40
  def decode(bs: Bytes, xml_cache: Option[XML.Cache] = None): T =
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    41
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    42
    val ps = XML.Decode.properties(YXML.parse_body(bs.text))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    43
    xml_cache match {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    44
      case None => ps
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    45
      case Some(cache) => cache.props(ps)
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    46
    }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    47
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    48
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    49
  def compress(ps: List[T], options: XZ.Options = XZ.options()): Bytes =
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    50
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    51
    if (ps.isEmpty) Bytes.empty
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    52
    else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    53
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    54
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    55
  def uncompress(bs: Bytes, xml_cache: Option[XML.Cache] = None): List[T] =
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    56
  {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    57
    if (bs.isEmpty) Nil
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    58
    else {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    59
      val ps = XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text))
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    60
      xml_cache match {
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    61
        case None => ps
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    62
        case Some(cache) => ps.map(cache.props(_))
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
  }
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    66
5d29d93766ef clarified use of XML.Cache;
wenzelm
parents: 65624
diff changeset
    67
65624
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    68
  /* multi-line entries */
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    69
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    70
  val separator = '\u000b'
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    71
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    72
  def encode_lines(s: java.lang.String): java.lang.String = s.replace('\n', separator)
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    73
  def decode_lines(s: java.lang.String): java.lang.String = s.replace(separator, '\n')
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    74
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    75
  def encode_lines(props: T): T = props.map({ case (a, b) => (a, encode_lines(b)) })
32fa61f694ef clarified multi-line properties;
wenzelm
parents: 65592
diff changeset
    76
  def decode_lines(props: T): T = props.map({ case (a, b) => (a, decode_lines(b)) })
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
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    84
  class String(val name: java.lang.String)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    85
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    86
    def apply(value: java.lang.String): T = List((name, value))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    87
    def unapply(props: T): Option[java.lang.String] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
    88
      props.find(_._1 == name).map(_._2)
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
48344
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    91
  class Boolean(val name: java.lang.String)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    92
  {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    93
    def apply(value: scala.Boolean): T = List((name, Value.Boolean(value)))
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    94
    def unapply(props: T): Option[scala.Boolean] =
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    95
      props.find(_._1 == name) match {
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    96
        case None => None
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    97
        case Some((_, value)) => Value.Boolean.unapply(value)
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    98
      }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
    99
  }
8dc904c45945 prefer general Properties.Value.Boolean;
wenzelm
parents: 48015
diff changeset
   100
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   101
  class Int(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   102
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   103
    def apply(value: scala.Int): T = List((name, Value.Int(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   104
    def unapply(props: T): Option[scala.Int] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   105
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   106
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   107
        case Some((_, value)) => Value.Int.unapply(value)
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
  }
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   110
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   111
  class Long(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   112
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   113
    def apply(value: scala.Long): T = List((name, Value.Long(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   114
    def unapply(props: T): Option[scala.Long] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   115
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   116
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   117
        case Some((_, value)) => Value.Long.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   118
      }
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
48015
878de88db080 tuned signature;
wenzelm
parents: 45673
diff changeset
   121
  class Double(val name: java.lang.String)
43780
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   122
  {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   123
    def apply(value: scala.Double): T = List((name, Value.Double(value)))
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   124
    def unapply(props: T): Option[scala.Double] =
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   125
      props.find(_._1 == name) match {
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   126
        case None => None
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   127
        case Some((_, value)) => Value.Double.unapply(value)
2cb2310d68b6 more uniform Properties in ML and Scala;
wenzelm
parents:
diff changeset
   128
      }
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
}