C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
MonetaryUnit.cs
Go to the documentation of this file.
1using System;
2using System.Runtime.Serialization;
4
5// ReSharper disable once CheckNamespace (Type is common enough to be on root namespace)
6// ReSharper disable NonReadonlyMemberInGetHashCode
7// We only compute the hashCode the first time it's needed, then never modify it.
8// Furthermore, the properties of a monetary unit never be changed after initialization,
9// they're only modifiable for deserialization purposes.
10namespace AnalyzeRe
11{
13 public class MonetaryUnit : APIType, IEquatable<MonetaryUnit>
14 {
15 #region Private Properties
19 private int? _hashCode;
20
21 // Backing fields for public properties
22 private string _currency;
23 private double _value;
24 private DateTime? _valueDate;
25 private double? _rate;
26 private string _rateCurrency;
27 #endregion Private Properties
28
29 #region Properties
32 [DataMember(Order = 2)]
33 [NotNull]
34 [NotEmpty]
35 public string currency
36 {
37 get => _currency;
38 set
39 {
40 _currency = value;
41 _hashCode = null;
42 }
43 }
44
46 [DataMember(Order = 3)]
47 public double value
48 {
49 get => _value;
50 set
51 {
52 _value = value;
53 _hashCode = null;
54 }
55 }
56
60 [DataMember(Order = 4)]
61 public DateTime? value_date
62 {
63 get => _valueDate;
64 set
65 {
66 _valueDate = value;
67 _hashCode = null;
68 }
69 }
70
75 [DataMember(Order = 5)]
76 [GreaterThan(0, canEqual: true)]
77 public double? rate
78 {
79 get => _rate;
80 set
81 {
82 _rate = value;
83 _hashCode = null;
84 }
85 }
86
89 [DataMember(Order = 6)]
90 [NotEmpty]
91 public string rate_currency
92 {
93 get => _rateCurrency;
94 set
95 {
96 _rateCurrency = value;
97 _hashCode = null;
98 }
99 }
100 #endregion Properties
101
102 #region Constructors
104 [Obsolete("Constructor only exists temporarily for the deserializer. All monetary units " +
105 "should be created using one of the other constructors.")]
107 {
108 }
109
113 public MonetaryUnit(double value, string currency)
114 {
115 _currency = currency;
116 _value = value;
117 }
118
125 public MonetaryUnit(double value, string currency, DateTime value_date)
126 : this(value, currency)
127 {
128 _valueDate = value_date;
129 }
130
140 public MonetaryUnit(double value, string currency, double rate, string rate_currency)
141 : this(value, currency)
142 {
143 _rate = rate;
144 _rateCurrency = rate_currency;
145 }
146 #endregion Constructors
147
148 #region Object Overrides
156 public override string ToString()
157 {
158 double absValue = Math.Abs(value);
159 string strRepresentation = 1E18 >= absValue && absValue >= 1000 ?
160 value.ToString("N2") : value.ToString("G");
161 if (currency != null)
162 strRepresentation = $"{currency} {strRepresentation}";
163 if (value_date != null)
164 // "o" is a DateTime round-trip representation which preserves the original time zone.
165 strRepresentation += " FX rate fixed to " + value_date.Value.ToString("o");
166 if (_rate != null)
167 strRepresentation += $" FX rate fixed to {rate_currency} {_rate.Value:G}";
168 return strRepresentation;
169 }
170
173 public bool Equals(MonetaryUnit other) => ReferenceEquals(this, other) ||
174 other != null &&
175 Equals(_currency, other.currency) &&
176 Equals(_value, other.value) &&
177 Equals(_valueDate, other.value_date) &&
178 Equals(_rate, other.rate) &&
179 Equals(_rateCurrency, other.rate_currency);
180
184 public override bool Equals(object obj) =>
185 obj is MonetaryUnit objMonetaryUnit && Equals(objMonetaryUnit);
186
189 public override int GetHashCode()
190 {
191 if (!_hashCode.HasValue)
192 _hashCode = new { _currency, _value, _valueDate, _rate, _rateCurrency }.GetHashCode();
193 return _hashCode.Value;
194 }
195 #endregion Object Overrides
196 }
197}
Base class used by all types and resources.
Definition APIType.cs:8
Representation of a monetary value with a currency.
DateTime? value_date
The date for which to retrieve the exchange rate whenever using this monetary unit and a currency con...
string currency
3-letter currency code based on ISO 4217 (see: https://en.wikipedia.org/wiki/ISO_4217)
MonetaryUnit(double value, string currency)
Construct a new monetary unit.
double? rate
The fixed rate to use whenever using this monetary unit and a currency conversion is required....
MonetaryUnit()
Construct an empty monetary unit.
bool Equals(MonetaryUnit other)
Determine if this MonetaryUnit is equivalent to another.
double value
The value of the monetary unit.
override bool Equals(object obj)
Determine if this MonetaryUnit is equivalent to another object.
string rate_currency
3-letter currency code that indicates the currency that the 'rate' parameter is the rate for....
override string ToString()
String representation of this MonetaryUnit's currency and value, and of its fixed exchange rate prope...
override int GetHashCode()
Get the HashCode for this MonetaryUnit.
MonetaryUnit(double value, string currency, DateTime value_date)
Construct a new monetary unit with a preferred exchange rate conversion date.
MonetaryUnit(double value, string currency, double rate, string rate_currency)
Construct a new monetary unit with a fixed exchange rate conversion.