Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs @ 17334

Last change on this file since 17334 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 2.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
23using System.Collections;
24using System.Text;
25using HEAL.Attic;
26using HeuristicLab.Persistence.Auxiliary;
27
28namespace HeuristicLab.Persistence.Default.Xml.Compact {
29
30  [StorableType("B4D23353-9322-4005-BAF2-AB3034DEE34A")]
31  internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
32
33    protected virtual char Separator { get { return ';'; } }
34    protected abstract void Add(IEnumerable enumeration, object o);
35    protected abstract IEnumerable Instantiate();
36    protected abstract string FormatValue(object o);
37    protected abstract object ParseValue(string o);
38
39    public override XmlString Format(T t) {
40      StringBuilder sb = new StringBuilder();
41      foreach (var value in (IEnumerable)t) {
42        sb.Append(FormatValue(value));
43        sb.Append(Separator);
44      }
45      return new XmlString(sb.ToString());
46    }
47
48    public override T Parse(XmlString x) {
49      try {
50        IEnumerable enumeration = Instantiate();
51        foreach (var value in x.Data.EnumerateSplit(Separator)) {
52          Add(enumeration, ParseValue(value));
53        }
54        return (T)enumeration;
55      }
56      catch (InvalidCastException e) {
57        throw new PersistenceException("Invalid element data during reconstruction of number enumerable.", e);
58      }
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.