Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs @ 3937

Last change on this file since 3937 was 3937, checked in by epitzer, 14 years ago

Estimate or calculate StringBuffer sizes in advance, use iterator over string splits instead of arrays. (#1138)

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections;
23using System.Text;
24using HeuristicLab.Persistence.Interfaces;
25using System;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Persistence.Auxiliary;
29
30namespace HeuristicLab.Persistence.Default.Xml.Compact {
31
32  [StorableClass]
33  internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
34
35    protected virtual char Separator { get { return ';'; } }
36    protected abstract void Add(IEnumerable enumeration, object o);
37    protected abstract IEnumerable Instantiate();
38    protected abstract string FormatValue(object o);
39    protected abstract object ParseValue(string o);
40
41    public override XmlString Format(T t) {
42      StringBuilder sb = new StringBuilder();
43      foreach (var value in (IEnumerable)t) {
44        sb.Append(FormatValue(value));
45        sb.Append(Separator);
46      }
47      return new XmlString(sb.ToString());
48    }
49
50    public override T Parse(XmlString x) {
51      try {
52        IEnumerable enumeration = Instantiate();
53        foreach (var value in x.Data.EnumerateSplit(Separator)) {
54          Add(enumeration, ParseValue(value));
55        }
56        return (T)enumeration;
57      } catch (InvalidCastException e) {
58        throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
59      } catch (OverflowException e) {
60        throw new PersistenceException("Overflow during element parsing while trying to reconstruct number enumerable.", e);
61      }
62    }
63  }
64
65}
Note: See TracBrowser for help on using the repository browser.