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.joda;
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  import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
40  import net.objectlab.kit.datecalc.common.CurrencyDateCalculatorBuilder;
41  import net.objectlab.kit.datecalc.common.HolidayHandler;
42  import net.objectlab.kit.datecalc.common.HolidayHandlerType;
43  import net.objectlab.kit.datecalc.common.IMMDateCalculator;
44  import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
45  import net.objectlab.kit.datecalc.common.SpotLag;
46  
47  import org.joda.time.LocalDate;
48  
49  /**
50   * The default factory for getting Joda <code>LocalDate</code> based
51   * calculators.
52   *
53   * @author Benoit Xhenseval
54   *
55   */
56  public class LocalDateKitCalculatorsFactory extends AbstractKitCalculatorsFactory<LocalDate> {
57  
58      private static final LocalDateKitCalculatorsFactory DEFAULT = new LocalDateKitCalculatorsFactory();
59  
60      private static final PeriodCountCalculator<LocalDate> PCC = new LocalDatePeriodCountCalculator();
61  
62      private static final IMMDateCalculator<LocalDate> IMMDC = new LocalDateIMMDateCalculator();
63  
64      public static LocalDateKitCalculatorsFactory getDefaultInstance() {
65          return DEFAULT;
66      }
67  
68      /**
69       * Return a builder using the registered calendars/working weeks and a Modified Forward Holiday handler for the currency pair; .
70       *
71       * If you want to change some of the parameters, simply modify the Builder returned and pass it to the constructor of the
72       * calculator you are interested in.
73       */
74      public CurrencyDateCalculatorBuilder<LocalDate> getDefaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2,
75              final SpotLag spotLag) {
76          final CurrencyDateCalculatorBuilder<LocalDate> builder = new CurrencyDateCalculatorBuilder<LocalDate>().currencyPair(ccy1, ccy2, spotLag);
77  
78          return configureCurrencyCalculatorBuilder(builder).tenorHolidayHandler(new LocalDateModifiedFollowingHandler());
79      }
80  
81      public static CurrencyDateCalculatorBuilder<LocalDate> defaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2,
82              final SpotLag spotLag) {
83          return DEFAULT.getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag);
84      }
85  
86      public static LocalDateCurrencyDateCalculator forwardCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
87          return DEFAULT.getDefaultCurrencyDateCalculator(ccy1, ccy2, spotLag);
88      }
89  
90      public LocalDateCurrencyDateCalculator getDefaultCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
91          return new LocalDateCurrencyDateCalculator(getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag));
92      }
93  
94      public LocalDateCurrencyDateCalculator buildCurrencyDateCalculator(final CurrencyDateCalculatorBuilder<LocalDate> builder) {
95          return new LocalDateCurrencyDateCalculator(builder);
96      }
97  
98      public static LocalDateCalculator forwardCalculator(final String name) {
99          return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD);
100     }
101 
102     public static LocalDateCalculator backwardCalculator(final String name) {
103         return DEFAULT.getDateCalculator(name, HolidayHandlerType.BACKWARD);
104     }
105 
106     public static LocalDateCalculator forwardUnlessMovingBackCalculator(final String name) {
107         return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK);
108     }
109 
110     public static LocalDateCalculator modifiedFollowingCalculator(final String name) {
111         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_FOLLOWING);
112     }
113 
114     public static LocalDateCalculator modifiedPrecedingCalculator(final String name) {
115         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_PRECEDING);
116     }
117 
118     // -----------------------------------------------------------------------
119     //
120     // ObjectLab, world leaders in the design and development of bespoke
121     // applications for the securities financing markets.
122     // www.ObjectLab.co.uk
123     //
124     // -----------------------------------------------------------------------
125 
126     /**
127      * Create a new DateCalculator for a given name and type of handling.
128      *
129      * @param name
130      *            calendar name (holidays set interested in). If there is set of
131      *            holidays with that name, it will return a DateCalculator with
132      *            an empty holiday set (will work on Weekend only).
133      * @param holidayHandlerType
134      *            typically one of the value of HolidayHandlerType
135      * @return a new DateCalculator
136      */
137     public LocalDateCalculator getDateCalculator(final String name, final String holidayHandlerType) {
138         final LocalDateCalculator cal = new LocalDateCalculator();
139         cal.setName(name);
140         setHolidays(name, cal);
141 
142         cal.setHolidayHandler(getHolidayHandler(holidayHandlerType));
143         return cal;
144     }
145 
146     public HolidayHandler<LocalDate> getHolidayHandler(final String holidayHandlerType) {
147         if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
148             return new LocalDateForwardHandler();
149         } else if (BACKWARD.equals(holidayHandlerType)) {
150             return new LocalDateBackwardHandler();
151         } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
152             return new LocalDateModifiedFollowingHandler();
153         } else if (MODIFIED_PRECEDING.equals(holidayHandlerType)) {
154             return new LocalDateModifiedPrecedingHandler();
155         } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
156             return new LocalDateForwardUnlessNegativeHandler();
157         } else if (holidayHandlerType != null) {
158             throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
159         }
160         return null;
161     }
162 
163     public PeriodCountCalculator<LocalDate> getPeriodCountCalculator() {
164         return PCC;
165     }
166 
167     public IMMDateCalculator<LocalDate> getIMMDateCalculator() {
168         return IMMDC;
169     }
170 }
171 
172 /*
173  * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
174  *
175  * Based in London, we are world leaders in the design and development
176  * of bespoke applications for the securities financing markets.
177  *
178  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
179  *           ___  _     _           _   _          _
180  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
181  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
182  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
183  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
184  *                   |__/
185  *
186  *                     www.ObjectLab.co.uk
187  */