Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Persistence/3.3/Core/Configuration.cs @ 16474

Last change on this file since 16474 was 3743, checked in by gkronber, 14 years ago

Fixed GPL license headers #893

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