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