View Javadoc
1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
3    *
4    * Based in London, we are world leaders in the design and development
5    * of bespoke applications for the securities financing markets.
6    *
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id$
18   *
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.objectlab.kit.datecalc.jdk;
34  
35  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.BACKWARD;
36  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
37  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
38  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEDING;
39  
40  import java.util.Calendar;
41  
42  import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
43  import net.objectlab.kit.datecalc.common.CurrencyDateCalculatorBuilder;
44  import net.objectlab.kit.datecalc.common.HolidayHandler;
45  import net.objectlab.kit.datecalc.common.HolidayHandlerType;
46  import net.objectlab.kit.datecalc.common.IMMDateCalculator;
47  import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
48  import net.objectlab.kit.datecalc.common.SpotLag;
49  
50  /**
51   * The default factory for getting Jdk <code>Calendar</code> based
52   * calculators.
53   *
54   * @author Marcin Jekot
55   *
56   */
57  public class CalendarKitCalculatorsFactory extends AbstractKitCalculatorsFactory<Calendar> {
58  
59      private static final CalendarKitCalculatorsFactory DEFAULT = new CalendarKitCalculatorsFactory();
60  
61      private static final PeriodCountCalculator<Calendar> PCC = new CalendarPeriodCountCalculator();
62  
63      private static final CalendarIMMDateCalculator IMMDC = new CalendarIMMDateCalculator();
64  
65      public static CalendarKitCalculatorsFactory getDefaultInstance() {
66          return DEFAULT;
67      }
68  
69      /**
70       * Return a builder using the registered calendars/working weeks and a Modified Forward Holiday handler for the currency pair; this
71       * does NOT copy the calendars or Currency Config.
72       *
73       * If you want to change some of the parameters, simply modify the Builder returned and pass it to the constructor of the
74       * calculator you are interested in.
75       */
76      public CurrencyDateCalculatorBuilder<Calendar> getDefaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2, final SpotLag spotLag) {
77          final CurrencyDateCalculatorBuilder<Calendar> builder = new CurrencyDateCalculatorBuilder<Calendar>().currencyPair(ccy1, ccy2, spotLag);
78  
79          return configureCurrencyCalculatorBuilder(builder).tenorHolidayHandler(new CalendarModifiedFollowingHandler());
80      }
81  
82      public static CurrencyDateCalculatorBuilder<Calendar> defaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2,
83              final SpotLag spotLag) {
84          return DEFAULT.getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag);
85      }
86  
87      public CalendarCurrencyDateCalculator buildCurrencyDateCalculator(final CurrencyDateCalculatorBuilder<Calendar> builder) {
88          return new CalendarCurrencyDateCalculator(builder);
89      }
90  
91      public static CalendarCurrencyDateCalculator forwardCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
92          return DEFAULT.getDefaultCurrencyDateCalculator(ccy1, ccy2, spotLag);
93      }
94  
95      public CalendarCurrencyDateCalculator getDefaultCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
96          return new CalendarCurrencyDateCalculator(getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag));
97      }
98  
99      public static CalendarDateCalculator forwardCalculator(final String name) {
100         return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD);
101     }
102 
103     public static CalendarDateCalculator backwardCalculator(final String name) {
104         return DEFAULT.getDateCalculator(name, HolidayHandlerType.BACKWARD);
105     }
106 
107     public static CalendarDateCalculator forwardUnlessMovingBackCalculator(final String name) {
108         return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK);
109     }
110 
111     public static CalendarDateCalculator modifiedFollowingCalculator(final String name) {
112         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_FOLLOWING);
113     }
114 
115     public static CalendarDateCalculator modifiedPrecedingCalculator(final String name) {
116         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_PRECEDING);
117     }
118 
119     /**
120      * Create a new DateCalculator for a given name and type of handling.
121      *
122      * @param name
123      *            calendar name (holidays set interested in). If there is set of
124      *            holidays with that name, it will return a DateCalculator with
125      *            an empty holiday set (will work on Weekend only).
126      * @param holidayHandlerType
127      *            typically one of the value of HolidayHandlerType or null.
128      * @return a new DateCalculator
129      * @exception IllegalArgumentException
130      *                if the type is not null or a valid value.
131      */
132     public CalendarDateCalculator getDateCalculator(final String name, final String holidayHandlerType) {
133         final CalendarDateCalculator cal = new CalendarDateCalculator();
134         cal.setName(name);
135         setHolidays(name, cal);
136         if (holidayHandlerType != null) {
137             cal.setHolidayHandler(getHolidayHandler(holidayHandlerType));
138         }
139         return cal;
140     }
141 
142     public HolidayHandler<Calendar> getHolidayHandler(final String holidayHandlerType) {
143         if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
144             return new CalendarForwardHandler();
145         } else if (BACKWARD.equals(holidayHandlerType)) {
146             return new CalendarBackwardHandler();
147         } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
148             return new CalendarModifiedFollowingHandler();
149         } else if (MODIFIED_PRECEDING.equals(holidayHandlerType)) {
150             return new CalendarModifiedPrecedingHandler();
151         } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
152             return new CalendarForwardUnlessNegativeHandler();
153         } else if (holidayHandlerType != null) {
154             throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
155         }
156         return null;
157     }
158 
159     // -----------------------------------------------------------------------
160     //
161     // ObjectLab, world leaders in the design and development of bespoke
162     // applications for the securities financing markets.
163     // www.ObjectLab.co.uk
164     //
165     // -----------------------------------------------------------------------
166 
167     /**
168      * Create a new PeriodCountCalculator.
169      *
170      * @return a PeriodCountCalculator
171      */
172     public PeriodCountCalculator<Calendar> getPeriodCountCalculator() {
173         return PCC;
174     }
175 
176     /**
177      * Create a new IMMDateCalculator.
178      *
179      * @return an IMMDateCalculator
180      */
181     public IMMDateCalculator<Calendar> getIMMDateCalculator() {
182         return IMMDC;
183     }
184 }
185 
186 /*
187  * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
188  *
189  * Based in London, we are world leaders in the design and development
190  * of bespoke applications for the securities financing markets.
191  *
192  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
193  *           ___  _     _           _   _          _
194  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
195  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
196  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
197  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
198  *                   |__/
199  *
200  *                     www.ObjectLab.co.uk
201  */