Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/Compact/NumberEnumeration2XmlFormatter.cs @ 1361

Last change on this file since 1361 was 1361, checked in by epitzer, 15 years ago

Split all classes into their own file (#506)

File size: 1.2 KB
Line 
1using System.Collections;
2using System.Text;
3using HeuristicLab.Persistence.Interfaces;
4using System;
5
6namespace HeuristicLab.Persistence.Default.Xml.Compact {
7 
8  public abstract class NumberEnumeration2XmlFormatter : IFormatter {
9
10    public abstract Type Type { get; }
11    public IFormat Format { get { return XmlFormat.Instance; } }
12    protected virtual string Separator { get { return ";"; } }
13    protected abstract void Add(IEnumerable enumeration, object o);
14    protected abstract object Instantiate();
15    protected abstract string formatValue(object o);
16    protected abstract object parseValue(string o);
17
18    public object DoFormat(object o) {
19      StringBuilder sb = new StringBuilder();
20      foreach (var value in (IEnumerable)o) {
21        sb.Append(formatValue(value));
22        sb.Append(Separator);
23      }
24      return sb.ToString();
25    }
26
27    public object Parse(object o) {
28      IEnumerable enumeration = (IEnumerable)Instantiate();
29      string[] values = ((string)o).Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
30      foreach (var value in values) {
31        Add(enumeration, parseValue(value));
32      }
33      return enumeration;
34    }
35  }
36 
37}
Note: See TracBrowser for help on using the repository browser.