Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactorBranch/HeuristicLab.Data/ObjectData.cs @ 885

Last change on this file since 885 was 885, checked in by gkronber, 15 years ago

Refactored cloning in HL.Core, HL.Data and HL.Constraints

#285 (Cloning could be improved by creating objects at the bottom of the cloning chain with 'new' instead of the top with Activator.CreateInstance())

File size: 5.5 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    /// Default constructor to create ObjectData instances
50    /// </summary>
51    public ObjectData() { }
52
53    /// <summary>
54    /// Copy constructor to create clones (deep).
55    /// </summary>
56    /// <param name="original">The original instance to be cloned.</param>
57    public ObjectData(ObjectData original) : this(original, new Dictionary<Guid, object>()) { }
58
59    /// <summary>
60    /// Copy constructor, creates a deep copy of the ObjectData instance.
61    /// </summary>
62    /// <remarks>HeuristicLab data items are cloned with the <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of
63    /// class <see cref="Auxiliary"/> (deep copy), all other items (like basic data types)
64    /// are cloned with their own <c>Clone</c> methods (shadow copy).</remarks>
65    /// <exception cref="InvalidOperationException">Thrown when the current instance is not cloneable.</exception>
66    /// <param name="clonedObjects">A dictionary of all already cloned objects (for referential integrity).</param>
67    protected ObjectData(ObjectData original, IDictionary<Guid, object> clonedObjects)
68      : base(original, clonedObjects) {
69      if (original.Data is IStorable)
70        this.myData = Auxiliary.Clone((IStorable)original.Data, clonedObjects);
71      else if (original.Data is ICloneable)
72        this.myData = ((ICloneable)original.Data).Clone();
73      else
74        throw new InvalidOperationException("contained object is not cloneable");
75    }
76
77    /// <summary>
78    /// Checks whether the current instance equals the specified <paramref name="obj"/>.
79    /// </summary>
80    /// <param name="obj">The object to compare with.</param>
81    /// <returns><c>true</c>, if both objects are the same or
82    /// the contained data is the same, <c>false</c> otherwise.</returns>
83    public override bool Equals(object obj) {
84      if (obj == this) return true; // same instance
85      IObjectData other = obj as IObjectData;
86      if (other != null)
87        return Data.Equals(other.Data); // are the contained Data the same?
88      else
89        return false;
90    }
91
92    /// <inheritdoc cref="object.GetHashCode"/>
93    public override int GetHashCode() {
94      return Data.GetHashCode();
95    }
96
97    /// <summary>
98    /// Compares the current instance to the given <paramref name="obj"/>.
99    /// </summary>
100    /// <remarks>Can also compare basic data types with their representing HeuristicLab data types
101    /// (e.g. a <see cref="BoolData"/> with a boolean value).</remarks>
102    /// <exception cref="InvalidOperationException">Thrown when the current
103    /// instance does not implement the interface <see cref="IComparable"/></exception>
104    /// <param name="obj">The object to compare the current instance to.</param>
105    /// <returns><c>0</c> if the objects are the same, a value smaller than zero when the current instance
106    /// is smaller than the given <paramref name="obj"/> and a value greater than zero
107    /// when the current instance is greater than the given <paramref name="obj"/>.</returns>
108    public int CompareTo(object obj) {
109      IComparable comparable = Data as IComparable;
110      if (comparable != null) {
111        IObjectData other = obj as IObjectData;
112        if (other != null)
113          return comparable.CompareTo(other.Data);
114        else
115          return comparable.CompareTo(obj);
116      }
117      throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable");
118    }
119
120    public override object Clone(IDictionary<Guid, object> clonedObjects) {
121      return new ObjectData(this, clonedObjects);
122    }
123
124    /// <summary>
125    /// The string representation of the current instance.
126    /// </summary>
127    /// <returns>"null" if property <see cref="Data"/> is <c>null</c>, else
128    /// the string representation of the current instance.</returns>
129    public override string ToString() {
130      if (Data == null)
131        return "null";
132      else
133        return Data.ToString();
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.