PageRenderTime 15ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.ObjectModel/RmReference.cs

#
C# | 146 lines | 127 code | 18 blank | 1 comment | 28 complexity | 34fd1fcfe8de0f90ec7bb683199125e4 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5
  6namespace Microsoft.ResourceManagement.ObjectModel
  7{
  8    public class RmReference : IComparable, IComparable<RmReference>
  9    {
 10        Guid guidValue;
 11        String stringValue;
 12        public RmReference()
 13        {
 14            this.Value = String.Empty;
 15        }
 16        public RmReference(String value)
 17        {
 18            this.Value = value;
 19        }
 20
 21        static String RemoveReference(String input)
 22        {
 23            if (String.IsNullOrEmpty(input))
 24                return Guid.Empty.ToString();
 25            else
 26                return input.Replace(@"urn:uuid:", String.Empty);
 27        }
 28
 29        static String AddReference(String input)
 30        {
 31            if (String.IsNullOrEmpty(input))
 32                return null;
 33            else
 34                //return String.Format(@"urn:uuid:{0}", this.value.ToString());
 35                return input;
 36        }
 37
 38        public String Value
 39        {
 40            get
 41            {
 42                return this.stringValue;
 43            }
 44            set
 45            {
 46                try
 47                {
 48                    this.guidValue = new Guid(RmReference.RemoveReference(value));
 49                    this.stringValue = RmReference.AddReference(this.guidValue.ToString());
 50                }
 51                catch (FormatException)
 52                {
 53                    throw new ArgumentException("The provided value did not match the reference format of urn:uuid:{guid}");
 54                }
 55            }
 56        }
 57
 58        public override string ToString()
 59        {
 60            return this.stringValue ;
 61        }
 62
 63        public override bool Equals(object obj)
 64        {
 65            RmReference other = obj as RmReference;
 66            if (other as Object == null || other.guidValue == null)
 67                return false;
 68            else
 69                return other.guidValue.Equals(this.guidValue);
 70        }
 71        public override int GetHashCode()
 72        {
 73            return this.guidValue.GetHashCode();
 74        }
 75
 76        #region IComparable Members
 77
 78        public int CompareTo(object obj)
 79        {
 80            if (obj as Object == null)
 81                throw new ArgumentNullException("obj");
 82            RmReference reference = obj as RmReference;
 83            if(reference as Object == null)
 84                throw new ArgumentNullException("obj");
 85            return this.CompareTo(reference);
 86        }
 87
 88        public static bool operator ==(RmReference attrib1, RmReference attrib2)
 89        {
 90            if (attrib1 as Object == null)
 91                return false;
 92            if (attrib2 as Object == null)
 93                return false;
 94            return attrib1.CompareTo(attrib2) == 0;
 95        }
 96
 97        public static bool operator !=(RmReference attrib1, RmReference attrib2)
 98        {
 99            if (attrib1 == null)
100                return false;
101            return attrib1.CompareTo(attrib2) != 0;
102        }
103
104        public static bool operator <(RmReference attrib1, RmReference attrib2)
105        {
106            if (attrib1 == null)
107                return false;
108            return attrib1.CompareTo(attrib2) < 0;
109        }
110
111        public static bool operator >(RmReference attrib1, RmReference attrib2)
112        {
113            if (attrib1 == null)
114                return false;
115            return attrib1.CompareTo(attrib2) > 0;
116        }
117
118        public static bool operator <=(RmReference attrib1, RmReference attrib2)
119        {
120            if (attrib1 == null)
121                return false;
122            return attrib1.CompareTo(attrib2) <= 0;
123        }
124
125        public static bool operator >=(RmReference attrib1, RmReference attrib2)
126        {
127            if (attrib1 == null)
128                return false;
129            return attrib1.CompareTo(attrib2) >= 0;
130        }
131
132        #endregion
133
134        #region IComparable<RmReference> Members
135
136        public int CompareTo(RmReference other)
137        {
138            if (other as object == null)
139                throw new ArgumentNullException("other");
140            else
141                return this.guidValue.CompareTo(other.guidValue);
142        }
143
144        #endregion
145    }
146}