Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/09 12:40:41 (16 years ago)
Author:
epitzer
Message:

Stronger typing for formatters with the help of generics. Separate format and serial data type. (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DateTime2XmlFormatter.cs

    r1454 r1564  
    66
    77  [EmptyStorableClass]
    8   public class DateTime2XmlFormatter : IFormatter {   
    9 
    10     public Type Type { get { return typeof(DateTime); } }
    11     public IFormat Format { get { return XmlFormat.Instance; } } 
    12 
    13     public object DoFormat(object o) {
    14       return ((DateTime)o).Ticks.ToString();     
     8  public class DateTime2XmlFormatter : FormatterBase<DateTime, XmlString> {   
     9   
     10    public override XmlString Format(DateTime dt) {
     11      return new XmlString(dt.Ticks.ToString());     
    1512    }
    1613
    17     public object Parse(object o) {
    18       return new DateTime(long.Parse((string)o));
     14    public override DateTime Parse(XmlString x) {
     15      return new DateTime(long.Parse(x.Data));
    1916    }
    2017
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/DecimalNumber2XmlFormatterBase.cs

    r1554 r1564  
    77namespace HeuristicLab.Persistence.Default.Xml.Primitive { 
    88 
    9   public abstract class DecimalNumber2XmlFormatterBase<T> : IFormatter {
    10 
    11     public Type Type { get { return typeof(T); } }
    12 
    13     public IFormat Format { get { return XmlFormat.Instance; } }
    14 
     9  public abstract class DecimalNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> {
     10   
    1511    private static MethodInfo ToStringMethod = typeof(T)
    1612      .GetMethod(
     
    3127        null);
    3228
    33     public object DoFormat(object o) {
    34       return ToStringMethod.Invoke(o, new object[] { "r", CultureInfo.InvariantCulture });
     29    public override XmlString Format(T t) {
     30      return new XmlString((string)ToStringMethod.Invoke(t, new object[] { "r", CultureInfo.InvariantCulture }));
    3531    }
    36     public object Parse(object o) {     
    37       return ParseMethod.Invoke(null, new[] { o, CultureInfo.InvariantCulture });
     32    public override T Parse(XmlString x) {     
     33      return (T)ParseMethod.Invoke(null, new object[] { x.Data, CultureInfo.InvariantCulture });
    3834    }
    3935  } 
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/SimpleNumber2XmlFormatterBase.cs

    r1554 r1564  
    77namespace HeuristicLab.Persistence.Default.Xml.Primitive { 
    88
    9   public abstract class SimpleNumber2XmlFormatterBase<T> : IFormatter {
    10 
    11     public Type Type { get { return typeof(T); } }
    12 
    13     public IFormat Format { get { return XmlFormat.Instance; } }
     9  public abstract class SimpleNumber2XmlFormatterBase<T> : FormatterBase<T, XmlString> {
    1410
    1511    private static MethodInfo ParseMethod = typeof(T)
     
    2218        null);
    2319
    24     public object DoFormat(object o) {
    25       return ((T)o).ToString();
     20    public override XmlString Format(T t) {
     21      return new XmlString(t.ToString());
    2622    }
    27     public object Parse(object o) {
    28       return ParseMethod.Invoke(null, new[] { o });     
     23    public override T Parse(XmlString x) {
     24      return (T)ParseMethod.Invoke(null, new[] { x.Data });     
    2925    }
    3026  } 
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlFormatter.cs

    r1542 r1564  
    88
    99  [EmptyStorableClass]
    10   public class String2XmlFormatter : IFormatter {
    11 
    12     public Type Type { get { return typeof(string); } }
    13     public IFormat Format { get { return XmlFormat.Instance;  } }
    14 
    15     public object DoFormat(object o) {     
    16       return "<![CDATA[" +
    17         ((string)o).Replace("]]>", "]]]]><![CDATA[>") +
    18         "]]>";
     10  public class String2XmlFormatter : FormatterBase<string, XmlString> {
     11   
     12    public override XmlString Format(string s) {
     13      StringBuilder sb = new StringBuilder();
     14      sb.Append("<![CDATA[");
     15      sb.Append(s.Replace("]]>", "]]]]><![CDATA[>"));
     16      sb.Append("]]>");
     17      return new XmlString(sb.ToString()); 
    1918    }
    2019
    21     public object Parse(object o) {
     20    private static readonly string[] separators = new string[] { "<![CDATA[", "]]>" };
     21
     22    public override string Parse(XmlString x) {
    2223      StringBuilder sb = new StringBuilder();
    23       foreach (string s in ((string)o).Split(
    24         new[] { "<![CDATA[", "]]>" },
     24      foreach (string s in x.Data.Split(separators,       
    2525        StringSplitOptions.RemoveEmptyEntries)) {
    2626        sb.Append(s);
Note: See TracChangeset for help on using the changeset viewer.