Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1564 was 1564, checked in by epitzer, 15 years ago

Stronger typing for formatters with the help of generics. Separate format and serial data type. (#548)

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