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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 | return Data.GetHashCode();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /// <summary>
|
---|
92 | /// Compares the current instance to the given <paramref name="obj"/>.
|
---|
93 | /// </summary>
|
---|
94 | /// <remarks>Can also compare basic data types with their representing HeuristicLab data types
|
---|
95 | /// (e.g. a <see cref="BoolData"/> with a boolean value).</remarks>
|
---|
96 | /// <exception cref="InvalidOperationException">Thrown when the current
|
---|
97 | /// instance does not implement the interface <see cref="IComparable"/></exception>
|
---|
98 | /// <param name="obj">The object to compare the current instance to.</param>
|
---|
99 | /// <returns><c>0</c> if the objects are the same, a value smaller than zero when the current instance
|
---|
100 | /// is smaller than the given <paramref name="obj"/> and a value greater than zero
|
---|
101 | /// when the current instance is greater than the given <paramref name="obj"/>.</returns>
|
---|
102 | public int CompareTo(object obj) {
|
---|
103 | IComparable comparable = Data as IComparable;
|
---|
104 | if (comparable != null) {
|
---|
105 | IObjectData other = obj as IObjectData;
|
---|
106 | if (other != null)
|
---|
107 | return comparable.CompareTo(other.Data);
|
---|
108 | else
|
---|
109 | return comparable.CompareTo(obj);
|
---|
110 | }
|
---|
111 | throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable");
|
---|
112 | }
|
---|
113 |
|
---|
114 | /// <summary>
|
---|
115 | /// The string representation of the current instance.
|
---|
116 | /// </summary>
|
---|
117 | /// <returns>"null" if property <see cref="Data"/> is <c>null</c>, else
|
---|
118 | /// the string representation of the current instance.</returns>
|
---|
119 | public override string ToString() {
|
---|
120 | if (Data == null)
|
---|
121 | return "null";
|
---|
122 | else
|
---|
123 | return Data.ToString();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|