#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 {
///
/// A class representing all basic data types having some constraints.
///
public class ConstrainedObjectData : ConstrainedItemBase, IObjectData {
[Storable]
private object myData;
///
/// Gets or sets the object with constraints to represent.
///
/// Calls in the setter.
///
public virtual object Data {
get { return myData; }
set {
if (myData != value) {
myData = value;
OnChanged();
}
}
}
///
/// Assigns the new if it is valid according to the constraints.
///
/// The data to assign.
/// true if the new could be assigned, false otherwise.
public virtual bool TrySetData(object data) {
if (myData != data) {
object backup = myData;
myData = data;
if (IsValid()) {
OnChanged();
return true;
} else {
myData = backup;
return false;
}
}
return true;
}
///
/// Assigns the new object if it is valid according to the constraints.
///
/// The data to assign.
/// Output parameter, containing all constraints that could not be fulfilled.
/// true if the new could be assigned, false otherwise.
public virtual bool TrySetData(object data, out ICollection violatedConstraints) {
if (myData != data) {
object backup = myData;
myData = data;
if (IsValid(out violatedConstraints)) {
OnChanged();
return true;
} else {
myData = backup;
return false;
}
}
violatedConstraints = new List();
return true;
}
///
/// Clones the current object.
///
/// 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).
/// All already cloned objects.
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
ConstrainedObjectData clone = (ConstrainedObjectData)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;
}
///
/// Compares the current instance to the given .
///
/// Can also compare basic data types with their representing HeuristicLab data types
/// (e.g. a with a double 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.
///
/// The current instance as a string.
public override string ToString() {
return Data.ToString();
}
}
}