Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.Data/3.2/ObjectData.cs @ 2587

Last change on this file since 2587 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Core;
27
28namespace HeuristicLab.Data {
29  /// <summary>
30  /// Represents the base class for all base data types.
31  /// </summary>
32  public class ObjectData : ItemBase, IObjectData {
33    private object myData;
34    /// <summary>
35    /// Gets or sets the data to represent.
36    /// </summary>
37    /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
38    public virtual object Data {
39      get { return myData; }
40      set {
41        if (myData != value) {
42          myData = value;
43          OnChanged();
44        }
45      }
46    }
47
48    /// <summary>
49    /// Clones the current instance.
50    /// </summary>
51    /// <remarks>HeuristicLab data items are cloned with the <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of
52    /// class <see cref="Auxiliary"/> (deep copy), all other items (like basic data types)
53    /// are cloned with their own <c>Clone</c> methods (shadow copy).</remarks>
54    /// <exception cref="InvalidOperationException">Thrown when the current instance is not cloneable.</exception>
55    /// <param name="clonedObjects">A dictionary of all already cloned objects.</param>
56    /// <returns>The clone instance.</returns>
57    public override object Clone(IDictionary<Guid, object> clonedObjects) {
58      ObjectData clone = (ObjectData)base.Clone(clonedObjects);
59      if (Data is IStorable)
60        clone.myData = Auxiliary.Clone((IStorable)Data, clonedObjects);
61      else if (Data is ICloneable)
62        clone.myData = ((ICloneable)Data).Clone();
63      else
64        throw new InvalidOperationException("contained object is not cloneable");
65      return clone;
66    }
67
68    /// <summary>
69    /// Checks whether the current instance equals the specified <paramref name="obj"/>.
70    /// </summary>
71    /// <param name="obj">The object to compare with.</param>
72    /// <returns><c>true</c>, if both objects are the same or
73    /// the contained data is the same, <c>false</c> otherwise.</returns>
74    public override bool Equals(object obj) {
75      if(obj == this) return true; // same instance
76      IObjectData other = obj as IObjectData;
77      if(other != null)
78        return Data.Equals(other.Data); // are the contained Data the same?
79      else
80        return false;
81    }
82
83    /// <inheritdoc cref="object.GetHashCode"/>
84    public override int GetHashCode() {
85      return Data.GetHashCode();
86    }
87
88    /// <summary>
89    /// Compares the current instance to the given <paramref name="obj"/>.
90    /// </summary>
91    /// <remarks>Can also compare basic data types with their representing HeuristicLab data types
92    /// (e.g. a <see cref="BoolData"/> with a boolean value).</remarks>
93    /// <exception cref="InvalidOperationException">Thrown when the current
94    /// instance does not implement the interface <see cref="IComparable"/></exception>
95    /// <param name="obj">The object to compare the current instance to.</param>
96    /// <returns><c>0</c> if the objects are the same, a value smaller than zero when the current instance
97    /// is smaller than the given <paramref name="obj"/> and a value greater than zero
98    /// when the current instance is greater than the given <paramref name="obj"/>.</returns>
99    public int CompareTo(object obj) {
100      IComparable comparable = Data as IComparable;
101      if (comparable != null) {
102        IObjectData other = obj as IObjectData;
103        if (other != null)
104          return comparable.CompareTo(other.Data);
105        else
106          return comparable.CompareTo(obj);
107      }
108      throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable");
109    }
110
111    /// <summary>
112    /// The string representation of the current instance.
113    /// </summary>
114    /// <returns>"null" if property <see cref="Data"/> is <c>null</c>, else
115    /// the string representation of the current instance.</returns>
116    public override string ToString() {
117      if (Data == null)
118        return "null";
119      else
120        return Data.ToString();
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.