Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/Core/Configuration.cs @ 1504

Last change on this file since 1504 was 1454, checked in by epitzer, 16 years ago

merge persistence exploration branch into trunk as HL plugin. (#506)

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Persistence.Interfaces;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.