#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using HEAL.Attic;
using HeuristicLab.Persistence.Interfaces;
namespace HeuristicLab.Persistence.Core {
///
/// Defines the set of primitive and composite serializers that are to be used
/// for a certain seraial format. The configuration can be obtained from the
/// ConfigurationService.
///
[StorableType("72F8B3EA-0BC3-43A8-9B58-EF798C154CF3")]
public class Configuration {
[Storable]
private readonly Dictionary primitiveSerializers;
[Storable]
private readonly List compositeSerializers;
private readonly Dictionary compositeSerializerCache;
///
/// Gets the format.
///
/// The format.
[Storable]
public IFormat Format { get; private set; }
[StorableConstructor]
protected Configuration(StorableConstructorFlag _) {
compositeSerializerCache = new Dictionary();
}
public Configuration() {
compositeSerializerCache = new Dictionary();
primitiveSerializers = new Dictionary();
compositeSerializers = new List();
}
///
/// Initializes a new instance of the class.
///
/// The format.
/// The primitive serializers.
/// The composite serializers.
public Configuration(IFormat format,
IEnumerable primitiveSerializers,
IEnumerable compositeSerializers)
: this() {
this.Format = format;
this.compositeSerializers.AddRange(compositeSerializers);
foreach (var primitiveSerializer in primitiveSerializers) {
if (primitiveSerializer.SerialDataType != format.SerialDataType)
throw new ArgumentException(string.Format(
"primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine +
"is not compatible with selected format's ({2}) seriali data type ({3})",
primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName,
format.Name, format.SerialDataType.FullName));
this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
}
}
///
/// Gets the primitive serializers.
///
/// The primitive serializers.
public IEnumerable PrimitiveSerializers {
get { return primitiveSerializers.Values; }
}
///
/// Gets the composite serializers.
///
/// An enumerable of composite serializers.
public IEnumerable CompositeSerializers {
get { return compositeSerializers; }
}
///
/// Gets the primitive serializer.
///
/// The type.
/// The appropriate primitive serializer for the type.
public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
IPrimitiveSerializer primitiveSerializer;
primitiveSerializers.TryGetValue(type, out primitiveSerializer);
return primitiveSerializer;
}
///
/// Gets the composite serializer for a given type.
///
/// The type.
/// The first matching composite serializer for the type.
public ICompositeSerializer GetCompositeSerializer(Type type) {
if (compositeSerializerCache.ContainsKey(type))
return compositeSerializerCache[type];
foreach (ICompositeSerializer d in compositeSerializers) {
if (d.CanSerialize(type)) {
compositeSerializerCache.Add(type, d);
return d;
}
}
compositeSerializerCache.Add(type, null);
return null;
}
///
/// Copies this configuration and re-instantiates all serializers.
///
/// A new
public Configuration Copy() {
var config = new Configuration();
config.Format = Format;
foreach (var ps in primitiveSerializers)
config.primitiveSerializers.Add(
ps.Key,
(IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
foreach (var cs in compositeSerializers)
config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
return config;
}
}
}