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