Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Result.cs @ 3332

Last change on this file since 3332 was 3317, checked in by swagner, 15 years ago

Implemented ReadOnlyView property for items (#969).

File size: 4.1 KB
RevLine 
[3226]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Optimization {
28  /// <summary>
29  /// Represents a result which has a name and a data type and holds an IItem.
30  /// </summary>
31  [Item("Result", "A result which has a name and a data type and holds an IItem.")]
32  [StorableClass]
33  public sealed class Result : NamedItem, IResult {
34    public override bool CanChangeName {
35      get { return false; }
36    }
37    public override bool CanChangeDescription {
38      get { return false; }
39    }
40
41    [Storable]
42    private Type dataType;
43    public Type DataType {
44      get { return dataType; }
45    }
46
[3317]47    [Storable]
[3226]48    private IItem value;
49    public IItem Value {
50      get { return value; }
51      set {
52        if (this.value != value) {
53          if ((value != null) && (!dataType.IsInstanceOfType(value)))
54            throw new ArgumentException(
55              string.Format("Type mismatch. Value is not a \"{0}\".",
56                            dataType.GetPrettyName())
57            );
58
59          if (this.value != null) this.value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
60          this.value = value;
61          if (this.value != null) this.value.ToStringChanged += new EventHandler(Value_ToStringChanged);
62          OnValueChanged();
63        }
64      }
65    }
66
67    public Result()
68      : base("Anonymous") {
69      this.dataType = typeof(IItem);
70      this.value = null;
71    }
72    public Result(string name, Type dataType)
73      : base(name) {
74      this.dataType = dataType;
75      this.value = null;
76    }
77    public Result(string name, string description, Type dataType)
78      : base(name, description) {
79      this.dataType = dataType;
80      this.value = null;
81    }
82    public Result(string name, IItem value)
83      : base(name) {
84      this.dataType = value == null ? typeof(IItem) : value.GetType();
85      this.value = value;
[3317]86      Initialize();
[3226]87    }
88    public Result(string name, string description, IItem value)
89      : base(name, description) {
90      this.dataType = value == null ? typeof(IItem) : value.GetType();
91      this.value = value;
[3317]92      Initialize();
[3226]93    }
[3317]94    [StorableConstructor]
95    private Result(bool deserializing) : base(deserializing) { }
[3226]96
[3317]97    [StorableHook(HookType.AfterDeserialization)]
98    private void Initialize() {
99      if (value != null) value.ToStringChanged += new EventHandler(Value_ToStringChanged);
100    }
101
[3226]102    public override IDeepCloneable Clone(Cloner cloner) {
[3317]103      Result clone = new Result(Name, Description, DataType);
[3226]104      cloner.RegisterClonedObject(this, clone);
[3317]105      clone.ReadOnlyView = ReadOnlyView;
106      clone.value = (IItem)cloner.Clone(value);
107      clone.Initialize();
[3226]108      return clone;
109    }
110
111    public override string ToString() {
112      return string.Format("{0}: {1} ({2})", Name, Value == null ? "null" : Value.ToString(), DataType.GetPrettyName());
113    }
114
115    public event EventHandler ValueChanged;
116    private void OnValueChanged() {
117      if (ValueChanged != null)
118        ValueChanged(this, EventArgs.Empty);
119      OnToStringChanged();
120    }
121
122    private void Value_ToStringChanged(object sender, EventArgs e) {
123      OnToStringChanged();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.