2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Diagnostics;
54 #endregion Enumerations
56 #region Public Static Singletons
91 #endregion Public Static Singletons
93 #region Private Properties
95 private readonly
string[] _perspectiveSegments;
98 private readonly Lazy<string> _stringRepresentation;
103 private readonly Lazy<HashSet<string>> _uniqueElements;
106 private readonly Lazy<int> _hashCode;
108 #region Static Caches
111 private static readonly IEqualityComparer<HashSet<string>> SetComparer = HashSet<string>.CreateSetComparer();
120 private static readonly ConcurrentDictionary<string, Perspective> CachedComplexPerspectivesByString =
121 new ConcurrentDictionary<string, Perspective>(StringComparer.OrdinalIgnoreCase);
122 #endregion Static Caches
123 #endregion Private Properties
131 if (perspectiveSegments ==
null || perspectiveSegments.Length == 0)
132 throw new ArgumentException(
"At least one perspective value is required to instantiate a perspective.");
133 _perspectiveSegments = perspectiveSegments;
139 _stringRepresentation =
new Lazy<string>(BuildStringRepresentation);
140 _uniqueElements =
new Lazy<HashSet<string>>(BuildElementHashSet);
141 _hashCode =
new Lazy<int>(ComputeHashCode);
149 this(perspectiveValues.Select(
Enum<
Base>.GetName).ToArray())
155 #region Obsolete Public Constructors
161 [Obsolete(
"Use the static Perspective.FromString factory method so that cached immutable Perspectives can be returned.")]
162 public Perspective(
string perspective) : this(SplitPerspectiveString(perspective).ToArray()) { }
172 [Obsolete(
"Use the static Perspective.FromEnums factory method. Perspectives are now built from any combination of Base enum values.")]
173 public Perspective(
Base perspective_base, IEnumerable<Option> options =
null) : this(new[] { perspective_base }
175 .Concat(options?.Select(o => o ==
Option.Premium ?
Base.Premium :
179 #endregion Obsolete Public Constructors
180 #endregion Constructors
182 #region Public Static Factories
188 perspectives !=
null && perspectives.Length > 0 ?
190 perspectives.Length == 1 ?
FromEnum(perspectives[0]) :
196 throw new ArgumentException(
"At least one perspective value is required to instantiate a perspective.");
223#pragma warning disable 618
225#pragma warning restore 618
227 Debug.WriteLine($
"The new perspective {perspective.ToString()} is missing " +
228 "from the enum to singleton converter. This has performance implications.");
241 FromEnums(
new[] { Base.NetLoss }.Concat(perspectives).ToArray());
248 return perspectiveString ==
null ? null :
256 CachedComplexPerspectivesByString.GetOrAdd(perspectiveString,
257 newPerspectiveString =>
new Perspective(SplitPerspectiveString(newPerspectiveString).ToArray()));
269 #endregion Public Static Factories
271 #region Private Methods
275 private string BuildStringRepresentation() =>
276 _perspectiveSegments.Length == 1 ? _perspectiveSegments[0] : String.Join(
",", _perspectiveSegments);
281 private HashSet<string> BuildElementHashSet() =>
282 new HashSet<string>(_perspectiveSegments, StringComparer.OrdinalIgnoreCase);
285 private int ComputeHashCode() => SetComparer.GetHashCode(_uniqueElements.Value);
289 private static readonly
char[] SplitChars = {
',',
'[',
']' };
292 private static IEnumerable<string> SplitPerspectiveString(
string perspectiveString) =>
293 perspectiveString.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries)
301 #endregion Private Methods
305 public override string ToString() => _stringRepresentation.Value;
317 if (ReferenceEquals(
this, other))
return true;
318 if (other ==
null)
return false;
320 return String.Equals(_stringRepresentation.Value, other._stringRepresentation.Value, StringComparison.Ordinal) ||
322 SetComparer.Equals(_uniqueElements.Value, other._uniqueElements.Value);
328 public override bool Equals(
object other) =>
329 ReferenceEquals(
this, other) ||
341 [Obsolete(
"Perspectives are now simply built from any combination of Base enum values.")]
353 [Obsolete(
"A cached string representation can simply be returned by calling ToString()")]
364 bool reinstatement_premium =
false,
bool reinstatement_brokerage =
false)
367 if (!premium && !reinstatement_premium && !reinstatement_brokerage)
370 List<Base> perspectives =
new List<Base> { Base.LossNetOfAggregateTerms };
371 if (premium) perspectives.Add(
Base.Premium);
372 if (reinstatement_premium) perspectives.Add(
Base.ReinstatementPremium);
373 if (reinstatement_brokerage) perspectives.Add(
Base.ReinstatementBrokerage);
Base class used by all types and resources.
The loss perspective determines what factors to include when computing a distribution.
static readonly Perspective ReinstatementBrokerage
Construct a distribution from the structure's net reinstatement brokerages.
static Perspective FromEnum(Base perspective)
Returns the appropriate singleton for the Base perspective given.
static readonly Perspective SlidingScaleCommission
Report on the structure's Fees.FixedExpense fees.
static string GetStringRepresentation(Perspective persp)
Generate a string representation of the specified perspective.
static readonly Perspective LossGross
Construct a distribution from the structure's gross losses.
static Perspective LossNetOf(params Base[] perspectives)
A convenience static method, similar to the other static singletons, which constructs a complex persp...
override string ToString()
Perspective string is just the class name.
static Perspective FromEnums(IEnumerable< Base > perspectives)
Construct a new Perspective that should include the specified Base perspective values.
override bool Equals(object other)
Tests object equality.
static readonly Perspective ProportionalExpense
Report on the structure's Fees.ProportionalExpense fees.
bool Equals(Perspective other)
Determine whether the two perspectives are equivalent.
Perspective(string perspective)
Construct a new perspective from a serialized perspective string.
static readonly Perspective FixedExpense
Report on the structure's Fees.FixedExpense fees.
static readonly Perspective NetLoss
Construct a distribution from the structure's net losses.
override int GetHashCode()
Serves as a hash function for a particular type.
static readonly Perspective LossNetOfAggregateTerms
The legacy perspective value indicating "net loss" - but this includes fee record types as well.
static readonly Perspective NoClaimsBonus
Report on the structure's Fees.FixedExpense fees.
Base
The set of available values and RecordTypes that can be used to construct a distribution or metrics r...
static Perspective FromString(string perspectiveString)
Implicit converter that allows automatically converting a string into a perspective,...
static Perspective LossNetOfAggregateTermsAnd(bool premium=false, bool reinstatement_premium=false, bool reinstatement_brokerage=false)
Constructs a legacy LossNetOfAggregateTerms[X,Y,Z] perspective, where you can specify which values to...
Option
The options to include in the base perspective distribution.
static readonly Perspective Premium
Report on the structure's premium only.
Perspective(Base perspective_base, IEnumerable< Option > options=null)
Constructs a legacy perspective string.
static readonly Perspective ReinstatementPremium
Construct a distribution from the structure's net reinstatement premiums.
static readonly Perspective ProfitCommission
Report on the structure's Fees.FixedExpense fees.
static Perspective FromEnums(params Base[] perspectives)
Construct a new Perspective that should include the specified Base perspective value or values.
Helper class for extending the functionality of the static Enumclass with compile-time-type-specific ...
static string GetName(T value)
Convert the enumeration value to a string.
static bool TryParse(string value, out T result)
Try to convert the string to an enumeration value.