#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Xml; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Data { /// /// Represents the base class for all base data types. /// public class ObjectData : ItemBase, IObjectData { [Storable] private object myData; /// /// Gets or sets the data to represent. /// /// Calls . public virtual object Data { get { return myData; } set { if (myData != value) { myData = value; OnChanged(); } } } /// /// Clones the current instance. /// /// HeuristicLab data items are cloned with the method of /// class (deep copy), all other items (like basic data types) /// are cloned with their own Clone methods (shadow copy). /// Thrown when the current instance is not cloneable. /// A dictionary of all already cloned objects. /// The clone instance. public override object Clone(IDictionary clonedObjects) { ObjectData clone = (ObjectData)base.Clone(clonedObjects); if (Data is IStorable) clone.myData = Auxiliary.Clone((IStorable)Data, clonedObjects); else if (Data is ICloneable) clone.myData = ((ICloneable)Data).Clone(); else throw new InvalidOperationException("contained object is not cloneable"); return clone; } /// /// Checks whether the current instance equals the specified . /// /// The object to compare with. /// true, if both objects are the same or /// the contained data is the same, false otherwise. public override bool Equals(object obj) { if(obj == this) return true; // same instance IObjectData other = obj as IObjectData; if(other != null) return Data.Equals(other.Data); // are the contained Data the same? else return false; } /// public override int GetHashCode() { return Data.GetHashCode(); } /// /// Compares the current instance to the given . /// /// Can also compare basic data types with their representing HeuristicLab data types /// (e.g. a with a boolean value). /// Thrown when the current /// instance does not implement the interface /// The object to compare the current instance to. /// 0 if the objects are the same, a value smaller than zero when the current instance /// is smaller than the given and a value greater than zero /// when the current instance is greater than the given . public int CompareTo(object obj) { IComparable comparable = Data as IComparable; if (comparable != null) { IObjectData other = obj as IObjectData; if (other != null) return comparable.CompareTo(other.Data); else return comparable.CompareTo(obj); } throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable"); } /// /// The string representation of the current instance. /// /// "null" if property is null, else /// the string representation of the current instance. public override string ToString() { if (Data == null) return "null"; else return Data.ToString(); } } }