RateSeriesBuilder Class
Definition
- Namespace
- Bodu.Financial.ExchangeRates
- Assembly
- Bodu.Financial.dll
- Package
- Bodu.Financial 1.0.0
- Source
- RateSeriesBuilder.cs
Provides a mutable construction and editing surface for an RateSeries, maintaining strictly ascending unique observation dates and strictly positive rates while supporting single-observation edits and bulk import.
public sealed class RateSeriesBuilder
- Inheritance
-
RateSeriesBuilder
- Inherited Members
- Extension Methods
Remarks
The builder is the natural entry point for assembling rate observations imperatively (manual data entry, streaming import, merge with prior history). After mutations complete, call ToSeries() to produce an immutable RateSeries snapshot for use in production lookup. Further mutations on the builder do not affect previously produced snapshots, and vice versa.
Instances are not thread-safe; concurrent mutation requires external synchronisation.
using Bodu.Financial.Currencies;
using Bodu.Financial.ExchangeRates;
var builder = new RateSeriesBuilder(new CurrencyPair(CurrencyCode.AUD, CurrencyCode.USD), "Treasury");
builder.Add(new DateOnly(2024, 3, 14), 0.6604m);
builder.Add(new DateOnly(2024, 3, 15), 0.6580m);
builder.Upsert(new DateOnly(2024, 3, 15), 0.6582m); // corrects the existing observation
// Freeze the observations; later builder mutations do not affect the snapshot.
RateSeries series = builder.ToSeries();
Constructors
RateSeriesBuilder(CurrencyPair, string)
Initializes a new instance of the RateSeriesBuilder class with no observations.
public RateSeriesBuilder(CurrencyPair pair, string provider)
Parameters
pairCurrencyPairThe currency pair this builder is editing.
providerstringThe non-empty identifier of the publishing source.
Exceptions
- ArgumentNullException
Thrown if
provideris null.- ArgumentException
Thrown if
provideris empty or white-space.
RateSeriesBuilder(RateSeries)
Initializes a new instance of the RateSeriesBuilder class seeded from the supplied immutable series.
public RateSeriesBuilder(RateSeries series)
Parameters
seriesRateSeriesThe series to copy observations from.
Exceptions
- ArgumentNullException
Thrown if
seriesis null.
Properties
Count
Gets the number of observations currently held.
public int Count { get; }
Property Value
- int
A non-negative count.
FetchedAtUtc
Gets or sets the UTC instant at which the load that produced this series downloaded its source data, or null when not tracked.
public DateTimeOffset? FetchedAtUtc { get; set; }
Property Value
- DateTimeOffset?
The fetch instant when known; otherwise null.
Remarks
The value is carried verbatim onto the RateSeries produced by ToSeries() and from there onto every ExchangeRate the series materializes. Editing the observation buffer does not change it; assign it explicitly to stamp a fresh load instant.
IsEmpty
Gets a value indicating whether the builder holds no observations.
public bool IsEmpty { get; }
Property Value
Pair
Gets the currency pair this builder is editing.
public CurrencyPair Pair { get; }
Property Value
- CurrencyPair
The series pair.
Provider
Gets the identifier of the source the builder represents.
public string Provider { get; }
Property Value
- string
A non-empty provider identifier.
Methods
Add(DateOnly, decimal)
Inserts a new observation; throws if an observation already exists for date.
public void Add(DateOnly date, decimal rate)
Parameters
Exceptions
- ArgumentOutOfRangeException
Thrown if
rateis zero or negative.- ArgumentException
Thrown if an observation already exists for
date.
AddRange(IEnumerable<RateObservation>)
Inserts a batch of observations, rejecting duplicate dates inside the batch and dates already present in the builder. On any validation failure the builder remains unchanged.
public void AddRange(IEnumerable<RateObservation> observations)
Parameters
observationsIEnumerable<RateObservation>The observations to insert.
Exceptions
- ArgumentNullException
Thrown if
observationsis null.- ArgumentOutOfRangeException
Thrown if any rate in
observationsis zero or negative.- ArgumentException
Thrown if the batch contains a duplicate date or a date already present in the builder.
AddRange(IEnumerable<(DateOnly Date, decimal Rate)>)
Tuple-shaped overload of AddRange(IEnumerable<RateObservation>) for import sources that hand the
builder raw (date, rate) pairs.
public void AddRange(IEnumerable<(DateOnly Date, decimal Rate)> rates)
Parameters
ratesIEnumerable<(DateOnly Date, decimal Rate)>The observations to insert.
Exceptions
- ArgumentNullException
Thrown if
ratesis null.- ArgumentOutOfRangeException
Thrown if any rate in
ratesis zero or negative.- ArgumentException
Thrown if the batch contains a duplicate date or a date already present in the builder.
ContainsDate(DateOnly)
Reports whether an observation exists for date.
public bool ContainsDate(DateOnly date)
Parameters
dateDateOnlyThe observation date to query.
Returns
GetObservations()
Enumerates the observations in strictly ascending date order.
public IEnumerable<RateObservation> GetObservations()
Returns
- IEnumerable<RateObservation>
A lazy sequence of RateObservation values.
Remove(DateOnly)
Removes the observation for date if present.
public bool Remove(DateOnly date)
Parameters
dateDateOnlyThe observation date to remove.
Returns
Set(DateOnly, decimal)
Updates the rate for an existing observation; throws if no observation exists for date.
public void Set(DateOnly date, decimal rate)
Parameters
Exceptions
- ArgumentOutOfRangeException
Thrown if
rateis zero or negative.- KeyNotFoundException
Thrown if no observation exists for
date.
ToSeries()
Produces an immutable RateSeries snapshot of the current observations.
public RateSeries ToSeries()
Returns
- RateSeries
A new RateSeries reflecting the builder's current state.
Exceptions
- InvalidOperationException
Thrown if the builder is empty; an immutable series must contain at least one observation.
TryAdd(DateOnly, decimal)
Attempts to insert a new observation; returns false if one already exists for
date.
public bool TryAdd(DateOnly date, decimal rate)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown if
rateis zero or negative.
TryGetRate(DateOnly, out decimal)
Attempts to retrieve the rate observed exactly on date.
public bool TryGetRate(DateOnly date, out decimal rate)
Parameters
dateDateOnlyThe observation date to query.
ratedecimalWhen this method returns true, the rate observed on
date; otherwise default.
Returns
TrySet(DateOnly, decimal)
Attempts to update the rate for an existing observation; returns false if none exists.
public bool TrySet(DateOnly date, decimal rate)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown if
rateis zero or negative.
Upsert(DateOnly, decimal)
Inserts a new observation or replaces the rate of an existing one for date.
public void Upsert(DateOnly date, decimal rate)
Parameters
Exceptions
- ArgumentOutOfRangeException
Thrown if
rateis zero or negative.
UpsertRange(IEnumerable<RateObservation>)
Merges a batch of observations into the builder: inserts new dates and replaces rates for dates already present. Rejects duplicate dates within the batch. On any validation failure the builder remains unchanged.
public void UpsertRange(IEnumerable<RateObservation> observations)
Parameters
observationsIEnumerable<RateObservation>The observations to merge.
Exceptions
- ArgumentNullException
Thrown if
observationsis null.- ArgumentOutOfRangeException
Thrown if any rate in
observationsis zero or negative.- ArgumentException
Thrown if the batch contains a duplicate date.
UpsertRange(IEnumerable<(DateOnly Date, decimal Rate)>)
Tuple-shaped overload of UpsertRange(IEnumerable<RateObservation>) for import sources that hand
the builder raw (date, rate) pairs.
public void UpsertRange(IEnumerable<(DateOnly Date, decimal Rate)> rates)
Parameters
ratesIEnumerable<(DateOnly Date, decimal Rate)>The observations to merge.
Exceptions
- ArgumentNullException
Thrown if
ratesis null.- ArgumentOutOfRangeException
Thrown if any rate in
ratesis zero or negative.- ArgumentException
Thrown if the batch contains a duplicate date.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |