Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Persistence/3.3/Core/Configuration.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.6 KB
RevLine 
[3743]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3743]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;
[1454]23using System.Collections.Generic;
[16565]24using HEAL.Attic;
[1454]25using HeuristicLab.Persistence.Interfaces;
26
27namespace HeuristicLab.Persistence.Core {
28
[3004]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
[3016]32  /// <c>ConfigurationService</c>.
[3004]33  /// </summary>
[16571]34  [StorableType("72F8B3EA-0BC3-43A8-9B58-EF798C154CF3")]
[1454]35  public class Configuration {
36
37    [Storable]
[1823]38    private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
[1564]39
[1454]40    [Storable]
[1823]41    private readonly List<ICompositeSerializer> compositeSerializers;
42    private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
[1454]43
[3016]44    /// <summary>
45    /// Gets the format.
46    /// </summary>
47    /// <value>The format.</value>
[1566]48    [Storable]
[1564]49    public IFormat Format { get; private set; }
[3030]50
51    [StorableConstructor]
[16565]52    protected Configuration(StorableConstructorFlag _) {
[1823]53      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
[16565]54    }
55
56    public Configuration() {
57      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
[3030]58      primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
59      compositeSerializers = new List<ICompositeSerializer>();
[1454]60    }
61
[16565]62
[3016]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>
[1823]69    public Configuration(IFormat format,
70        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
[3030]71        IEnumerable<ICompositeSerializer> compositeSerializers)
[16565]72      : this() {
[1564]73      this.Format = format;
[3030]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,
[4068]81            format.Name, format.SerialDataType.FullName));
[1823]82        this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
[4068]83      }
[1454]84    }
85
[3016]86    /// <summary>
87    /// Gets the primitive serializers.
88    /// </summary>
89    /// <value>The primitive serializers.</value>
[1823]90    public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
91      get { return primitiveSerializers.Values; }
[1454]92    }
93
[3016]94    /// <summary>
95    /// Gets the composite serializers.
96    /// </summary>
97    /// <value>An enumerable of composite serializers.</value>
[1823]98    public IEnumerable<ICompositeSerializer> CompositeSerializers {
99      get { return compositeSerializers; }
[1454]100    }
101
[3016]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>
[1823]107    public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
108      IPrimitiveSerializer primitiveSerializer;
109      primitiveSerializers.TryGetValue(type, out primitiveSerializer);
110      return primitiveSerializer;
[1454]111    }
112
[3016]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>
[1823]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);
[1454]124          return d;
125        }
126      }
[1823]127      compositeSerializerCache.Add(type, null);
[1454]128      return null;
[1566]129    }
[3030]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() {
[16565]136      var config = new Configuration();
[3030]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
[1566]147  }
148
[1454]149}
Note: See TracBrowser for help on using the repository browser.