Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/ConstrainedTypeValue.cs @ 8535

Last change on this file since 8535 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
31  [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
32  [StorableClass]
33  public class ConstrainedTypeValue : TypeValue {
34    private IEnumerable<Type> validTypes;
35    public IEnumerable<Type> ValidTypes {
36      get { return validTypes; }
37      set { validTypes = value; }
38    }
39
40    public override Type Value {
41      set {
42        if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes");
43        base.Value = value;
44      }
45    }
46
47    #region Constructors and Cloning
48    [StorableConstructor]
49    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
50    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner)
51      : base(original, cloner) {
52      if (original.validTypes != null)
53        this.validTypes = new List<Type>(original.validTypes);
54    }
55    public ConstrainedTypeValue()
56      : base() {
57      this.readOnly = false;
58    }
59    public ConstrainedTypeValue(Type value)
60      : this() {
61      this.Value = value;
62    }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new ConstrainedTypeValue(this, cloner);
65    }
66    #endregion
67  }
68
69  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
70  [StorableClass]
71  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
72    [StorableConstructor]
73    protected ConstrainedTypeValue(bool deserializing)
74      : base(deserializing) {
75      if (deserializing) {
76        this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
77      }
78    }
79    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
80    public ConstrainedTypeValue()
81      : base() {
82      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
83      this.Value = ValidTypes.First();
84    }
85    public ConstrainedTypeValue(Type value)
86      : base() {
87      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
88      this.Value = value;
89    }
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new ConstrainedTypeValue<T>(this, cloner);
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.