Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Data/3.3/ObjectData.cs @ 3644

Last change on this file since 3644 was 1842, checked in by epitzer, 15 years ago

Don't call Data.GetHashCode() if Data is null. (#631)

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