1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Persistence.Core {
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Defines the set of primitive and composite serializers that are to be used
|
---|
10 | /// for a certain seraial format. The configuration can be obtained from the
|
---|
11 | /// <c>ConfigurationService</c>.
|
---|
12 | /// </summary>
|
---|
13 | [StorableClass]
|
---|
14 | public class Configuration {
|
---|
15 |
|
---|
16 | [Storable]
|
---|
17 | private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
|
---|
18 |
|
---|
19 | [Storable]
|
---|
20 | private readonly List<ICompositeSerializer> compositeSerializers;
|
---|
21 | private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
|
---|
22 |
|
---|
23 | /// <summary>
|
---|
24 | /// Gets the format.
|
---|
25 | /// </summary>
|
---|
26 | /// <value>The format.</value>
|
---|
27 | [Storable]
|
---|
28 | public IFormat Format { get; private set; }
|
---|
29 |
|
---|
30 | private Configuration() {
|
---|
31 | compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
|
---|
32 | }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Initializes a new instance of the <see cref="Configuration"/> class.
|
---|
36 | /// </summary>
|
---|
37 | /// <param name="format">The format.</param>
|
---|
38 | /// <param name="primitiveSerializers">The primitive serializers.</param>
|
---|
39 | /// <param name="compositeSerializers">The composite serializers.</param>
|
---|
40 | public Configuration(IFormat format,
|
---|
41 | IEnumerable<IPrimitiveSerializer> primitiveSerializers,
|
---|
42 | IEnumerable<ICompositeSerializer> compositeSerializers) {
|
---|
43 | this.Format = format;
|
---|
44 | this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
|
---|
45 | foreach (IPrimitiveSerializer primitiveSerializer in primitiveSerializers) {
|
---|
46 | if (primitiveSerializer.SerialDataType != format.SerialDataType) {
|
---|
47 | throw new ArgumentException("All primitive serializers must have the same IFormat.");
|
---|
48 | }
|
---|
49 | this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
|
---|
50 | }
|
---|
51 | this.compositeSerializers = new List<ICompositeSerializer>(compositeSerializers);
|
---|
52 | compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Gets the primitive serializers.
|
---|
57 | /// </summary>
|
---|
58 | /// <value>The primitive serializers.</value>
|
---|
59 | public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
|
---|
60 | get { return primitiveSerializers.Values; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Gets the composite serializers.
|
---|
65 | /// </summary>
|
---|
66 | /// <value>An enumerable of composite serializers.</value>
|
---|
67 | public IEnumerable<ICompositeSerializer> CompositeSerializers {
|
---|
68 | get { return compositeSerializers; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Gets the primitive serializer.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="type">The type.</param>
|
---|
75 | /// <returns>The appropriate primitive serializer for the type.</returns>
|
---|
76 | public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
|
---|
77 | IPrimitiveSerializer primitiveSerializer;
|
---|
78 | primitiveSerializers.TryGetValue(type, out primitiveSerializer);
|
---|
79 | return primitiveSerializer;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Gets the composite serializer for a given type.
|
---|
84 | /// </summary>
|
---|
85 | /// <param name="type">The type.</param>
|
---|
86 | /// <returns>The first matching composite serializer for the type.</returns>
|
---|
87 | public ICompositeSerializer GetCompositeSerializer(Type type) {
|
---|
88 | if (compositeSerializerCache.ContainsKey(type))
|
---|
89 | return compositeSerializerCache[type];
|
---|
90 | foreach (ICompositeSerializer d in compositeSerializers) {
|
---|
91 | if (d.CanSerialize(type)) {
|
---|
92 | compositeSerializerCache.Add(type, d);
|
---|
93 | return d;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | compositeSerializerCache.Add(type, null);
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | } |
---|