Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Persistence/3.3/Core/Configuration.cs @ 3962

Last change on this file since 3962 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Persistence.Interfaces;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Persistence.Core {
7
8  public class Configuration {
9
10    [Storable]
11    private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
12
13    [Storable]
14    private readonly List<ICompositeSerializer> compositeSerializers;
15    private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
16
17    [Storable]
18    public IFormat Format { get; private set; }
19
20    private Configuration() {
21      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
22    }
23
24    public Configuration(IFormat format,
25        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
26        IEnumerable<ICompositeSerializer> compositeSerializers) {
27      this.Format = format;
28      this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
29      foreach (IPrimitiveSerializer primitiveSerializer in primitiveSerializers) {
30        if (primitiveSerializer.SerialDataType != format.SerialDataType) {
31          throw new ArgumentException("All primitive serializers must have the same IFormat.");
32        }
33        this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
34      }
35      this.compositeSerializers = new List<ICompositeSerializer>(compositeSerializers);
36      compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
37    }
38
39    public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
40      get { return primitiveSerializers.Values; }
41    }
42
43    public IEnumerable<ICompositeSerializer> CompositeSerializers {
44      get { return compositeSerializers; }
45    }
46
47    public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
48      IPrimitiveSerializer primitiveSerializer;
49      primitiveSerializers.TryGetValue(type, out primitiveSerializer);
50      return primitiveSerializer;
51    }
52
53    public ICompositeSerializer GetCompositeSerializer(Type type) {
54      if (compositeSerializerCache.ContainsKey(type))
55        return compositeSerializerCache[type];
56      foreach (ICompositeSerializer d in compositeSerializers) {
57        if (d.CanSerialize(type)) {
58          compositeSerializerCache.Add(type, d);
59          return d;
60        }
61      }
62      compositeSerializerCache.Add(type, null);
63      return null;
64    }
65  }
66
67}
Note: See TracBrowser for help on using the repository browser.