Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Persistence/3.3/Core/Configuration.cs @ 18242

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

#2875: Merged r17180 from trunk to stable

File size: 5.6 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.Generic;
24using HEAL.Attic;
25using HeuristicLab.Persistence.Interfaces;
26
27namespace HeuristicLab.Persistence.Core {
28
29  /// <summary>
30  /// Defines the set of primitive and composite serializers that are to be used
31  /// for a certain seraial format. The configuration can be obtained from the
32  /// <c>ConfigurationService</c>.
33  /// </summary>
34  [StorableType("72F8B3EA-0BC3-43A8-9B58-EF798C154CF3")]
35  public class Configuration {
36
37    [Storable]
38    private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
39
40    [Storable]
41    private readonly List<ICompositeSerializer> compositeSerializers;
42    private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
43
44    /// <summary>
45    /// Gets the format.
46    /// </summary>
47    /// <value>The format.</value>
48    [Storable]
49    public IFormat Format { get; private set; }
50
51    [StorableConstructor]
52    protected Configuration(StorableConstructorFlag _) {
53      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
54    }
55
56    public Configuration() {
57      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
58      primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
59      compositeSerializers = new List<ICompositeSerializer>();
60    }
61
62
63    /// <summary>
64    /// Initializes a new instance of the <see cref="Configuration"/> class.
65    /// </summary>
66    /// <param name="format">The format.</param>
67    /// <param name="primitiveSerializers">The primitive serializers.</param>
68    /// <param name="compositeSerializers">The composite serializers.</param>
69    public Configuration(IFormat format,
70        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
71        IEnumerable<ICompositeSerializer> compositeSerializers)
72      : this() {
73      this.Format = format;
74      this.compositeSerializers.AddRange(compositeSerializers);
75      foreach (var primitiveSerializer in primitiveSerializers) {
76        if (primitiveSerializer.SerialDataType != format.SerialDataType)
77          throw new ArgumentException(string.Format(
78            "primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine +
79            "is not compatible with selected format's ({2}) seriali data type ({3})",
80            primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName,
81            format.Name, format.SerialDataType.FullName));
82        this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
83      }
84    }
85
86    /// <summary>
87    /// Gets the primitive serializers.
88    /// </summary>
89    /// <value>The primitive serializers.</value>
90    public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
91      get { return primitiveSerializers.Values; }
92    }
93
94    /// <summary>
95    /// Gets the composite serializers.
96    /// </summary>
97    /// <value>An enumerable of composite serializers.</value>
98    public IEnumerable<ICompositeSerializer> CompositeSerializers {
99      get { return compositeSerializers; }
100    }
101
102    /// <summary>
103    /// Gets the primitive serializer.
104    /// </summary>
105    /// <param name="type">The type.</param>
106    /// <returns>The appropriate primitive serializer for the type.</returns>
107    public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
108      IPrimitiveSerializer primitiveSerializer;
109      primitiveSerializers.TryGetValue(type, out primitiveSerializer);
110      return primitiveSerializer;
111    }
112
113    /// <summary>
114    /// Gets the composite serializer for a given type.
115    /// </summary>
116    /// <param name="type">The type.</param>
117    /// <returns>The first matching composite serializer for the type.</returns>
118    public ICompositeSerializer GetCompositeSerializer(Type type) {
119      if (compositeSerializerCache.ContainsKey(type))
120        return compositeSerializerCache[type];
121      foreach (ICompositeSerializer d in compositeSerializers) {
122        if (d.CanSerialize(type)) {
123          compositeSerializerCache.Add(type, d);
124          return d;
125        }
126      }
127      compositeSerializerCache.Add(type, null);
128      return null;
129    }
130
131    /// <summary>
132    /// Copies this configuration and re-instantiates all serializers.
133    /// </summary>
134    /// <returns>A new <see cref="Configuration"/></returns>
135    public Configuration Copy() {
136      var config = new Configuration();
137      config.Format = Format;
138      foreach (var ps in primitiveSerializers)
139        config.primitiveSerializers.Add(
140          ps.Key,
141          (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
142      foreach (var cs in compositeSerializers)
143        config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
144      return config;
145    }
146
147  }
148
149}
Note: See TracBrowser for help on using the repository browser.