Hardware/Identifier.cs
author moel.mich
Tue, 30 Dec 2014 21:04:54 +0000
changeset 430 6b24e39f1b84
parent 195 0ee888c485d5
permissions -rw-r--r--
Fixed Issue 651.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Text;
    14 
    15 namespace OpenHardwareMonitor.Hardware {
    16   public class Identifier : IComparable<Identifier> {
    17     private readonly string identifier;
    18 
    19     private const char Separator = '/';
    20 
    21     private static void CheckIdentifiers(IEnumerable<string> identifiers) {      
    22       foreach (string s in identifiers)
    23         if (s.Contains(" ") || s.Contains(Separator.ToString()))
    24           throw new ArgumentException("Invalid identifier");
    25     }
    26 
    27     public Identifier(params string[] identifiers) {
    28       CheckIdentifiers(identifiers);
    29 
    30       StringBuilder s = new StringBuilder();
    31       for (int i = 0; i < identifiers.Length; i++) {
    32         s.Append(Separator);
    33         s.Append(identifiers[i]);
    34       }
    35       this.identifier = s.ToString();
    36     }
    37 
    38     public Identifier(Identifier identifier, params string[] extensions) {
    39       CheckIdentifiers(extensions);
    40 
    41       StringBuilder s = new StringBuilder();
    42       s.Append(identifier.ToString());
    43       for (int i = 0; i < extensions.Length; i++) {
    44         s.Append(Separator);
    45         s.Append(extensions[i]);
    46       }
    47       this.identifier = s.ToString();
    48     }
    49 
    50     public override string ToString() {
    51       return identifier;
    52     }
    53 
    54     public override bool Equals(Object obj) {
    55       if (obj == null)
    56         return false;
    57 
    58       Identifier id = obj as Identifier;
    59       if (id == null)
    60         return false;
    61 
    62       return (identifier == id.identifier);
    63     }
    64 
    65     public override int GetHashCode() {
    66       return identifier.GetHashCode();
    67     }
    68 
    69     public int CompareTo(Identifier other) {
    70       if (other == null)
    71         return 1;
    72       else 
    73         return string.Compare(this.identifier, other.identifier, 
    74           StringComparison.Ordinal);
    75     }
    76 
    77     public static bool operator ==(Identifier id1, Identifier id2) {
    78       if (id1.Equals(null))
    79         return id2.Equals(null);
    80       else
    81         return id1.Equals(id2);
    82     }
    83 
    84     public static bool operator !=(Identifier id1, Identifier id2) {
    85       return !(id1 == id2);
    86     }
    87 
    88     public static bool operator <(Identifier id1, Identifier id2) {
    89       if (id1 == null)
    90         return id2 != null;
    91       else 
    92         return (id1.CompareTo(id2) < 0);
    93     }
    94 
    95     public static bool operator >(Identifier id1, Identifier id2) {
    96       if (id1 == null)
    97         return false;
    98       else 
    99         return (id1.CompareTo(id2) > 0);
   100     }  
   101 
   102   }
   103 }