Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/NamedItem.cs @ 2830

Last change on this file since 2830 was 2830, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 5.3 KB
Line 
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 System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  [Item("NamedItem", "Base class for items which have a name and an optional description.")]
31  public abstract class NamedItem : Item, INamedItem {
32    [Storable]
33    private string name;
34    /// <inheritdoc/>
35    /// <remarks>Calls <see cref="OnNameChanging"/> and also <see cref="OnNameChanged"/>
36    /// eventually in the setter.</remarks>
37    public string Name {
38      get { return name; }
39      set {
40        if (!CanChangeName) throw new NotSupportedException("Name of NamedItem cannot be changed.");
41        if (value == null) throw new ArgumentNullException();
42        if (!name.Equals(value)) {
43          CancelEventArgs<string> e = new CancelEventArgs<string>(value);
44          OnNameChanging(e);
45          if (!e.Cancel) {
46            name = value;
47            OnNameChanged();
48          }
49        }
50      }
51    }
52    public virtual bool CanChangeName {
53      get { return true; }
54    }
55    [Storable]
56    private string description;
57    public string Description {
58      get { return description; }
59      set {
60        if (!CanChangeDescription) throw new NotSupportedException("Description of NamedItem cannot be changed.");
61        if ((description == null) || (!description.Equals(value))) {
62          description = value;
63          OnDescriptionChanged();
64        }
65      }
66    }
67    public virtual bool CanChangeDescription {
68      get { return true; }
69    }
70
71    /// <summary>
72    /// Initializes a new instance of <see cref="Variable"/> with name <c>Anonymous</c>
73    /// and value <c>null</c>.
74    /// </summary>
75    protected NamedItem() {
76      name = string.Empty;
77      description = string.Empty;
78    }
79    /// <summary>
80    /// Initializes a new instance of <see cref="Variable"/> with the specified <paramref name="name"/>
81    /// and the specified <paramref name="value"/>.
82    /// </summary>
83    /// <param name="name">The name of the current instance.</param>
84    /// <param name="value">The value of the current instance.</param>
85    protected NamedItem(string name) {
86      if (name == null) throw new ArgumentNullException();
87      this.name = name;
88      description = string.Empty;
89    }
90    protected NamedItem(string name, string description) {
91      if (name == null) throw new ArgumentNullException();
92      this.name = name;
93      this.description = description;
94    }
95
96    /// <summary>
97    /// Clones the current instance (deep clone).
98    /// </summary>
99    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
100    /// <returns>The cloned object as <see cref="Variable"/>.</returns>
101    public override IDeepCloneable Clone(Cloner cloner) {
102      NamedItem clone = (NamedItem)base.Clone(cloner);
103      clone.name = name;
104      clone.description = description;
105      return clone;
106    }
107
108    /// <summary>
109    /// Gets the string representation of the current instance in the format: <c>Name: [null|Value]</c>.
110    /// </summary>
111    /// <returns>The current instance as a string.</returns>
112    public override string ToString() {
113      return Name;
114    }
115
116    /// <inheritdoc/>
117    public event EventHandler<CancelEventArgs<string>> NameChanging;
118    /// <summary>
119    /// Fires a new <c>NameChanging</c> event.
120    /// </summary>
121    /// <param name="e">The event arguments of the changing.</param>
122    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
123      if (NameChanging != null)
124        NameChanging(this, e);
125    }
126    /// <inheritdoc/>
127    public event EventHandler NameChanged;
128    /// <summary>
129    /// Fires a new <c>NameChanged</c> event.
130    /// </summary>
131    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
132    protected virtual void OnNameChanged() {
133      if (NameChanged != null)
134        NameChanged(this, EventArgs.Empty);
135      OnChanged();
136    }
137    /// <inheritdoc/>
138    public event EventHandler DescriptionChanged;
139    /// <summary>
140    /// Fires a new <c>DescriptionChanged</c> event.
141    /// </summary>
142    /// <remarks>Calls <see cref="ItemBase.OnChanged"/>.</remarks>
143    protected virtual void OnDescriptionChanged() {
144      if (DescriptionChanged != null)
145        DescriptionChanged(this, EventArgs.Empty);
146      OnChanged();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.