Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.3/ItemTypeConstraint.cs @ 2989

Last change on this file since 2989 was 1823, checked in by epitzer, 15 years ago

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

File size: 4.1 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.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint that limits the type of a given item.
33  /// </summary>
34  /// <remarks>If the item is a <see cref="ConstrainedItemList"/>, any containing elements are limited to
35  /// the type and not the <see cref="ConstrainedItemList"/> itself.</remarks>
36  public class ItemTypeConstraint : ConstraintBase {
37
38    [Storable]
39    private Type type;
40    /// <summary>
41    /// Gets or sets the type to which the items should be limited.
42    /// </summary>
43    /// <remarks>Calls <see cref="ItemBase.OnChanged"/> of base class <see cref="ConstraintBase"/>
44    /// in the setter.</remarks>
45    public Type Type {
46      get { return type; }
47      set {
48        type = value;
49        OnChanged();
50      }
51    }
52
53    /// <inheritdoc select="summary"/>
54    public override string Description {
55      get {
56        return @"The ItemTypeConstraint limits the type of a given item.
57If the item is a ConstrainedItemList, any containing elements are limited to the type and not the ConstrainedItemList itself.";
58      }
59    }
60
61    /// <summary>
62    /// Initializes a new instance of <see cref="ItemTypeConstraint"/> with the <c>Type</c> property
63    /// set to <see cref="ItemBase"/> as default.
64    /// </summary>
65    public ItemTypeConstraint() {
66      type = typeof(ItemBase);
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="ItemTypeConstraint"/> with the given <paramref name="type"/>.
71    /// </summary>
72    /// <param name="type">The type the items should be limited to.</param>
73    public ItemTypeConstraint(Type type) {
74      this.type = type;
75    }
76
77    /// <summary>
78    /// Checks whether the given element fulfills the current constraint.
79    /// </summary>
80    /// <param name="data">The item to check.</param>
81    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
82    public override bool Check(IItem data) {
83      ConstrainedItemList list = (data as ConstrainedItemList);
84      if (list != null) {
85        for (int i = 0; i < list.Count; i++)
86          if (!list[i].GetType().Equals(type)) return false;
87        return true;
88      }
89      return data.GetType().Equals(type);
90    }
91
92    /// <summary>
93    /// Creates a new instance of <see cref="ItemTypeConstraintView"/> to represent the current
94    /// instance visually.
95    /// </summary>
96    /// <returns>The created view as <see cref="ItemTypeConstraintView"/>.</returns>
97    public override IView CreateView() {
98      return new ItemTypeConstraintView(this);
99    }
100
101    /// <summary>
102    /// Clones the current instance (deep clone).
103    /// </summary>
104    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
105    /// <returns>The cloned object as <see cref="ItemTypeConstraint"/>.</returns>
106    public override object Clone(IDictionary<Guid, object> clonedObjects) {
107      ItemTypeConstraint clone = new ItemTypeConstraint(type);
108      clonedObjects.Add(Guid, clone);
109      return clone;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.