using System; using System.Collections.Generic; using HeuristicLab.Persistence.Interfaces; namespace HeuristicLab.Persistence.Core { public class Configuration { [Storable] private readonly Dictionary formatters; [Storable] private readonly List decomposers; private readonly Dictionary decomposerCache; [Storable] public readonly IFormat Format; private Configuration() {} public Configuration(Dictionary formatters, IEnumerable decomposers) { this.formatters = new Dictionary(); foreach ( var pair in formatters ) { if (Format == null) { Format = pair.Value.Format; } else if (pair.Value.Format != Format ) { throw new ArgumentException("All formatters must have the same IFormat."); } this.formatters.Add(pair.Key, pair.Value); } this.decomposers = new List(decomposers); decomposerCache = new Dictionary(); } public IEnumerable Formatters { get { return formatters.Values; } } public IEnumerable Decomposers { get { return decomposers; } } public IFormatter GetFormatter(Type type) { IFormatter formatter; formatters.TryGetValue(type, out formatter); return formatter; } public IDecomposer GetDecomposer(Type type) { IDecomposer decomposer; decomposerCache.TryGetValue(type, out decomposer); if (decomposer != null) return decomposer; foreach (IDecomposer d in decomposers) { if (d.CanDecompose(type)) { decomposerCache.Add(type, d); return d; } } return null; } } }