Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Range.cs @ 4830

Last change on this file since 4830 was 4830, checked in by cneumuel, 13 years ago

#1215 worked on metaoptimization

File size: 4.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Common;
9
10namespace HeuristicLab.Problems.MetaOptimization {
11  [StorableClass]
12  public class Range<T> : Item, IRange<T> where T : class, IItem, IStringConvertibleValue, IDeepCloneable {
13    [Storable]
14    private T lowerBound;
15    public T LowerBound {
16      get { return lowerBound; }
17      set {
18        if (lowerBound != value) {
19          if (lowerBound != null) {
20            lowerBound.ValueChanged -= new EventHandler(lowerBound_ValueChanged);
21          }
22          lowerBound = value;
23          if (lowerBound != null) {
24            lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged);
25          }
26          OnLowerBoundChanged();
27        }
28      }
29    }
30
31    [Storable]
32    private T upperBound;
33    public T UpperBound {
34      get { return upperBound; }
35      set {
36        if (upperBound != value) {
37          if (upperBound != null) {
38            upperBound.ValueChanged -= new EventHandler(upperBound_ValueChanged);
39          }
40          upperBound = value;
41          if (upperBound != null) {
42            upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged);
43          }
44          OnUpperBoundChanged();
45        }
46      }
47    }
48
49    [Storable]
50    private T stepSize;
51    public T StepSize {
52      get { return stepSize; }
53      set {
54        if (stepSize != value) {
55          if (stepSize != null) {
56            stepSize.ValueChanged -= new EventHandler(upperBound_ValueChanged);
57          }
58          stepSize = value;
59          if (stepSize != null) {
60            stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged);
61          }
62          OnStepSizeChanged();
63        }
64      }
65    }
66
67    public Range() {
68      LowerBound = Activator.CreateInstance<T>();
69      UpperBound = Activator.CreateInstance<T>();
70      StepSize = Activator.CreateInstance<T>(); // todo: stepsize should be != 0
71    }
72
73    [StorableConstructor]
74    protected Range(bool deserializing) : base(deserializing) { }
75    protected Range(Range<T> original, Cloner cloner) : base(original, cloner) {
76      this.LowerBound = cloner.Clone(original.LowerBound);
77      this.UpperBound = cloner.Clone(original.UpperBound);
78      this.StepSize = cloner.Clone(original.StepSize);
79    }
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new Range<T>(this, cloner);
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      if (lowerBound != null) {
87        lowerBound.ValueChanged += new EventHandler(lowerBound_ValueChanged);
88      }
89      if (upperBound != null) {
90        upperBound.ValueChanged += new EventHandler(upperBound_ValueChanged);
91      }
92      if (stepSize != null) {
93        stepSize.ValueChanged += new EventHandler(stepSize_ValueChanged);
94      }
95    }
96
97    void lowerBound_ValueChanged(object sender, EventArgs e) {
98      OnToStringChanged();
99    }
100    void upperBound_ValueChanged(object sender, EventArgs e) {
101      OnToStringChanged();
102    }
103    void stepSize_ValueChanged(object sender, EventArgs e) {
104      OnToStringChanged();
105    }
106
107    public event EventHandler LowerBoundChanged;
108    private void OnLowerBoundChanged() {
109      var handler = LowerBoundChanged;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112
113    public event EventHandler UpperBoundChanged;
114    private void OnUpperBoundChanged() {
115      var handler = UpperBoundChanged;
116      if (handler != null) handler(this, EventArgs.Empty);
117    }
118    public event EventHandler StepSizeChanged;
119    private void OnStepSizeChanged() {
120      var handler = StepSizeChanged;
121      if (handler != null) handler(this, EventArgs.Empty);
122    }
123
124    public override string ToString() {
125      return string.Format("[{0},{1}:{2}]", LowerBound.ToString(), UpperBound.ToString(), StepSize.ToString());
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.