Hardware/Identifier.cs
author moel.mich
Mon, 13 Sep 2010 22:55:25 +0000
changeset 184 3ad822f418cb
parent 182 4801e9eaf979
child 195 0ee888c485d5
permissions -rw-r--r--
Fixed some hardware sorting issue in the gadget. Hardware with the same type and name would be added to the same section.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Text;
    40 
    41 namespace OpenHardwareMonitor.Hardware {
    42   public class Identifier : IComparable<Identifier> {
    43     private string identifier;
    44 
    45     private static char SEPARATOR = '/';
    46 
    47     private static void CheckIdentifiers(string[] identifiers) {      
    48       foreach (string s in identifiers)
    49         if (s.Contains(" ") || s.Contains(SEPARATOR.ToString()))
    50           throw new ArgumentException("Invalid identifier");
    51     }
    52 
    53     public Identifier(params string[] identifiers) {
    54       CheckIdentifiers(identifiers);
    55 
    56       StringBuilder s = new StringBuilder();
    57       for (int i = 0; i < identifiers.Length; i++) {
    58         s.Append(SEPARATOR);
    59         s.Append(identifiers[i]);
    60       }
    61       this.identifier = s.ToString();
    62     }
    63 
    64     public Identifier(Identifier identifier, params string[] extensions) {
    65       CheckIdentifiers(extensions);
    66 
    67       StringBuilder s = new StringBuilder();
    68       s.Append(identifier.ToString());
    69       for (int i = 0; i < extensions.Length; i++) {
    70         s.Append(SEPARATOR);
    71         s.Append(extensions[i]);
    72       }
    73       this.identifier = s.ToString();
    74     }
    75 
    76     public override string ToString() {
    77       return identifier;
    78     }
    79 
    80     public override bool Equals(System.Object obj) {
    81       if (obj == null)
    82         return false;
    83 
    84       Identifier id = obj as Identifier;
    85       if (id == null)
    86         return false;
    87 
    88       return (identifier == id.identifier);
    89     }
    90 
    91     public override int GetHashCode() {
    92       return identifier.GetHashCode();
    93     }
    94 
    95     public int CompareTo(Identifier other) {
    96       if (other == null)
    97         return 1;
    98       else 
    99         return string.Compare(this.identifier, other.identifier, 
   100           StringComparison.Ordinal);
   101     }
   102 
   103     public static bool operator ==(Identifier id1, Identifier id2) {
   104       if (id1.Equals(null))
   105         return id2.Equals(null);
   106       else
   107         return id1.Equals(id2);
   108     }
   109 
   110     public static bool operator !=(Identifier id1, Identifier id2) {
   111       return !(id1 == id2);
   112     }
   113 
   114     public static bool operator <(Identifier id1, Identifier id2) {
   115       if (id1 == null)
   116         return id2 != null;
   117       else 
   118         return (id1.CompareTo(id2) < 0);
   119     }
   120 
   121     public static bool operator >(Identifier id1, Identifier id2) {
   122       if (id1 == null)
   123         return false;
   124       else 
   125         return (id1.CompareTo(id2) > 0);
   126     }  
   127 
   128   }
   129 }