using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class Range : Item, IRange where T : class, IItem, IStringConvertibleValue, IDeepCloneable { [Storable] private T lowerBound; public T LowerBound { get { return lowerBound; } set { if (lowerBound != value) { if (lowerBound != null) { lowerBound.ValueChanged -= new EventHandler(lowerBound_ValueChanged); } lowerBound = value; if (lowerBound != null) { lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged); } OnLowerBoundChanged(); } } } [Storable] private T upperBound; public T UpperBound { get { return upperBound; } set { if (upperBound != value) { if (upperBound != null) { upperBound.ValueChanged -= new EventHandler(upperBound_ValueChanged); } upperBound = value; if (upperBound != null) { upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged); } OnUpperBoundChanged(); } } } [Storable] private T stepSize; public T StepSize { get { return stepSize; } set { if (stepSize != value) { if (stepSize != null) { stepSize.ValueChanged -= new EventHandler(upperBound_ValueChanged); } stepSize = value; if (stepSize != null) { stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged); } OnStepSizeChanged(); } } } public Range() { LowerBound = Activator.CreateInstance(); UpperBound = Activator.CreateInstance(); StepSize = Activator.CreateInstance(); // todo: stepsize should be != 0 } [StorableConstructor] protected Range(bool deserializing) : base(deserializing) { } protected Range(Range original, Cloner cloner) : base(original, cloner) { this.LowerBound = cloner.Clone(original.LowerBound); this.UpperBound = cloner.Clone(original.UpperBound); this.StepSize = cloner.Clone(original.StepSize); } public override IDeepCloneable Clone(Cloner cloner) { return new Range(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (lowerBound != null) { lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged); } if (upperBound != null) { upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged); } if (stepSize != null) { stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged); } } void lowerBound_ValueChanged(object sender, EventArgs e) { OnToStringChanged(); } void upperBound_ValueChanged(object sender, EventArgs e) { OnToStringChanged(); } void stepSize_ValueChanged(object sender, EventArgs e) { OnToStringChanged(); } public event EventHandler LowerBoundChanged; private void OnLowerBoundChanged() { var handler = LowerBoundChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler UpperBoundChanged; private void OnUpperBoundChanged() { var handler = UpperBoundChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler StepSizeChanged; private void OnStepSizeChanged() { var handler = StepSizeChanged; if (handler != null) handler(this, EventArgs.Empty); } public override string ToString() { return string.Format("[{0},{1}:{2}]", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString()); } } }