Last change
on this file since 1564 was
1564,
checked in by epitzer, 15 years ago
|
Stronger typing for formatters with the help of generics. Separate format and serial data type. (#548)
|
File size:
1.2 KB
|
Line | |
---|
1 | using System.Collections;
|
---|
2 | using System.Text;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using System;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Persistence.Default.Xml.Compact {
|
---|
7 |
|
---|
8 | public abstract class NumberEnumeration2XmlFormatterBase<T> : FormatterBase<T, XmlString> where T : IEnumerable {
|
---|
9 |
|
---|
10 | protected virtual string Separator { get { return ";"; } }
|
---|
11 | protected abstract void Add(IEnumerable enumeration, object o);
|
---|
12 | protected abstract IEnumerable Instantiate();
|
---|
13 | protected abstract string FormatValue(object o);
|
---|
14 | protected abstract object ParseValue(string o);
|
---|
15 |
|
---|
16 | public override XmlString Format(T t) {
|
---|
17 | StringBuilder sb = new StringBuilder();
|
---|
18 | foreach (var value in (IEnumerable)t) {
|
---|
19 | sb.Append(FormatValue(value));
|
---|
20 | sb.Append(Separator);
|
---|
21 | }
|
---|
22 | return new XmlString(sb.ToString());
|
---|
23 | }
|
---|
24 |
|
---|
25 | public override T Parse(XmlString x) {
|
---|
26 | IEnumerable enumeration = Instantiate();
|
---|
27 | string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
|
---|
28 | foreach (var value in values) {
|
---|
29 | Add(enumeration, ParseValue(value));
|
---|
30 | }
|
---|
31 | return (T)enumeration;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.