Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PersistableQuantity.cs
1using System;
2using System.Text;
3using Waher.Content;
8
10{
14 public class PersistableQuantity : IPhysicalQuantity, IComparable
15 {
20 {
21 }
22
29 public PersistableQuantity(double Value, string Unit, byte NrDecimals)
30 {
31 this.Value = Value;
32 this.Unit = Unit;
33 this.NrDecimals = NrDecimals;
34 }
35
39 [ShortName("v")]
40 public double Value { get; set; }
41
45 [ShortName("d")]
46 [DefaultValueStringEmpty]
47 public string Unit { get; set; }
48
52 [DefaultValue(0)]
53 public byte NrDecimals { get; set; }
54
56 public override bool Equals(object obj)
57 {
58 return obj is PersistableQuantity Q &&
59 this.Value == Q.Value &&
60 this.Unit == Q.Unit &&
61 this.NrDecimals == Q.NrDecimals;
62 }
63
65 public override int GetHashCode()
66 {
67 int Result = this.Value.GetHashCode();
68 Result ^= Result << 5 ^ (this.Unit?.GetHashCode() ?? 0);
69 Result ^= Result << 5 ^ this.NrDecimals.GetHashCode();
70
71 return Result;
72 }
73
75 public override string ToString()
76 {
77 StringBuilder sb = new StringBuilder();
78
79 sb.Append(CommonTypes.Encode(this.Value, this.NrDecimals));
80
81 if (!string.IsNullOrEmpty(this.Unit))
82 {
83 sb.Append(' ');
84 sb.Append(this.Unit);
85 }
86
87 return sb.ToString();
88 }
89
95 {
96 return new PhysicalQuantity(this.Value, new Unit(this.Unit));
97 }
98
102 public int CompareTo(object obj)
103 {
104 if (!(obj is IPhysicalQuantity PQ))
105 throw new ScriptException("Values not comparable.");
106
107 return this.ToPhysicalQuantity().CompareTo(PQ.ToPhysicalQuantity());
108 }
109 }
110}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
Base class for script exceptions.
int CompareTo(object obj)
IComparable.CompareTo
Represents a unit.
Definition: Unit.cs:15
override int GetHashCode()
Definition: Unit.cs:506
int CompareTo(object obj)
IComparable.CompareTo
PersistableQuantity(double Value, string Unit, byte NrDecimals)
Persistable quantity object.
PersistableQuantity()
Persistable quantity object.
PhysicalQuantity ToPhysicalQuantity()
Converts underlying object to a physical quantity.
Interface for objects that can be represented as a physical quantity.