Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringValue.cs @ 3157

Last change on this file since 3157 was 3054, checked in by swagner, 15 years ago

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Data {
27  [Item("StringValue", "Represents a string.")]
28  [Creatable("Test")]
29  [StorableClass]
30  public class StringValue : Item, IComparable, IStringConvertibleValue {
31    [Storable]
32    protected string value;
33    public virtual string Value {
34      get { return value; }
35      set {
36        if (value != this.value) {
37          if ((value != null) || (this.value != string.Empty)) {
38            this.value = value != null ? value : string.Empty;
39            OnValueChanged();
40          }
41        }
42      }
43    }
44
45    public StringValue() {
46      this.value = string.Empty;
47    }
48    public StringValue(string value) {
49      this.value = value != null ? value : string.Empty;
50    }
51
52    public override IDeepCloneable Clone(Cloner cloner) {
53      StringValue clone = new StringValue(value);
54      cloner.RegisterClonedObject(this, clone);
55      return clone;
56    }
57
58    public override string ToString() {
59      return value;
60    }
61
62    public virtual int CompareTo(object obj) {
63      StringValue other = obj as StringValue;
64      if (other != null)
65        return Value.CompareTo(other.Value);
66      else
67        return Value.CompareTo(obj);
68    }
69
70    public event EventHandler ValueChanged;
71    protected virtual void OnValueChanged() {
72      if (ValueChanged != null)
73        ValueChanged(this, EventArgs.Empty);
74      OnToStringChanged();
75    }
76
77    protected virtual bool Validate(string value, out string errorMessage) {
78      if (value == null) {
79        errorMessage = "Invalid Value (string must not be null)";
80        return false;
81      } else {
82        errorMessage = string.Empty;
83        return true;
84      }
85    }
86    protected virtual string GetValue() {
87      return Value;
88    }
89    protected virtual bool SetValue(string value) {
90      if (value != null) {
91        Value = value;
92        return true;
93      } else {
94        return false;
95      }
96    }
97
98    #region IStringConvertibleValue Members
99    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
100      return Validate(value, out errorMessage);
101    }
102    string IStringConvertibleValue.GetValue() {
103      return GetValue();
104    }
105    bool IStringConvertibleValue.SetValue(string value) {
106      return SetValue(value);
107    }
108    #endregion
109  }
110}
Note: See TracBrowser for help on using the repository browser.