Free cookie consent management tool by TermsFeed Policy Generator

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

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

Initialize GUI with current configuration form ConfigurationService. (#506)

File size: 1.8 KB
RevLine 
[1400]1using System;
2using System.Collections;
3using System.Collections.Generic;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace HeuristicLab.Persistence.Core {
7
8  public class Configuration {
9
10    private readonly Dictionary<Type, IFormatter> formatters;
11    private readonly List<IDecomposer> decomposers;
12    private readonly Dictionary<Type, IDecomposer> decomposerCache;
13    public readonly IFormat Format;
14
15    public Configuration(Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) {     
16      this.formatters = new Dictionary<Type, IFormatter>();
17      foreach ( var pair in formatters ) {
18        if (Format == null) {
19          Format = pair.Value.Format;
20        } else if (pair.Value.Format != Format ) {
21          throw new ArgumentException("All formatters must have the same IFormat.");
22        }
23        this.formatters.Add(pair.Key, pair.Value);
24      }
25      this.decomposers = new List<IDecomposer>(decomposers);
26      decomposerCache = new Dictionary<Type, IDecomposer>();     
27    }
28
29    public IEnumerable Formatters {
30      get { return formatters.Values; }
31    }
32
[1401]33    public IEnumerable<IDecomposer> Decomposers {
34      get { return decomposers; }
35    }
36
[1400]37    public IFormatter GetFormatter(Type type) {     
38      IFormatter formatter;
39      formatters.TryGetValue(type, out formatter);
40      return formatter;
41    }
42
43    public IDecomposer GetDecomposer(Type type) {
44      IDecomposer decomposer;
45      decomposerCache.TryGetValue(type, out decomposer);
46      if (decomposer != null)
47        return decomposer;
48      foreach (IDecomposer d in decomposers) {
49        if (d.CanDecompose(type)) {
50          decomposerCache.Add(type, d);
51          return d;
52        }
53      }
54      return null;
55    }   
56  } 
57 
58}
Note: See TracBrowser for help on using the repository browser.