using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Services.Optimization.ControllerService.Model; namespace HeuristicLab.Services.Optimization.ControllerService.Parameters { public interface IParameterHandler { Type HandledType { get; } Parameter Map(I item); }; public class ParameterMappingException : Exception { public ParameterMappingException(string message) : base(message) { } } public class ParameterMapper { Dictionary> handlers = new Dictionary>(); public ParameterMapper() { } public void Register(IParameterHandler handler) { handlers[handler.HandledType] = handler; } public bool IsHandlerAvailable(I element) { return handlers.ContainsKey(element.GetType()); } public Parameter Map(string name, I element) { if (element == null) throw new ArgumentNullException("Element parameter may not be null"); IParameterHandler handler; if (handlers.TryGetValue(element.GetType(), out handler)) { var param = handler.Map(element); if (param == null) throw new ParameterMappingException("Handler '" + handler.GetType().FullName + "' returned null parameter for type " + element.GetType().FullName); param.Value.Name = name; return param; } throw new ParameterMappingException("No handler for type " + element.GetType().FullName); } } }