[2] | 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 |
|
---|
| 28 | namespace HeuristicLab.Data {
|
---|
[737] | 29 | /// <summary>
|
---|
| 30 | /// Represents the base class for all base data types.
|
---|
| 31 | /// </summary>
|
---|
[2] | 32 | public class ObjectData : ItemBase, IObjectData {
|
---|
| 33 | private object myData;
|
---|
[737] | 34 | /// <summary>
|
---|
| 35 | /// Gets or sets the data to represent.
|
---|
| 36 | /// </summary>
|
---|
| 37 | /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/>.</remarks>
|
---|
[2] | 38 | public virtual object Data {
|
---|
| 39 | get { return myData; }
|
---|
| 40 | set {
|
---|
| 41 | if (myData != value) {
|
---|
| 42 | myData = value;
|
---|
| 43 | OnChanged();
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[737] | 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>
|
---|
[2] | 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 |
|
---|
[737] | 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>
|
---|
[66] | 74 | public override bool Equals(object obj) {
|
---|
[508] | 75 | if(obj == this) return true; // same instance
|
---|
[66] | 76 | IObjectData other = obj as IObjectData;
|
---|
[508] | 77 | if(other != null)
|
---|
| 78 | return Data.Equals(other.Data); // are the contained Data the same?
|
---|
[66] | 79 | else
|
---|
[508] | 80 | return false;
|
---|
[66] | 81 | }
|
---|
| 82 |
|
---|
[737] | 83 | /// <inheritdoc cref="object.GetHashCode"/>
|
---|
[66] | 84 | public override int GetHashCode() {
|
---|
| 85 | return Data.GetHashCode();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[737] | 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>
|
---|
[2] | 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 |
|
---|
[737] | 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>
|
---|
[2] | 116 | public override string ToString() {
|
---|
| 117 | if (Data == null)
|
---|
| 118 | return "null";
|
---|
| 119 | else
|
---|
| 120 | return Data.ToString();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[737] | 123 | /// <summary>
|
---|
| 124 | /// The point of intersection where an <see cref="IObjectDataVisitor"/>
|
---|
| 125 | /// can change the object.
|
---|
| 126 | /// </summary>
|
---|
| 127 | /// <param name="visitor">The visitor that changes the element.</param>
|
---|
[2] | 128 | public virtual void Accept(IObjectDataVisitor visitor) {
|
---|
| 129 | visitor.Visit(this);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|