[2694] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2694] | 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;
|
---|
[3254] | 23 | using System.Linq;
|
---|
[2694] | 24 | using System.Collections;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3254] | 29 | using System.Collections.Generic;
|
---|
[2694] | 30 |
|
---|
| 31 | namespace HeuristicLab.Data {
|
---|
[3048] | 32 | [Item("StringArray", "Represents an array of strings.")]
|
---|
[3017] | 33 | [StorableClass]
|
---|
[3254] | 34 | public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
|
---|
[2694] | 35 | [Storable]
|
---|
[3054] | 36 | protected string[] array;
|
---|
[2694] | 37 |
|
---|
[3054] | 38 | public virtual int Length {
|
---|
[2694] | 39 | get { return array.Length; }
|
---|
[3054] | 40 | protected set {
|
---|
[2694] | 41 | if (value != Length) {
|
---|
| 42 | Array.Resize<string>(ref array, value);
|
---|
| 43 | OnReset();
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
[3054] | 47 | public virtual string this[int index] {
|
---|
[2694] | 48 | get { return array[index]; }
|
---|
| 49 | set {
|
---|
| 50 | if (value != array[index]) {
|
---|
| 51 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
| 52 | array[index] = value != null ? value : string.Empty;
|
---|
| 53 | OnItemChanged(index);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[3048] | 59 | public StringArray() {
|
---|
[2694] | 60 | array = new string[0];
|
---|
| 61 | }
|
---|
[3048] | 62 | public StringArray(int length) {
|
---|
[2694] | 63 | array = new string[length];
|
---|
| 64 | for (int i = 0; i < array.Length; i++)
|
---|
| 65 | array[i] = string.Empty;
|
---|
| 66 | }
|
---|
[3048] | 67 | public StringArray(string[] elements) {
|
---|
[2694] | 68 | if (elements == null) throw new ArgumentNullException();
|
---|
| 69 | array = new string[elements.Length];
|
---|
| 70 | for (int i = 0; i < array.Length; i++)
|
---|
| 71 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3054] | 75 | StringArray clone = new StringArray();
|
---|
[2694] | 76 | cloner.RegisterClonedObject(this, clone);
|
---|
[3054] | 77 | clone.array = (string[])array.Clone();
|
---|
[2694] | 78 | return clone;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public override string ToString() {
|
---|
| 82 | StringBuilder sb = new StringBuilder();
|
---|
| 83 | sb.Append("[");
|
---|
| 84 | if (array.Length > 0) {
|
---|
| 85 | sb.Append(array[0]);
|
---|
| 86 | for (int i = 1; i < array.Length; i++)
|
---|
| 87 | sb.Append(";").Append(array[i]);
|
---|
| 88 | }
|
---|
| 89 | sb.Append("]");
|
---|
| 90 | return sb.ToString();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[3254] | 93 | public IEnumerator<string> GetEnumerator() {
|
---|
| 94 | return (IEnumerator<string>)array.GetEnumerator();
|
---|
[2694] | 95 | }
|
---|
| 96 |
|
---|
[3254] | 97 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 98 | return ((IEnumerable<string>)this).GetEnumerator();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[3054] | 101 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 102 | if (value == null) {
|
---|
| 103 | errorMessage = "Invalid Value (string must not be null)";
|
---|
| 104 | return false;
|
---|
| 105 | } else {
|
---|
| 106 | errorMessage = string.Empty;
|
---|
| 107 | return true;
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
[3054] | 110 | protected virtual string GetValue(int index) {
|
---|
[2973] | 111 | return this[index];
|
---|
[2694] | 112 | }
|
---|
[3054] | 113 | protected virtual bool SetValue(string value, int index) {
|
---|
[2694] | 114 | if (value != null) {
|
---|
[2973] | 115 | this[index] = value;
|
---|
[2694] | 116 | return true;
|
---|
| 117 | } else {
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
[3054] | 121 |
|
---|
[2973] | 122 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
[3054] | 123 | protected virtual void OnItemChanged(int index) {
|
---|
[2694] | 124 | if (ItemChanged != null)
|
---|
[2973] | 125 | ItemChanged(this, new EventArgs<int>(index));
|
---|
[2932] | 126 | OnToStringChanged();
|
---|
[2694] | 127 | }
|
---|
[2973] | 128 | public event EventHandler Reset;
|
---|
[3054] | 129 | protected virtual void OnReset() {
|
---|
[2694] | 130 | if (Reset != null)
|
---|
| 131 | Reset(this, EventArgs.Empty);
|
---|
[2932] | 132 | OnToStringChanged();
|
---|
[2694] | 133 | }
|
---|
[3054] | 134 |
|
---|
| 135 | #region IStringConvertibleArray Members
|
---|
| 136 | int IStringConvertibleArray.Length {
|
---|
| 137 | get { return Length; }
|
---|
| 138 | set { Length = value; }
|
---|
| 139 | }
|
---|
| 140 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
| 141 | return Validate(value, out errorMessage);
|
---|
| 142 | }
|
---|
| 143 | string IStringConvertibleArray.GetValue(int index) {
|
---|
| 144 | return GetValue(index);
|
---|
| 145 | }
|
---|
| 146 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
| 147 | return SetValue(value, index);
|
---|
| 148 | }
|
---|
[2694] | 149 | #endregion
|
---|
| 150 | }
|
---|
| 151 | }
|
---|