Unit
using ForgeUnitsCLR;
using System;
using System.Collections.Generic;
namespace DynamoUnits
{
public class Unit
{
internal readonly Unit forgeUnit;
public string Name => forgeUnit.getName();
public string TypeId => forgeUnit.getTypeId();
public IEnumerable<Unit> ConvertibleUnits => Utilities.ConvertForgeUnitDictionaryToCollection(Utilities.ForgeUnitsEngine.getConvertibleUnits(TypeId));
public IEnumerable<Quantity> QuantitiesContainingUnit => Utilities.CovertForgeQuantityDictionaryToCollection(Utilities.ForgeUnitsEngine.getQuantitiesContainingUnit(TypeId));
internal Unit(Unit unit)
{
if (unit == null)
throw new ArgumentNullException();
forgeUnit = unit;
}
public static Unit ByTypeID(string typeId)
{
try {
return new Unit(Utilities.ForgeUnitsEngine.getUnit(typeId));
} catch (Exception) {
if (!Utilities.TryParseTypeId(typeId, out string typeName, out Version _) || !Utilities.GetAllLatestRegisteredUnitVersions().TryGetValue(typeName, out Version value))
throw;
return new Unit(Utilities.ForgeUnitsEngine.getUnit(typeName + "-" + value.ToString()));
}
}
public static bool AreUnitsConvertible(Unit fromUnit, Unit toUnit)
{
return Utilities.ForgeUnitsEngine.areUnitsConvertible(fromUnit.TypeId, toUnit.TypeId);
}
public override string ToString()
{
return "Unit(Name = " + Name + ")";
}
public override int GetHashCode()
{
return (Name ?? string.Empty).GetHashCode() ^ (TypeId ?? string.Empty).GetHashCode();
}
public override bool Equals(object obj)
{
return EqualsImpl(obj as Unit);
}
internal bool EqualsImpl(Unit u)
{
if ((object)u == null)
return false;
if ((object)this == u)
return true;
if (GetType() != u.GetType())
return false;
if (Name == u.Name)
return TypeId == u.TypeId;
return false;
}
public static bool operator ==(Unit lhs, Unit rhs)
{
if ((object)lhs == null) {
if ((object)rhs == null)
return true;
return false;
}
return lhs.EqualsImpl(rhs);
}
public static bool operator !=(Unit lhs, Unit rhs)
{
return !(lhs == rhs);
}
}
}