Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Core/Configuration.cs @ 1406

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

Incorporate settings infrastructure. (#506)

File size: 1.8 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
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
33    public IEnumerable<IFormatter> Formatters {
34      get { return formatters.Values; }
35    }
36
37    public IEnumerable<IDecomposer> Decomposers {
38      get { return decomposers; }
39    }
40
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}
Note: See TracBrowser for help on using the repository browser.