Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs @ 16724

Last change on this file since 16724 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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