Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ConstrainedObjectData.cs @ 1885

Last change on this file since 1885 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 5.6 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;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Data {
30  /// <summary>
31  /// A class representing all basic data types having some constraints.
32  /// </summary>
33  public class ConstrainedObjectData : ConstrainedItemBase, IObjectData {
34
35    [Storable]
36    private object myData;
37    /// <summary>
38    /// Gets or sets the object with constraints to represent.
39    /// </summary>
40    /// <remarks>Calls <see cref="HeuristicLab.Core.ItemBase.OnChanged"/> in the setter.
41    /// </remarks>
42    public virtual object Data {
43      get { return myData; }
44      set {
45        if (myData != value) {
46          myData = value;
47          OnChanged();
48        }
49      }
50    }
51
52    /// <summary>
53    /// Assigns the new <paramref name="data"/> if it is valid according to the constraints.
54    /// </summary>
55    /// <param name="data">The data to assign.</param>
56    /// <returns><c>true</c> if the new <paramref name="data"/> could be assigned, <c>false</c> otherwise.</returns>
57    public virtual bool TrySetData(object data) {
58      if (myData != data) {
59        object backup = myData;
60        myData = data;
61        if (IsValid()) {
62          OnChanged();
63          return true;
64        } else {
65          myData = backup;
66          return false;
67        }
68      }
69      return true;
70    }
71    /// <summary>
72    /// Assigns the new object <paramref name="data"/> if it is valid according to the constraints.
73    /// </summary>
74    /// <param name="data">The data to assign.</param>
75    /// <param name="violatedConstraints">Output parameter, containing all constraints that could not be fulfilled.</param>
76    /// <returns><c>true</c> if the new <paramref name="data"/> could be assigned, <c>false</c> otherwise.</returns>
77    public virtual bool TrySetData(object data, out ICollection<IConstraint> violatedConstraints) {
78      if (myData != data) {
79        object backup = myData;
80        myData = data;
81        if (IsValid(out violatedConstraints)) {
82          OnChanged();
83          return true;
84        } else {
85          myData = backup;
86          return false;
87        }
88      }
89      violatedConstraints = new List<IConstraint>();
90      return true;
91    }
92
93    /// <summary>
94    /// Clones the current object.
95    /// </summary>
96    /// <remarks>HeuristicLab data items are cloned with the <see cref="HeuristicLab.Core.Auxiliary.Clone"/> method of
97    /// class <see cref="Auxiliary"/> (deep copy), all other items (like basic data types)
98    /// are cloned with their own <c>Clone</c> methods (shadow copy).</remarks>
99    /// <param name="clonedObjects">All already cloned objects.</param>
100    /// <returns>The cloned object as <see cref="ConstrainedObjectData"/>.</returns>
101    public override object Clone(IDictionary<Guid, object> clonedObjects) {
102      ConstrainedObjectData clone = (ConstrainedObjectData)base.Clone(clonedObjects);
103      if (Data is IStorable)
104        clone.myData = Auxiliary.Clone((IStorable)Data, clonedObjects);
105      else if (Data is ICloneable)
106        clone.myData = ((ICloneable)Data).Clone();
107      else
108        throw new InvalidOperationException("contained object is not cloneable");
109      return clone;
110    }
111
112    /// <summary>
113    /// Compares the current instance to the given <paramref name="obj"/>.
114    /// </summary>
115    /// <remarks>Can also compare basic data types with their representing HeuristicLab data types
116    /// (e.g. a <see cref="ConstrainedDoubleData"/> with a double value).</remarks>
117    /// <exception cref="InvalidOperationException">Thrown when the current
118    /// instance does not implement the interface <see cref="IComparable"/></exception>
119    /// <param name="obj">The object to compare the current instance to.</param>
120    /// <returns><c>0</c> if the objects are the same, a value smaller than zero when the current instance
121    /// is smaller than the given <paramref name="obj"/> and a value greater than zero
122    /// when the current instance is greater than the given <paramref name="obj"/>.</returns>
123    public int CompareTo(object obj) {
124      IComparable comparable = Data as IComparable;
125      if (comparable != null) {
126        IObjectData other = obj as IObjectData;
127        if (other != null)
128          return comparable.CompareTo(other.Data);
129        else
130          return comparable.CompareTo(obj);
131      }
132      throw new InvalidOperationException("Cannot compare as contained object doesn't implement IComparable");
133    }
134
135    /// <summary>
136    /// The string representation of the current instance.
137    /// </summary>
138    /// <returns>The current instance as a string.</returns>
139    public override string ToString() {
140      return Data.ToString();
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.