Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs @ 4949

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

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

File size: 5.2 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;
23using System.Collections.Generic;
24using System.Reflection;
25using System.Text;
26using HeuristicLab.Persistence.Auxiliary;
27using HeuristicLab.Persistence.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Persistence.Interfaces;
30
31namespace HeuristicLab.Persistence.Default.CompositeSerializers {
32
33  [StorableClass]
34  internal sealed class NumberEnumerable2StringSerializer : ICompositeSerializer {
35
36    public int Priority {
37      get { return 200; }
38    }
39
40    private static readonly Number2StringSerializer numberConverter =
41      new Number2StringSerializer();
42
43    private static readonly Dictionary<Type, Type> interfaceCache = new Dictionary<Type, Type>();
44
45    public Type GetGenericEnumerableInterface(Type type) {
46      if (interfaceCache.ContainsKey(type))
47        return interfaceCache[type];
48      foreach (Type iface in type.GetInterfaces()) {
49        if (iface.IsGenericType &&
50          iface.GetGenericTypeDefinition() == typeof(IEnumerable<>) &&
51          numberConverter.CanSerialize(iface.GetGenericArguments()[0])) {
52          interfaceCache.Add(type, iface);
53          return iface;
54        }
55      }
56      interfaceCache.Add(type, null);
57      return null;
58    }
59
60    public bool ImplementsGenericEnumerable(Type type) {
61      return GetGenericEnumerableInterface(type) != null;
62    }
63
64    public bool HasAddMethod(Type type) {
65      return
66        type.GetMethod("Add") != null &&
67        type.GetMethod("Add").GetParameters().Length == 1 &&
68        type.GetConstructor(
69          BindingFlags.Public |
70          BindingFlags.NonPublic |
71          BindingFlags.Instance,
72          null, Type.EmptyTypes, null) != null;
73    }
74
75    public bool CanSerialize(Type type) {
76      return
77        ReflectionTools.HasDefaultConstructor(type) &&
78        ImplementsGenericEnumerable(type) &&
79        HasAddMethod(type);
80    }
81
82    public string JustifyRejection(Type type) {
83      if (!ReflectionTools.HasDefaultConstructor(type))
84        return "no default constructor";
85      if (!ImplementsGenericEnumerable(type))
86        return "IEnumerable<> not implemented";
87      return "no Add method with one parameter";
88    }
89
90    public IEnumerable<Tag> CreateMetaInfo(object o) {
91      return new Tag[] { };
92    }
93
94    private static object[] emptyArgs = new object[0];
95    public IEnumerable<Tag> Decompose(object obj) {
96      Type type = obj.GetType();
97      Type enumerable = GetGenericEnumerableInterface(type);
98      InterfaceMapping iMap = obj.GetType().GetInterfaceMap(enumerable);
99      MethodInfo getEnumeratorMethod =
100        iMap.TargetMethods[
101        Array.IndexOf(
102          iMap.InterfaceMethods,
103          enumerable.GetMethod("GetEnumerator"))];
104      object genericEnumerator = getEnumeratorMethod.Invoke(obj, emptyArgs);
105      MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext");
106      PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current");
107      StringBuilder sb = new StringBuilder();
108      while ((bool)moveNextMethod.Invoke(genericEnumerator, emptyArgs))
109        sb.Append(
110          numberConverter.Format(
111            currentProperty.GetValue(genericEnumerator, null))).Append(';');
112      yield return new Tag("compact enumerable", sb.ToString());
113    }
114
115    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
116      return Activator.CreateInstance(type, true);
117    }
118
119    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
120      Type enumerable = GetGenericEnumerableInterface(type);
121      Type elementType = enumerable.GetGenericArguments()[0];
122      MethodInfo addMethod = type.GetMethod("Add");
123      try {
124        var tagEnumerator = tags.GetEnumerator();
125        tagEnumerator.MoveNext();
126        string[] stringValues = ((string)tagEnumerator.Current.Value)
127          .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
128        foreach (var value in stringValues) {
129          addMethod.Invoke(instance, new[] { numberConverter.Parse(value, elementType) });
130        }
131      }
132      catch (InvalidOperationException e) {
133        throw new PersistenceException("Insufficient element data to reconstruct number enumerable", e);
134      }
135      catch (InvalidCastException e) {
136        throw new PersistenceException("Invalid element data during reconstruction of number enumerable", e);
137      }
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.