src/Pure/General/properties.scala
changeset 65857 5d29d93766ef
parent 65624 32fa61f694ef
child 65932 db5e701b691a
equal deleted inserted replaced
65856:69f4dacd31cf 65857:5d29d93766ef
    28         case (p @ (x1, _)) :: rest =>
    28         case (p @ (x1, _)) :: rest =>
    29           if (x1 == x) (x1, y) :: rest else p :: update(rest)
    29           if (x1 == x) (x1, y) :: rest else p :: update(rest)
    30         case Nil => Nil
    30         case Nil => Nil
    31       }
    31       }
    32     if (defined(props, x)) update(props) else entry :: props
    32     if (defined(props, x)) update(props) else entry :: props
       
    33   }
       
    34 
       
    35 
       
    36   /* external storage */
       
    37 
       
    38   def encode(ps: T): Bytes = Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
       
    39 
       
    40   def decode(bs: Bytes, xml_cache: Option[XML.Cache] = None): T =
       
    41   {
       
    42     val ps = XML.Decode.properties(YXML.parse_body(bs.text))
       
    43     xml_cache match {
       
    44       case None => ps
       
    45       case Some(cache) => cache.props(ps)
       
    46     }
       
    47   }
       
    48 
       
    49   def compress(ps: List[T], options: XZ.Options = XZ.options()): Bytes =
       
    50   {
       
    51     if (ps.isEmpty) Bytes.empty
       
    52     else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
       
    53   }
       
    54 
       
    55   def uncompress(bs: Bytes, xml_cache: Option[XML.Cache] = None): List[T] =
       
    56   {
       
    57     if (bs.isEmpty) Nil
       
    58     else {
       
    59       val ps = XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text))
       
    60       xml_cache match {
       
    61         case None => ps
       
    62         case Some(cache) => ps.map(cache.props(_))
       
    63       }
       
    64     }
    33   }
    65   }
    34 
    66 
    35 
    67 
    36   /* multi-line entries */
    68   /* multi-line entries */
    37 
    69 
    93       props.find(_._1 == name) match {
   125       props.find(_._1 == name) match {
    94         case None => None
   126         case None => None
    95         case Some((_, value)) => Value.Double.unapply(value)
   127         case Some((_, value)) => Value.Double.unapply(value)
    96       }
   128       }
    97   }
   129   }
    98 
       
    99 
       
   100   /* cached store */
       
   101 
       
   102   trait Store
       
   103   {
       
   104     val xml_cache: XML.Cache = new XML.Cache()
       
   105 
       
   106     def encode_properties(ps: T): Bytes =
       
   107       Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
       
   108 
       
   109     def decode_properties(bs: Bytes): T =
       
   110       xml_cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
       
   111 
       
   112     def compress_properties(ps: List[T], options: XZ.Options = XZ.options()): Bytes =
       
   113     {
       
   114       if (ps.isEmpty) Bytes.empty
       
   115       else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
       
   116     }
       
   117 
       
   118     def uncompress_properties(bs: Bytes): List[T] =
       
   119     {
       
   120       if (bs.isEmpty) Nil
       
   121       else
       
   122         XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text)).
       
   123           map(xml_cache.props(_))
       
   124     }
       
   125   }
       
   126 }
   130 }