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