/src/quantlib/ql/time/calendars/jointcalendar.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003 RiskMap srl |
5 | | Copyright (C) 2007 StatPro Italia srl |
6 | | Copyright (C) 2020 Piotr Siejda |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <http://quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | #include <ql/errors.hpp> |
23 | | #include <ql/time/calendars/jointcalendar.hpp> |
24 | | #include <sstream> |
25 | | #include <utility> |
26 | | |
27 | | namespace QuantLib { |
28 | | |
29 | | JointCalendar::Impl::Impl(const Calendar& c1, |
30 | | const Calendar& c2, |
31 | | JointCalendarRule r) |
32 | 0 | : rule_(r), calendars_(2) { |
33 | 0 | calendars_[0] = c1; |
34 | 0 | calendars_[1] = c2; |
35 | 0 | } |
36 | | |
37 | | |
38 | | JointCalendar::Impl::Impl(const Calendar& c1, |
39 | | const Calendar& c2, |
40 | | const Calendar& c3, |
41 | | JointCalendarRule r) |
42 | 0 | : rule_(r), calendars_(3) { |
43 | 0 | calendars_[0] = c1; |
44 | 0 | calendars_[1] = c2; |
45 | 0 | calendars_[2] = c3; |
46 | 0 | } |
47 | | |
48 | | JointCalendar::Impl::Impl(const Calendar& c1, |
49 | | const Calendar& c2, |
50 | | const Calendar& c3, |
51 | | const Calendar& c4, |
52 | | JointCalendarRule r) |
53 | 0 | : rule_(r), calendars_(4) { |
54 | 0 | calendars_[0] = c1; |
55 | 0 | calendars_[1] = c2; |
56 | 0 | calendars_[2] = c3; |
57 | 0 | calendars_[3] = c4; |
58 | 0 | } |
59 | | |
60 | | JointCalendar::Impl::Impl(std::vector<Calendar> cv, JointCalendarRule r) |
61 | 0 | : rule_(r), calendars_(std::move(cv)) {} |
62 | | |
63 | 0 | std::string JointCalendar::Impl::name() const { |
64 | 0 | std::ostringstream out; |
65 | 0 | switch (rule_) { |
66 | 0 | case JoinHolidays: |
67 | 0 | out << "JoinHolidays("; |
68 | 0 | break; |
69 | 0 | case JoinBusinessDays: |
70 | 0 | out << "JoinBusinessDays("; |
71 | 0 | break; |
72 | 0 | default: |
73 | 0 | QL_FAIL("unknown joint calendar rule"); |
74 | 0 | } |
75 | 0 | out << calendars_.front().name(); |
76 | 0 | std::vector<Calendar>::const_iterator i; |
77 | 0 | for (i=calendars_.begin()+1; i!=calendars_.end(); ++i) |
78 | 0 | out << ", " << i->name(); |
79 | 0 | out << ")"; |
80 | 0 | return out.str(); |
81 | 0 | } |
82 | | |
83 | 0 | bool JointCalendar::Impl::isWeekend(Weekday w) const { |
84 | 0 | std::vector<Calendar>::const_iterator i; |
85 | 0 | switch (rule_) { |
86 | 0 | case JoinHolidays: |
87 | 0 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
88 | 0 | if (i->isWeekend(w)) |
89 | 0 | return true; |
90 | 0 | } |
91 | 0 | return false; |
92 | 0 | case JoinBusinessDays: |
93 | 0 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
94 | 0 | if (!i->isWeekend(w)) |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | return true; |
98 | 0 | default: |
99 | 0 | QL_FAIL("unknown joint calendar rule"); |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | bool JointCalendar::Impl::isBusinessDay(const Date& date) const { |
104 | 0 | std::vector<Calendar>::const_iterator i; |
105 | 0 | switch (rule_) { |
106 | 0 | case JoinHolidays: |
107 | 0 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
108 | 0 | if (i->isHoliday(date)) |
109 | 0 | return false; |
110 | 0 | } |
111 | 0 | return true; |
112 | 0 | case JoinBusinessDays: |
113 | 0 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
114 | 0 | if (i->isBusinessDay(date)) |
115 | 0 | return true; |
116 | 0 | } |
117 | 0 | return false; |
118 | 0 | default: |
119 | 0 | QL_FAIL("unknown joint calendar rule"); |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | |
124 | | JointCalendar::JointCalendar(const Calendar& c1, |
125 | | const Calendar& c2, |
126 | 0 | JointCalendarRule r) { |
127 | 0 | impl_ = ext::shared_ptr<Calendar::Impl>( |
128 | 0 | new JointCalendar::Impl(c1,c2,r)); |
129 | 0 | } |
130 | | |
131 | | JointCalendar::JointCalendar(const Calendar& c1, |
132 | | const Calendar& c2, |
133 | | const Calendar& c3, |
134 | 0 | JointCalendarRule r) { |
135 | 0 | impl_ = ext::shared_ptr<Calendar::Impl>( |
136 | 0 | new JointCalendar::Impl(c1,c2,c3,r)); |
137 | 0 | } |
138 | | |
139 | | JointCalendar::JointCalendar(const Calendar& c1, |
140 | | const Calendar& c2, |
141 | | const Calendar& c3, |
142 | | const Calendar& c4, |
143 | 0 | JointCalendarRule r) { |
144 | 0 | impl_ = ext::shared_ptr<Calendar::Impl>( |
145 | 0 | new JointCalendar::Impl(c1,c2,c3,c4,r)); |
146 | 0 | } |
147 | | |
148 | | JointCalendar::JointCalendar(const std::vector<Calendar> &cv, |
149 | 0 | JointCalendarRule r) { |
150 | 0 | impl_ = ext::shared_ptr<Calendar::Impl>( |
151 | 0 | new JointCalendar::Impl(cv,r)); |
152 | 0 | } |
153 | | |
154 | | } |