PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.ObjectModel/RmAttributeValue.cs

#
C# | 169 lines | 156 code | 12 blank | 1 comment | 24 complexity | 724a6250a1daef8c17d9ccb48ac25199 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5
  6namespace Microsoft.ResourceManagement.ObjectModel
  7{
  8    public class RmAttributeValue : ICloneable
  9    {
 10        List<IComparable> values;
 11        bool isMultiValue;
 12
 13        public RmAttributeValue()
 14        {
 15            this.values = new List<IComparable>();
 16        }
 17
 18        public RmAttributeValue(IComparable value)
 19        {
 20            this.values = new List<IComparable>();
 21            if (value != null)
 22                this.values.Add(value);
 23        }
 24
 25        public RmAttributeValue(IEnumerable<IComparable> values)
 26        {
 27            this.values = new List<IComparable>();
 28            this.values.AddRange(values);
 29            this.isMultiValue = true;
 30        }
 31
 32        public List<IComparable> Values
 33        {
 34            get
 35            {
 36                return this.values;
 37            }
 38        }
 39        public IComparable Value
 40        {
 41            get
 42            {
 43                if (this.values.Count > 0)
 44                {
 45                    return this.values[this.values.Count-1];
 46                }
 47                else
 48                {
 49                    return null;
 50                }
 51            }
 52            set
 53            {
 54                lock (this.values)
 55                {
 56                    if (this.values.Count > 0)
 57                    {
 58                        if (value == null)
 59                        {
 60                            this.values.Clear();
 61                        }
 62                        else
 63                        {
 64                            this.values.Add(value);
 65                        }
 66                    }
 67                    else
 68                    {
 69                        this.values.Add(value);
 70                    }
 71                }
 72            }
 73        }
 74
 75        public bool IsMultiValue
 76        {
 77            get
 78            {
 79                return this.isMultiValue || this.values.Count > 1;
 80            }
 81        }
 82
 83        #region ICloneable Members
 84
 85        public object Clone()
 86        {
 87            RmAttributeValue newValue = new RmAttributeValue();
 88            newValue.values = new List<IComparable>();
 89            foreach (IComparable value in this.values)
 90            {
 91                ICloneable cloneValue = value as ICloneable;
 92                if (cloneValue == null)
 93                {
 94                    newValue.values.Add(value);
 95                }
 96                else
 97                {
 98                    IComparable cloneInsert = cloneValue.Clone() as IComparable;
 99                    if (cloneInsert == null)
100                        throw new InvalidOperationException("A comparable, when cloned, returned a non-comparable: " + cloneValue.ToString());
101                    newValue.values.Add(cloneInsert);
102                }
103            }
104            return newValue;
105        }
106
107        #endregion
108
109        public override bool Equals(object obj)
110        {
111            RmAttributeValue other = obj as RmAttributeValue;
112            if (other == null)
113                return false;
114            lock (this.values)
115            {
116                lock (other.values)
117                {
118                    if (this.values.Count != other.values.Count)
119                        return false;
120                    this.values.Sort();
121                    other.values.Sort();
122                    for(int i=0;i<this.values.Count;i++)
123                    {
124                        if (this.values[i].Equals(other.values[i]) == false)
125                            return false;
126                    }
127                }
128            }
129            return true;
130        }
131
132        public override int GetHashCode()
133        {
134            return this.ToString().GetHashCode();
135        }
136
137        public override string ToString()
138        {
139            lock (this.values)
140            {
141                if (this.values.Count == 0)
142                {
143                    return @"RmAttributeValue.Null";
144                }
145                else if (this.values.Count == 1)
146                {
147                    return this.values[0].ToString();
148                }
149                else
150                {
151                    StringBuilder sb = new StringBuilder(this.values.Count);
152                    foreach (Object v in this.values)
153                    {
154                        sb.Append("{");
155                        sb.Append(v);
156                        sb.Append("}");
157                        sb.Append(@" ");
158                    }
159                    // take off the last space
160                    if (sb.Length > 0)
161                    {
162                        sb.Remove(sb.Length - 1, 1);
163                    }
164                    return sb.ToString();
165                }
166            }
167        }
168    }
169}