using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.MetaOptimization { [StorableClass] public class NumericRange : Item, INumericRange { [Storable] private IntValue lowerBound; public IntValue 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 IntValue upperBound; public IntValue 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 IntValue stepSize; public IntValue 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 NumericRange() { LowerBound = new IntValue(0); UpperBound = new IntValue(0); StepSize = new IntValue(1); } [StorableConstructor] protected NumericRange(bool deserializing) : base(deserializing) { } [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()); } #region Cloning public override Common.IDeepCloneable Clone(Common.Cloner cloner) { NumericRange clone = (NumericRange)base.Clone(cloner); clone.LowerBound = (IntValue)this.LowerBound.Clone(); clone.UpperBound = (IntValue)this.UpperBound.Clone(); clone.StepSize = (IntValue)this.StepSize.Clone(); return base.Clone(cloner); } #endregion } }