//******************************************************************************************************
// SignalReference.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 05/05/2009 - J. Ritchie Carroll
// Generated original version of source code
// 12/17/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.Diagnostics.CodeAnalysis;
// ReSharper disable NonReadonlyMemberInGetHashCode
namespace GSF.Units.EE
{
///
/// Represents a signal that can be referenced by its constituent components.
///
public struct SignalReference : IEquatable, IComparable
{
#region [ Members ]
// Fields
///
/// Gets or sets the acronym of this .
///
public string Acronym;
///
/// Gets or sets the signal index of this .
///
public int Index;
///
/// Gets or sets the of this .
///
public SignalKind Kind;
///
/// Gets or sets the cell index, if applicable, of this .
///
public int CellIndex;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
/// representation of this .
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", Justification = "Failed int.TryParse out value of zero is desirable, bool result is not needed.")]
public SignalReference(string signal)
{
// Signal reference may contain multiple dashes, we're interested in the last one
int splitIndex = signal.LastIndexOf('-');
// Assign default values to fields
Index = 0;
CellIndex = 0;
if (splitIndex > -1)
{
string signalType = signal.Substring(splitIndex + 1).Trim().ToUpper();
Acronym = signal.Substring(0, splitIndex).Trim().ToUpper();
// If the length of the signal type acronym is greater than 2, then this
// is an indexed signal type (e.g., CORDOVA-PA2)
if (signalType.Length > 2)
{
Kind = signalType.Substring(0, 2).ParseSignalKind();
if (Kind != SignalKind.Unknown)
int.TryParse(signalType.Substring(2), out Index);
}
else
Kind = signalType.ParseSignalKind();
}
else
{
// This represents an error - best we can do is assume entire string is the acronym
Acronym = signal.Trim().ToUpper();
Kind = SignalKind.Unknown;
}
}
#endregion
#region [ Methods ]
///
/// Returns a that represents the current .
///
/// A that represents the current .
public override string ToString() =>
ToString(Acronym, Kind, Index);
///
/// Returns the hash code for this .
///
/// A 32-bit signed integer hash code.
public override int GetHashCode() =>
ToString(Acronym, Kind, Index).GetHashCode();
///
/// Indicates whether the current object is equal to another object of the same type.
///
/// An object to compare with this object.
/// true if the current object is equal to the other parameter; otherwise, false.
/// Object is not a .
public override bool Equals(object obj) =>
obj is SignalReference signalReference && Equals(signalReference);
///
/// Indicates whether the current object is equal to another object of the same type.
///
/// An object to compare with this object.
/// true if the current object is equal to the other parameter; otherwise, false.
public bool Equals(SignalReference other) =>
string.Compare(Acronym, other.Acronym, StringComparison.OrdinalIgnoreCase) == 0 && Kind == other.Kind && Index == other.Index;
///
/// Compares the current object with another object of the same type.
///
/// Another to compare with this .
/// A 32-bit signed integer that indicates the relative order of the objects being compared.
public int CompareTo(SignalReference other)
{
int acronymCompare = string.Compare(Acronym, other.Acronym, StringComparison.OrdinalIgnoreCase);
if (acronymCompare == 0)
{
int signalTypeCompare = Kind < other.Kind ? -1 : (Kind > other.Kind ? 1 : 0);
return signalTypeCompare == 0 ? Index.CompareTo(other.Index) : signalTypeCompare;
}
return acronymCompare;
}
///
/// Compares the current object with another object of the same type.
///
/// An object to compare with this .
/// A 32-bit signed integer that indicates the relative order of the objects being compared.
/// Object is not a .
public int CompareTo(object obj) =>
obj is SignalReference signalReference ?
CompareTo(signalReference) :
throw new ArgumentException("object is not a SignalReference");
#endregion
#region [ Operators ]
///
/// Compares two values for equality.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator ==(SignalReference signal1, SignalReference signal2) =>
signal1.Equals(signal2);
///
/// Compares two values for inequality.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator !=(SignalReference signal1, SignalReference signal2) =>
!signal1.Equals(signal2);
///
/// Returns true if left value is greater than right value.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator >(SignalReference signal1, SignalReference signal2) =>
signal1.CompareTo(signal2) > 0;
///
/// Returns true if left value is greater than or equal to right value.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator >=(SignalReference signal1, SignalReference signal2) =>
signal1.CompareTo(signal2) >= 0;
///
/// Returns true if left value is less than right value.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator <(SignalReference signal1, SignalReference signal2) =>
signal1.CompareTo(signal2) < 0;
///
/// Returns true if left value is less than or equal to right value.
///
/// A left hand operand.
/// A right hand operand.
/// A boolean representing the result.
public static bool operator <=(SignalReference signal1, SignalReference signal2) =>
signal1.CompareTo(signal2) <= 0;
#endregion
#region [ Static ]
// Static Methods
///
/// Returns a that represents the specified and .
///
/// Acronym portion of the desired representation.
/// portion of the desired representation.
/// A that represents the specified and .
public static string ToString(string acronym, SignalKind type) =>
ToString(acronym, type, 0);
///
/// Returns a that represents the specified , and .
///
/// Acronym portion of the desired representation.
/// portion of the desired representation.
/// Index of portion of the desired representation.
/// A that represents the specified , and .
public static string ToString(string acronym, SignalKind type, int index) =>
$"{acronym}-{type.GetAcronym()}{(index > 0 ? $"{index}" : "")}";
#endregion
}
}