[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[1853] | 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 25 |
|
---|
| 26 | namespace HeuristicLab.Data {
|
---|
[3048] | 27 | [Item("StringValue", "Represents a string.")]
|
---|
[3017] | 28 | [StorableClass]
|
---|
[3054] | 29 | public class StringValue : Item, IComparable, IStringConvertibleValue {
|
---|
[2669] | 30 | [Storable]
|
---|
[3054] | 31 | protected string value;
|
---|
| 32 | public virtual string Value {
|
---|
[2669] | 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;
|
---|
[2932] | 38 | OnValueChanged();
|
---|
[2669] | 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
[2] | 42 | }
|
---|
[2669] | 43 |
|
---|
[3048] | 44 | public StringValue() {
|
---|
[2669] | 45 | this.value = string.Empty;
|
---|
[2] | 46 | }
|
---|
[3048] | 47 | public StringValue(string value) {
|
---|
[2669] | 48 | this.value = value != null ? value : string.Empty;
|
---|
| 49 | }
|
---|
[2] | 50 |
|
---|
[2665] | 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 52 | StringValue clone = new StringValue(value);
|
---|
[2526] | 53 | cloner.RegisterClonedObject(this, clone);
|
---|
[2] | 54 | return clone;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[2669] | 57 | public override string ToString() {
|
---|
| 58 | return value;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[3054] | 61 | public virtual int CompareTo(object obj) {
|
---|
[3048] | 62 | StringValue other = obj as StringValue;
|
---|
[2757] | 63 | if (other != null)
|
---|
| 64 | return Value.CompareTo(other.Value);
|
---|
| 65 | else
|
---|
| 66 | return Value.CompareTo(obj);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[2932] | 69 | public event EventHandler ValueChanged;
|
---|
[3054] | 70 | protected virtual void OnValueChanged() {
|
---|
[2932] | 71 | if (ValueChanged != null)
|
---|
| 72 | ValueChanged(this, EventArgs.Empty);
|
---|
| 73 | OnToStringChanged();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[3054] | 76 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 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 | }
|
---|
[2676] | 84 | }
|
---|
[3054] | 85 | protected virtual string GetValue() {
|
---|
[2665] | 86 | return Value;
|
---|
[2] | 87 | }
|
---|
[3054] | 88 | protected virtual bool SetValue(string value) {
|
---|
[2694] | 89 | if (value != null) {
|
---|
| 90 | Value = value;
|
---|
| 91 | return true;
|
---|
| 92 | } else {
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
[2665] | 95 | }
|
---|
[3054] | 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 | }
|
---|
[2676] | 107 | #endregion
|
---|
[2] | 108 | }
|
---|
| 109 | }
|
---|