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 IFormat Format { get; private set; } private Configuration() { decomposerCache = new Dictionary(); } public Configuration(IFormat format, Dictionary formatters, IEnumerable decomposers) { this.Format = format; this.formatters = new Dictionary(); foreach ( var pair in formatters ) { if (pair.Value.SerialDataType != format.SerialDataType ) { 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) { if (decomposerCache.ContainsKey(type)) return decomposerCache[type]; foreach (IDecomposer d in decomposers) { if (d.CanDecompose(type)) { decomposerCache.Add(type, d); return d; } } decomposerCache.Add(type, null); return null; } } }