| 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 | : rule_(r), calendars_(2) { |
| 33 | calendars_[0] = c1; |
| 34 | calendars_[1] = c2; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | JointCalendar::Impl::Impl(const Calendar& c1, |
| 39 | const Calendar& c2, |
| 40 | const Calendar& c3, |
| 41 | JointCalendarRule r) |
| 42 | : rule_(r), calendars_(3) { |
| 43 | calendars_[0] = c1; |
| 44 | calendars_[1] = c2; |
| 45 | calendars_[2] = c3; |
| 46 | } |
| 47 | |
| 48 | JointCalendar::Impl::Impl(const Calendar& c1, |
| 49 | const Calendar& c2, |
| 50 | const Calendar& c3, |
| 51 | const Calendar& c4, |
| 52 | JointCalendarRule r) |
| 53 | : rule_(r), calendars_(4) { |
| 54 | calendars_[0] = c1; |
| 55 | calendars_[1] = c2; |
| 56 | calendars_[2] = c3; |
| 57 | calendars_[3] = c4; |
| 58 | } |
| 59 | |
| 60 | JointCalendar::Impl::Impl(std::vector<Calendar> cv, JointCalendarRule r) |
| 61 | : rule_(r), calendars_(std::move(cv)) {} |
| 62 | |
| 63 | std::string JointCalendar::Impl::name() const { |
| 64 | std::ostringstream out; |
| 65 | switch (rule_) { |
| 66 | case JoinHolidays: |
| 67 | out << "JoinHolidays(" ; |
| 68 | break; |
| 69 | case JoinBusinessDays: |
| 70 | out << "JoinBusinessDays(" ; |
| 71 | break; |
| 72 | default: |
| 73 | QL_FAIL("unknown joint calendar rule" ); |
| 74 | } |
| 75 | out << calendars_.front().name(); |
| 76 | std::vector<Calendar>::const_iterator i; |
| 77 | for (i=calendars_.begin()+1; i!=calendars_.end(); ++i) |
| 78 | out << ", " << i->name(); |
| 79 | out << ")" ; |
| 80 | return out.str(); |
| 81 | } |
| 82 | |
| 83 | bool JointCalendar::Impl::isWeekend(Weekday w) const { |
| 84 | std::vector<Calendar>::const_iterator i; |
| 85 | switch (rule_) { |
| 86 | case JoinHolidays: |
| 87 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
| 88 | if (i->isWeekend(w)) |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
| 92 | case JoinBusinessDays: |
| 93 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
| 94 | if (!i->isWeekend(w)) |
| 95 | return false; |
| 96 | } |
| 97 | return true; |
| 98 | default: |
| 99 | QL_FAIL("unknown joint calendar rule" ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool JointCalendar::Impl::isBusinessDay(const Date& date) const { |
| 104 | std::vector<Calendar>::const_iterator i; |
| 105 | switch (rule_) { |
| 106 | case JoinHolidays: |
| 107 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
| 108 | if (i->isHoliday(d: date)) |
| 109 | return false; |
| 110 | } |
| 111 | return true; |
| 112 | case JoinBusinessDays: |
| 113 | for (i=calendars_.begin(); i!=calendars_.end(); ++i) { |
| 114 | if (i->isBusinessDay(d: date)) |
| 115 | return true; |
| 116 | } |
| 117 | return false; |
| 118 | default: |
| 119 | QL_FAIL("unknown joint calendar rule" ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | JointCalendar::JointCalendar(const Calendar& c1, |
| 125 | const Calendar& c2, |
| 126 | JointCalendarRule r) { |
| 127 | impl_ = ext::shared_ptr<Calendar::Impl>( |
| 128 | new JointCalendar::Impl(c1,c2,r)); |
| 129 | } |
| 130 | |
| 131 | JointCalendar::JointCalendar(const Calendar& c1, |
| 132 | const Calendar& c2, |
| 133 | const Calendar& c3, |
| 134 | JointCalendarRule r) { |
| 135 | impl_ = ext::shared_ptr<Calendar::Impl>( |
| 136 | new JointCalendar::Impl(c1,c2,c3,r)); |
| 137 | } |
| 138 | |
| 139 | JointCalendar::JointCalendar(const Calendar& c1, |
| 140 | const Calendar& c2, |
| 141 | const Calendar& c3, |
| 142 | const Calendar& c4, |
| 143 | JointCalendarRule r) { |
| 144 | impl_ = ext::shared_ptr<Calendar::Impl>( |
| 145 | new JointCalendar::Impl(c1,c2,c3,c4,r)); |
| 146 | } |
| 147 | |
| 148 | JointCalendar::JointCalendar(const std::vector<Calendar> &cv, |
| 149 | JointCalendarRule r) { |
| 150 | impl_ = ext::shared_ptr<Calendar::Impl>( |
| 151 | new JointCalendar::Impl(cv,r)); |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |