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