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;
37  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK;
38  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_FOLLOWING;
39  import static net.objectlab.kit.datecalc.common.HolidayHandlerType.MODIFIED_PRECEDING;
40  import net.objectlab.kit.datecalc.common.AbstractKitCalculatorsFactory;
41  import net.objectlab.kit.datecalc.common.DateCalculator;
42  import net.objectlab.kit.datecalc.common.HolidayHandlerType;
43  import net.objectlab.kit.datecalc.common.IMMDateCalculator;
44  import net.objectlab.kit.datecalc.common.KitCalculatorsFactory;
45  import net.objectlab.kit.datecalc.common.PeriodCountCalculator;
46  
47  import org.joda.time.YearMonthDay;
48  
49  /**
50   * The default factory for getting Joda <code>YearMonthDay</code> based
51   * calculators.
52   * 
53   * @author Benoit Xhenseval
54   * 
55   */
56  public class YearMonthDayKitCalculatorsFactory extends AbstractKitCalculatorsFactory<YearMonthDay> {
57  
58      private static final YearMonthDayKitCalculatorsFactory DEFAULT = new YearMonthDayKitCalculatorsFactory();
59  
60      private static final PeriodCountCalculator<YearMonthDay> PCC = new YearMonthDayPeriodCountCalculator();
61  
62      private static final IMMDateCalculator<YearMonthDay> IMMDC = new YearMonthDayIMMDateCalculator();
63  
64      public static YearMonthDayKitCalculatorsFactory getDefaultInstance() {
65          return DEFAULT;
66      }
67      
68      public static YearMonthDayDateCalculator forwardCalculator(final String name) {
69          return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD);
70      }
71      
72      public static YearMonthDayDateCalculator backwardCalculator(final String name) {
73          return DEFAULT.getDateCalculator(name, HolidayHandlerType.BACKWARD);
74      }
75      
76      public static YearMonthDayDateCalculator forwardUnlessMovingBackCalculator(final String name) {
77          return DEFAULT.getDateCalculator(name, HolidayHandlerType.FORWARD_UNLESS_MOVING_BACK);
78      }
79      
80      public static YearMonthDayDateCalculator modifiedFollowingCalculator(final String name) {
81          return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_FOLLOWING);
82      }
83      
84      public static YearMonthDayDateCalculator modifiedPrecedingCalculator(final String name) {
85          return DEFAULT.getDateCalculator(name, HolidayHandlerType.MODIFIED_PRECEDING);
86      }
87  
88      // -----------------------------------------------------------------------
89      //
90      //    ObjectLab, world leaders in the design and development of bespoke 
91      //          applications for the securities financing markets.
92      //                         www.ObjectLab.co.uk
93      //
94      // -----------------------------------------------------------------------
95  
96     /**
97       * Create a new DateCalculator for a given name and type of handling.
98       * 
99       * @param name
100      *            calendar name (holidays set interested in). If there is set of
101      *            holidays with that name, it will return a DateCalculator with
102      *            an empty holiday set (will work on Weekend only).
103      * @param holidayHandlerType
104      *            typically one of the value of HolidayHandlerType
105      * @return a new DateCalculator
106      * @throws IllegalArgumentException if name is null
107      */
108     public YearMonthDayDateCalculator getDateCalculator(final String name, final String holidayHandlerType) {
109         if (name == null) {
110             throw new IllegalArgumentException("name cannot be null, use anything.");
111         }
112         final YearMonthDayDateCalculator cal = new YearMonthDayDateCalculator();
113         cal.setName(name);
114         setHolidays(name, cal);
115 
116         if (FORWARD.equals(holidayHandlerType)) {
117             cal.setHolidayHandler(new YearMonthDayForwardHandler());
118         } else if (BACKWARD.equals(holidayHandlerType)) {
119             cal.setHolidayHandler(new YearMonthDayBackwardHandler());
120         } else if (MODIFIED_FOLLOWING.equals(holidayHandlerType)) {
121             cal.setHolidayHandler(new YearMonthDayModifiedFollowingHandler());
122         } else if (MODIFIED_PRECEDING.equals(holidayHandlerType)) {
123             cal.setHolidayHandler(new YearMonthDayModifiedPrecedingHandler());
124         } else if (FORWARD_UNLESS_MOVING_BACK.equals(holidayHandlerType)) {
125             cal.setHolidayHandler(new YearMonthDayForwardUnlessNegativeHandler());
126         } else if (holidayHandlerType != null) {
127             throw new IllegalArgumentException("Unsupported HolidayHandler: " + holidayHandlerType);
128         }
129         return cal;
130     }
131 
132     public PeriodCountCalculator<YearMonthDay> getPeriodCountCalculator() {
133         return PCC;
134     }
135 
136     public IMMDateCalculator<YearMonthDay> getIMMDateCalculator() {
137         return IMMDC;
138     }
139 }
140 
141 /*
142  * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
143  * 
144  * Based in London, we are world leaders in the design and development 
145  * of bespoke applications for the securities financing markets.
146  * 
147  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
148  *           ___  _     _           _   _          _
149  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
150  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
151  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
152  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
153  *                   |__/
154  *
155  *                     www.ObjectLab.co.uk
156  */