Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4537 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 2.4 KB
RevLine 
[3742]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
[4068]22using System;
[3742]23using System.Collections;
[1542]24using System.Text;
[4068]25using HeuristicLab.Persistence.Auxiliary;
[1625]26using HeuristicLab.Persistence.Core;
[1853]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1542]28
29namespace HeuristicLab.Persistence.Default.Xml.Compact {
[1566]30
[3017]31  [StorableClass]
[3036]32  internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
[1542]33
[3937]34    protected virtual char Separator { get { return ';'; } }
[1542]35    protected abstract void Add(IEnumerable enumeration, object o);
[1564]36    protected abstract IEnumerable Instantiate();
[1542]37    protected abstract string FormatValue(object o);
38    protected abstract object ParseValue(string o);
39
[1564]40    public override XmlString Format(T t) {
[1542]41      StringBuilder sb = new StringBuilder();
[1564]42      foreach (var value in (IEnumerable)t) {
[1542]43        sb.Append(FormatValue(value));
44        sb.Append(Separator);
45      }
[1564]46      return new XmlString(sb.ToString());
[1542]47    }
48
[1564]49    public override T Parse(XmlString x) {
[1625]50      try {
51        IEnumerable enumeration = Instantiate();
[3937]52        foreach (var value in x.Data.EnumerateSplit(Separator)) {
[1625]53          Add(enumeration, ParseValue(value));
54        }
55        return (T)enumeration;
[4068]56      }
57      catch (InvalidCastException e) {
[1625]58        throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
[4068]59      }
60      catch (OverflowException e) {
[1625]61        throw new PersistenceException("Overflow during element parsing while trying to reconstruct number enumerable.", e);
[1853]62      }
[1542]63    }
64  }
[1566]65
[1542]66}
Note: See TracBrowser for help on using the repository browser.