5 // Atsushi Enomoto <atsushi@ximian.com>
6 // Jonathan Pryor <jpryor@novell.com>
8 // Copyright (C) 2005,2007,2009 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections.Generic;
32 using System.Security;
42 : this (Guid.NewGuid ())
46 public UniqueId (byte [] id)
51 public UniqueId (Guid id)
60 public UniqueId (string value)
63 throw new ArgumentNullException ("value cannot be null", "value");
65 if (value.Length == 0)
66 throw new FormatException ("UniqueId cannot be zero length");
75 public UniqueId (byte [] id, int offset)
78 throw new ArgumentNullException ();
80 throw new ArgumentOutOfRangeException ("offset");
81 if (offset >= id.Length)
82 throw new ArgumentException ("id too small.", "offset");
84 if (id.Length - offset != 16)
85 throw new ArgumentException ("id and offset provide less than 16 bytes");
90 List<byte> buf = new List<byte> (id);
91 buf.RemoveRange (0, offset);
92 guid = new Guid (buf.ToArray ());
100 public UniqueId (char [] id, int offset, int count)
103 throw new ArgumentNullException ("id");
104 if (offset < 0 || offset >= id.Length)
105 throw new ArgumentOutOfRangeException ("offset");
106 if (count < 0 || id.Length - offset < count)
107 throw new ArgumentOutOfRangeException ("count");
109 throw new FormatException ();
111 // Does it start with "urn:uuid:"? If so, it's a Guid.
112 if (count > 8 && id [offset] == 'u' && id [offset+1] == 'r' &&
113 id [offset+2] == 'n' && id [offset+3] == ':' &&
114 id [offset+4] == 'u' && id [offset+5] == 'u' &&
115 id [offset+6] == 'i' && id [offset+7] == 'd' &&
116 id [offset+8] == ':') {
118 throw new ArgumentOutOfRangeException ("Invalid Guid");
119 this.guid = new Guid (new string (id, offset+9, count-9));
121 this.id = new string (id, offset, count);
124 public int CharArrayLength {
127 [SecurityTreatAsSafe]
129 get {return id != null ? id.Length : 45;}
133 get { return guid != default (Guid); }
136 public override bool Equals (Object obj)
138 UniqueId other = obj as UniqueId;
143 if (IsGuid && other.IsGuid) {
144 return guid.Equals (other.guid);
147 return id == other.id;
150 [MonoTODO ("Determine semantics when IsGuid==true")]
151 public override int GetHashCode ()
153 return id != null ? id.GetHashCode () : guid.GetHashCode ();
156 public static bool operator == (UniqueId id1, UniqueId id2)
158 return object.Equals (id1, id2);
161 public static bool operator != (UniqueId id1, UniqueId id2)
163 return ! (id1 == id2);
168 [SecurityTreatAsSafe]
170 public int ToCharArray (char [] array, int offset)
173 throw new ArgumentNullException ("array");
174 if (offset < 0 || offset >= array.Length)
175 throw new ArgumentOutOfRangeException ("offset");
177 string s = ToString ();
178 s.CopyTo (0, array, offset, s.Length);
184 [SecurityTreatAsSafe]
186 public override string ToString ()
189 return "urn:uuid:" + guid;
194 public bool TryGetGuid (out Guid guid)
200 guid = default (Guid);
207 [SecurityTreatAsSafe]
209 public bool TryGetGuid (byte [] buffer, int offset)
215 throw new ArgumentNullException ("buffer");
216 if (offset < 0 || offset >= buffer.Length)
217 throw new ArgumentOutOfRangeException ("offset");
218 if (buffer.Length - offset < 16)
219 throw new ArgumentOutOfRangeException ("offset");
221 guid.ToByteArray ().CopyTo (buffer, offset);