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.Date;
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>Date</code> based calculators.
52   *
53   * @author Benoit Xhenseval
54   *
55   */
56  public class DateKitCalculatorsFactory extends AbstractKitCalculatorsFactory<Date> {
57  
58      private static final DateKitCalculatorsFactory DEFAULT = new DateKitCalculatorsFactory();
59  
60      private static final PeriodCountCalculator<Date> PCC = new DatePeriodCountCalculator();
61  
62      private static final DateIMMDateCalculator IMMDC = new DateIMMDateCalculator();
63  
64      public static DateKitCalculatorsFactory 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; this
70       * does NOT copy the calendars or Currency Config.
71       *
72       * If you want to change some of the parameters, simply modify the Builder returned and pass it to the constructor of the
73       * calculator you are interested in.
74       */
75      public CurrencyDateCalculatorBuilder<Date> getDefaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2, final SpotLag spotLag) {
76          final CurrencyDateCalculatorBuilder<Date> builder = new CurrencyDateCalculatorBuilder<Date>().currencyPair(ccy1, ccy2, spotLag);
77  
78          return configureCurrencyCalculatorBuilder(builder).tenorHolidayHandler(new DateModifiedFollowingHandler());
79      }
80  
81      public static CurrencyDateCalculatorBuilder<Date> defaultCurrencyDateCalculatorBuilder(final String ccy1, final String ccy2, final SpotLag spotLag) {
82          return DEFAULT.getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag);
83      }
84  
85      public DateCurrencyDateCalculator buildCurrencyDateCalculator(final CurrencyDateCalculatorBuilder<Date> builder) {
86          return new DateCurrencyDateCalculator(builder);
87      }
88  
89      public static DateCurrencyDateCalculator forwardCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
90          return DEFAULT.getDefaultCurrencyDateCalculator(ccy1, ccy2, spotLag);
91      }
92  
93      public DateCurrencyDateCalculator getDefaultCurrencyDateCalculator(final String ccy1, final String ccy2, final SpotLag spotLag) {
94          return new DateCurrencyDateCalculator(DEFAULT.getDefaultCurrencyDateCalculatorBuilder(ccy1, ccy2, spotLag));
95      }
96  
97      public static DateDateCalculator forwardCalculator(final String name) {
98          return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD);
99      }
100 
101     public static DateDateCalculator backwardCalculator(final String name) {
102         return DEFAULT.getDateCalculator(name, HolidayHandlerType.BACKWARD);
103     }
104 
105     public static DateDateCalculator forwardUnlessMovingBackCalculator(final String name) {
106         return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK);
107     }
108 
109     public static DateDateCalculator modifiedFollowingCalculator(final String name) {
110         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_FOLLOWING);
111     }
112 
113     public static DateDateCalculator modifiedPrecedingCalculator(final String name) {
114         return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_PRECEDING);
115     }
116 
117     // -----------------------------------------------------------------------
118     //
119     // ObjectLab, world leaders in the design and development of bespoke
120     // applications for the securities financing markets.
121     // www.ObjectLab.co.uk
122     //
123     // -----------------------------------------------------------------------
124 
125     /*
126      * (non-Javadoc)
127      *
128      * @see net.objectlab.kit.datecalc.jdk.JdkDateCalculatorFactory#getDateCalculator(java.lang.String,
129      *      java.lang.String)
130      */
131     public DateDateCalculator getDateCalculator(final String name, final String holidayHandlerType) {
132         final DateDateCalculator cal = new DateDateCalculator();
133         cal.setName(name);
134         setHolidays(name, cal);
135 
136         cal.setHolidayHandler(getHolidayHandler(holidayHandlerType));
137 
138         return cal;
139     }
140 
141     public HolidayHandler<Date> getHolidayHandler(final String holidayHandlerType) {
142         if (HolidayHandlerType.FORWARD.equals(holidayHandlerType)) {
143             return new DateForwardHandler();
144         } else if (BACKWARD.equals(holidayHandlerType)) {
145             return new DateBackwardHandler();
146         } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
147             return new DateModifiedFollowingHandler();
148         } else if (MODIFIED_PRECEDING.equals(holidayHandlerType)) {
149             return new DateModifiedPreceedingHandler();
150         } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
151             return new DateForwardUnlessNegativeHandler();
152         } else if (holidayHandlerType != null) {
153             throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
154         }
155         return null;
156     }
157 
158     /*
159      * (non-Javadoc)
160      *
161      * @see net.objectlab.kit.datecalc.jdk.JdkDateCalculatorFactory#getPeriodCountCalculator()
162      */
163     public PeriodCountCalculator<Date> getPeriodCountCalculator() {
164         return PCC;
165     }
166 
167     public IMMDateCalculator<Date> 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  */