moel@109: /* moel@109: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@109: moel@344: Copyright (C) 2009-2010 Michael Möller moel@344: moel@109: */ moel@109: moel@109: using System; moel@195: using System.Collections.Generic; moel@109: using System.Text; moel@109: moel@109: namespace OpenHardwareMonitor.Hardware { moel@176: public class Identifier : IComparable { moel@195: private readonly string identifier; moel@109: moel@195: private const char Separator = '/'; moel@109: moel@195: private static void CheckIdentifiers(IEnumerable identifiers) { moel@109: foreach (string s in identifiers) moel@195: if (s.Contains(" ") || s.Contains(Separator.ToString())) moel@109: throw new ArgumentException("Invalid identifier"); moel@109: } moel@109: moel@109: public Identifier(params string[] identifiers) { moel@109: CheckIdentifiers(identifiers); moel@109: moel@109: StringBuilder s = new StringBuilder(); moel@109: for (int i = 0; i < identifiers.Length; i++) { moel@195: s.Append(Separator); moel@109: s.Append(identifiers[i]); moel@109: } moel@109: this.identifier = s.ToString(); moel@109: } moel@109: moel@109: public Identifier(Identifier identifier, params string[] extensions) { moel@109: CheckIdentifiers(extensions); moel@109: moel@109: StringBuilder s = new StringBuilder(); moel@109: s.Append(identifier.ToString()); moel@109: for (int i = 0; i < extensions.Length; i++) { moel@195: s.Append(Separator); moel@109: s.Append(extensions[i]); moel@109: } moel@109: this.identifier = s.ToString(); moel@109: } moel@109: moel@109: public override string ToString() { moel@109: return identifier; moel@109: } moel@109: moel@195: public override bool Equals(Object obj) { moel@109: if (obj == null) moel@109: return false; moel@109: moel@109: Identifier id = obj as Identifier; moel@109: if (id == null) moel@109: return false; moel@109: moel@109: return (identifier == id.identifier); moel@109: } moel@109: moel@109: public override int GetHashCode() { moel@109: return identifier.GetHashCode(); moel@109: } moel@176: moel@176: public int CompareTo(Identifier other) { moel@176: if (other == null) moel@176: return 1; moel@176: else moel@182: return string.Compare(this.identifier, other.identifier, moel@182: StringComparison.Ordinal); moel@176: } moel@182: moel@182: public static bool operator ==(Identifier id1, Identifier id2) { moel@184: if (id1.Equals(null)) moel@184: return id2.Equals(null); moel@182: else moel@182: return id1.Equals(id2); moel@182: } moel@182: moel@182: public static bool operator !=(Identifier id1, Identifier id2) { moel@182: return !(id1 == id2); moel@182: } moel@182: moel@182: public static bool operator <(Identifier id1, Identifier id2) { moel@182: if (id1 == null) moel@182: return id2 != null; moel@182: else moel@182: return (id1.CompareTo(id2) < 0); moel@182: } moel@182: moel@182: public static bool operator >(Identifier id1, Identifier id2) { moel@182: if (id1 == null) moel@182: return false; moel@182: else moel@182: return (id1.CompareTo(id2) > 0); moel@182: } moel@182: moel@109: } moel@109: }