Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/BoolValue.cs @ 15401

Last change on this file since 15401 was 13827, checked in by jlodewyc, 9 years ago

#2582 Parameter changing busy, save file, download file and email on pass reset

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Data {
30  [Item("BoolValue", "Represents a boolean value.")]
31  [StorableClass]
32  public class BoolValue : ValueTypeValue<bool>, IComparable, IStringConvertibleValue {
33    public static new Image StaticItemImage
34    {
35      get { return new Bitmap(25, 25); }
36    }
37
38    protected BoolValue(BoolValue original, Cloner cloner)
39      : base(original, cloner) {
40    }
41    public BoolValue() : base() { }
42    [StorableConstructor]
43    public BoolValue(bool value)
44      : base(value) {
45      //mkommend: Be aware that the base call refers to the storable ctor => the value is set explicitly in the ctor body.
46      //          This should not affect the persistence, because first the ctor is called and afterwards the values are set by reflection.
47      this.value = value;
48    }
49
50        public static BoolValue Parse(string val)
51        {
52            if (val == "true" || val == "on")
53                return new BoolValue(true);
54            else
55                return new BoolValue(false);
56        }
57
58        public override IDeepCloneable Clone(Cloner cloner) {
59      return new BoolValue(this, cloner);
60    }
61
62    public virtual int CompareTo(object obj) {
63      BoolValue other = obj as BoolValue;
64      if (other != null)
65        return Value.CompareTo(other.Value);
66      else
67        return Value.CompareTo(obj);
68    }
69
70    protected virtual bool Validate(string value, out string errorMessage) {
71      bool val;
72      bool valid = bool.TryParse(value, out val);
73      errorMessage = string.Empty;
74      if (!valid) {
75        StringBuilder sb = new StringBuilder();
76        sb.Append("Invalid Value (Valid Value Format: \"");
77        sb.Append(FormatPatterns.GetBoolFormatPattern());
78        sb.Append("\")");
79        errorMessage = sb.ToString();
80      }
81      return valid;
82    }
83    protected virtual string GetValue() {
84      return Value.ToString();
85    }
86    protected virtual bool SetValue(string value) {
87      bool val;
88      if (bool.TryParse(value, out val)) {
89        Value = val;
90        return true;
91      } else {
92        return false;
93      }
94    }
95
96    #region IStringConvertibleValue Members
97    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
98      return Validate(value, out errorMessage);
99    }
100    string IStringConvertibleValue.GetValue() {
101      return GetValue();
102    }
103    bool IStringConvertibleValue.SetValue(string value) {
104      return SetValue(value);
105    }
106    #endregion
107  }
108}
Note: See TracBrowser for help on using the repository browser.