1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Persistence.Core {
|
---|
6 |
|
---|
7 | public class Configuration {
|
---|
8 |
|
---|
9 | [Storable]
|
---|
10 | private readonly Dictionary<Type, IFormatter> formatters;
|
---|
11 | [Storable]
|
---|
12 | private readonly List<IDecomposer> decomposers;
|
---|
13 | private readonly Dictionary<Type, IDecomposer> decomposerCache;
|
---|
14 | [Storable]
|
---|
15 | public readonly IFormat Format;
|
---|
16 |
|
---|
17 | private Configuration() {
|
---|
18 | decomposerCache = new Dictionary<Type, IDecomposer>();
|
---|
19 | }
|
---|
20 |
|
---|
21 | public Configuration(Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) {
|
---|
22 | this.formatters = new Dictionary<Type, IFormatter>();
|
---|
23 | foreach ( var pair in formatters ) {
|
---|
24 | if (Format == null) {
|
---|
25 | Format = pair.Value.Format;
|
---|
26 | } else if (pair.Value.Format != Format ) {
|
---|
27 | throw new ArgumentException("All formatters must have the same IFormat.");
|
---|
28 | }
|
---|
29 | this.formatters.Add(pair.Key, pair.Value);
|
---|
30 | }
|
---|
31 | this.decomposers = new List<IDecomposer>(decomposers);
|
---|
32 | decomposerCache = new Dictionary<Type, IDecomposer>();
|
---|
33 | }
|
---|
34 |
|
---|
35 | public IEnumerable<IFormatter> Formatters {
|
---|
36 | get { return formatters.Values; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | public IEnumerable<IDecomposer> Decomposers {
|
---|
40 | get { return decomposers; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public IFormatter GetFormatter(Type type) {
|
---|
44 | IFormatter formatter;
|
---|
45 | formatters.TryGetValue(type, out formatter);
|
---|
46 | return formatter;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public IDecomposer GetDecomposer(Type type) {
|
---|
50 | if (decomposerCache.ContainsKey(type))
|
---|
51 | return decomposerCache[type];
|
---|
52 | foreach (IDecomposer d in decomposers) {
|
---|
53 | if (d.CanDecompose(type)) {
|
---|
54 | decomposerCache.Add(type, d);
|
---|
55 | return d;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | decomposerCache.Add(type, null);
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | } |
---|