BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bbldc_terminateddaterangedaycountadapter.h
Go to the documentation of this file.
1/// @file bbldc_terminateddaterangedaycountadapter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bbldc_terminateddaterangedaycountadapter.h -*-C++-*-
8#ifndef INCLUDED_BBLDC_TERMINATEDDATERANGEDAYCOUNTADAPTER
9#define INCLUDED_BBLDC_TERMINATEDDATERANGEDAYCOUNTADAPTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bbldc_terminateddaterangedaycountadapter bbldc_terminateddaterangedaycountadapter
15/// @brief Provide a parameterized day-count convention implementation.
16/// @addtogroup bbl
17/// @{
18/// @addtogroup bbldc
19/// @{
20/// @addtogroup bbldc_terminateddaterangedaycountadapter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bbldc_terminateddaterangedaycountadapter-purpose"> Purpose</a>
25/// * <a href="#bbldc_terminateddaterangedaycountadapter-classes"> Classes </a>
26/// * <a href="#bbldc_terminateddaterangedaycountadapter-description"> Description </a>
27/// * <a href="#bbldc_terminateddaterangedaycountadapter-usage"> Usage </a>
28/// * <a href="#bbldc_terminateddaterangedaycountadapter-example-1-adapting-bbldc-terminatedisda30360eom"> Example 1: Adapting bbldc::TerminatedIsda30360Eom </a>
29///
30/// # Purpose {#bbldc_terminateddaterangedaycountadapter-purpose}
31/// Provide a parameterized day-count convention implementation.
32///
33/// # Classes {#bbldc_terminateddaterangedaycountadapter-classes}
34///
35/// - bbldc::TerminatedDateRangeDayCountAdapter:
36/// - `bbldc::DateRangeDayCount` adapter
37///
38/// # Description {#bbldc_terminateddaterangedaycountadapter-description}
39/// This component provides a parameterized (template)
40/// implementation, `bbldc::TerminatedDateRangeDayCountAdapter`, of the
41/// `bbldc::DateRangeDayCount` protocol that allows for special handling of a
42/// termination date (e.g., maturity date). The template argument can be any
43/// type supporting the following two class methods.
44/// @code
45/// int daysDiff(const bdlt::Date& beginDate,
46/// const bdlt::Date& endDate,
47/// const bdlt::Date& terminationDate);
48///
49/// double yearsDiff(const bdlt::Date& beginDate,
50/// const bdlt::Date& endDate,
51/// const bdlt::Date& terminationDate);
52/// @endcode
53/// The template class `bbldc::TerminatedDateRangeDayCountAdapter` provides
54/// convenient support for run-time polymorphic choice of day-count conventions
55/// (via conventional use of a base-class pointer or reference) without having
56/// to implement each derived type explicitly. In this sense,
57/// `bbldc::TerminatedDateRangeDayCountAdapter` adapts the various concrete
58/// "terminated" day-count convention classes (e.g.,
59/// `bbldc::TerminatedIsda30360Eom`) to a run-time binding mechanism.
60///
61/// The `bbldc::DateRangeDayCount` protocol requires two methods, `firstDate`
62/// and `lastDate`, that define a date range for which calculations are valid,
63/// to reflect the valid range of, say, a calendar required for the
64/// computations. For "terminated" day-count implementations, the valid date
65/// range is identical to the range of `bdlt::Date`.
66///
67/// ## Usage {#bbldc_terminateddaterangedaycountadapter-usage}
68///
69///
70/// This section illustrates intended use of this component.
71///
72/// ### Example 1: Adapting bbldc::TerminatedIsda30360Eom {#bbldc_terminateddaterangedaycountadapter-example-1-adapting-bbldc-terminatedisda30360eom}
73///
74///
75/// This example shows the procedure for using
76/// `bbldc::TerminatedDateRangeDayCountAdapter` to adapt the
77/// `bbldc::TerminatedIsda30360Eom` day-count convention to the
78/// `bbldc::DateRangeDayCount` protocol, and then the use of the day-count
79/// methods.
80///
81/// First, we define an instance of the adapted `bbldc::TerminatedIsda30360Eom`
82/// day-count convention and obtain a reference to the
83/// `bbldc::DateRangeDayCount`:
84/// @code
85/// const bbldc::TerminatedDateRangeDayCountAdapter<
86/// bbldc::TerminatedIsda30360Eom>
87/// myDcc(bdlt::Date(2004, 2, 29));
88///
89/// const bbldc::DateRangeDayCount& dcc = myDcc;
90/// @endcode
91/// Then, create two `bdlt::Date` variables, `d1` and `d2`, with which to use
92/// the day-count convention methods:
93/// @code
94/// const bdlt::Date d1(2003, 10, 18);
95/// const bdlt::Date d2(2003, 12, 31);
96/// @endcode
97/// Now, use the base-class reference to compute the day count between the two
98/// dates:
99/// @code
100/// const int daysDiff = dcc.daysDiff(d1, d2);
101/// assert(72 == daysDiff);
102/// @endcode
103/// Finally, use the base-class reference to compute the year fraction between
104/// the two dates:
105/// @code
106/// const double yearsDiff = dcc.yearsDiff(d1, d2);
107/// // Need fuzzy comparison since 'yearsDiff' is a 'double'.
108/// assert(0.1999 < yearsDiff && 0.2001 > yearsDiff);
109/// @endcode
110/// @}
111/** @} */
112/** @} */
113
114/** @addtogroup bbl
115 * @{
116 */
117/** @addtogroup bbldc
118 * @{
119 */
120/** @addtogroup bbldc_terminateddaterangedaycountadapter
121 * @{
122 */
123
124#include <bblscm_version.h>
125
127
128#include <bdlt_date.h>
129
130#include <bsls_keyword.h>
131
132
133namespace bbldc {
134
135 // ========================================
136 // class TerminatedDateRangeDayCountAdapter
137 // ========================================
138
139/// This `class` provides an "adapter" from the specified `CONVENTION` to
140/// the `bbldc::DateRangeDayCount` protocol that can be used for determining
141/// values based on dates according to the day-count `CONVENTION`.
142///
143/// See @ref bbldc_terminateddaterangedaycountadapter
144template <class CONVENTION>
146
147 // DATA
148 bdlt::Date d_terminationDate; // termination date used in all calculations
149
150 private:
151 // NOT IMPLEMENTED
156
157 public:
158 // CREATORS
159
160 /// Create a day-count adapter that uses the specified `terminationDate`
161 /// during.
162 TerminatedDateRangeDayCountAdapter(const bdlt::Date& terminationDate);
163
164 /// Destroy this object.
166
167 // ACCESSORS
168 int daysDiff(const bdlt::Date& beginDate, const bdlt::Date& endDate) const
170 // Return the (signed) number of days between the specified 'beginDate'
171 // and 'endDate' as per the 'CONVENTION' template policy. If
172 // 'beginDate <= endDate', then the result is non-negative. Note that
173 // reversing the order of 'beginDate' and 'endDate' negates the result.
174
175 /// Return a reference providing non-modifiable access to a `bdlt::Date`
176 /// with the value 1/1/1. Note that this value is the earliest date in
177 /// the valid range of the adapted day-count convention.
179
180 /// Return a reference providing non-modifiable access to a `bdlt::Date`
181 /// with the value 9999/12/31. Note that this value is the latest date
182 /// in the valid range of the adapted day-count convention.
184
185 /// Return the (signed fractional) number of years between the specified
186 /// `beginDate` and `endDate` as per the `CONVENTION` template policy.
187 /// If `beginDate <= endDate`, then the result is non-negative. Note
188 /// that reversing the order of `beginDate` and `endDate` negates the
189 /// result; specifically,
190 /// `|yearsDiff(b, e) + yearsDiff(e, b)| <= 1.0e-15` for all dates `b`
191 /// and `e`.
192 double yearsDiff(const bdlt::Date& beginDate,
193 const bdlt::Date& endDate) const BSLS_KEYWORD_OVERRIDE;
194};
195
196// ============================================================================
197// INLINE DEFINITIONS
198// ============================================================================
199
200 // ----------------------------------------
201 // class TerminatedDateRangeDayCountAdapter
202 // ----------------------------------------
203
204// CREATORS
205template <class CONVENTION>
206inline
209: d_terminationDate(terminationDate)
210{
211}
212
213template <class CONVENTION>
214inline
219
220// ACCESSORS
221template <class CONVENTION>
222inline
224 const bdlt::Date& beginDate,
225 const bdlt::Date& endDate) const
226{
227 return CONVENTION::daysDiff(beginDate, endDate, d_terminationDate);
228}
229
230template <class CONVENTION>
231inline
233 firstDate() const
234{
235 static bdlt::Date firstDate(1, 1, 1);
236 return firstDate;
237}
238
239template <class CONVENTION>
240inline
242 lastDate() const
243{
244 static bdlt::Date lastDate(9999, 12, 31);
245 return lastDate;
246}
247
248template <class CONVENTION>
249inline
251 const bdlt::Date& beginDate,
252 const bdlt::Date& endDate) const
253{
254 return CONVENTION::yearsDiff(beginDate, endDate, d_terminationDate);
255}
256
257} // close package namespace
258
259
260#endif
261
262// ----------------------------------------------------------------------------
263// Copyright 2016 Bloomberg Finance L.P.
264//
265// Licensed under the Apache License, Version 2.0 (the "License");
266// you may not use this file except in compliance with the License.
267// You may obtain a copy of the License at
268//
269// http://www.apache.org/licenses/LICENSE-2.0
270//
271// Unless required by applicable law or agreed to in writing, software
272// distributed under the License is distributed on an "AS IS" BASIS,
273// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
274// See the License for the specific language governing permissions and
275// limitations under the License.
276// ----------------------------- END-OF-FILE ----------------------------------
277
278/** @} */
279/** @} */
280/** @} */
Definition bbldc_daterangedaycount.h:191
Definition bbldc_terminateddaterangedaycountadapter.h:145
const bdlt::Date & firstDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_terminateddaterangedaycountadapter.h:233
double yearsDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_terminateddaterangedaycountadapter.h:250
~TerminatedDateRangeDayCountAdapter() BSLS_KEYWORD_OVERRIDE
Destroy this object.
Definition bbldc_terminateddaterangedaycountadapter.h:216
int daysDiff(const bdlt::Date &beginDate, const bdlt::Date &endDate) const BSLS_KEYWORD_OVERRIDE
Definition bbldc_terminateddaterangedaycountadapter.h:223
const bdlt::Date & lastDate() const BSLS_KEYWORD_OVERRIDE
Definition bbldc_terminateddaterangedaycountadapter.h:242
Definition bdlt_date.h:294
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_KEYWORD_OVERRIDE
Definition bsls_keyword.h:653
Definition bbldc_basicactual360.h:107