001/*
002 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
003 *
004 * Based in London, we are world leaders in the design and development
005 * of bespoke applications for the securities financing markets.
006 *
007 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
008 *           ___  _     _           _   _          _
009 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
010 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
011 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
012 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
013 *                   |__/
014 *
015 *                     www.ObjectLab.co.uk
016 *
017 * $Id: DateCalculator.java 200 2006-10-10 20:15:58Z benoitx $
018 *
019 * Copyright 2006 the original author or authors.
020 *
021 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
022 * use this file except in compliance with the License. You may obtain a copy of
023 * the License at
024 *
025 * http://www.apache.org/licenses/LICENSE-2.0
026 *
027 * Unless required by applicable law or agreed to in writing, software
028 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
029 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
030 * License for the specific language governing permissions and limitations under
031 * the License.
032 */
033package net.objectlab.kit.datecalc.common;
034
035import java.io.Serializable;
036import java.text.SimpleDateFormat;
037import java.util.Calendar;
038import java.util.Collections;
039import java.util.Date;
040import java.util.HashSet;
041import java.util.Map;
042import java.util.Set;
043import java.util.TreeMap;
044
045/**
046 * @author $LastChangedBy: marchy $
047 *
048 */
049public class DefaultHolidayCalendar<E extends Serializable> implements HolidayCalendar<E> {
050    private static final long serialVersionUID = -8558686840806739645L;
051
052    /**
053     * Changed to a Map of String to E, given the JODA issue 
054     * http://joda-interest.219941.n2.nabble.com/LocalDate-equals-method-bug-td7572429.html
055     * @since 1.4.0
056     */
057    private Map<String, E> holidays;
058
059    private E earlyBoundary;
060
061    private E lateBoundary;
062
063    public DefaultHolidayCalendar() {
064        super();
065        holidays = Collections.emptyMap();
066    }
067
068    public DefaultHolidayCalendar(final Set<E> holidays, final E earlyBoundary, final E lateBoundary) {
069        super();
070        setHolidays(holidays);
071
072        this.earlyBoundary = earlyBoundary;
073        this.lateBoundary = lateBoundary;
074    }
075
076    public DefaultHolidayCalendar(final Set<E> holidays) {
077        super();
078        setHolidays(holidays);
079    }
080
081    /*
082     * (non-Javadoc)
083     *
084     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#getEarlyBoundary()
085     */
086    @Override
087    public E getEarlyBoundary() {
088        return earlyBoundary;
089    }
090
091    /*
092     * (non-Javadoc)
093     *
094     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#getHolidays()
095     */
096    @Override
097    public Set<E> getHolidays() {
098        return new HashSet<>(holidays.values());
099    }
100
101    /*
102     * (non-Javadoc)
103     *
104     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#getLateBoundary()
105     */
106    @Override
107    public E getLateBoundary() {
108        return lateBoundary;
109    }
110
111    /*
112     * (non-Javadoc)
113     *
114     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#setEarlyBoundary(java.lang.Object)
115     */
116    @Override
117    public HolidayCalendar<E> setEarlyBoundary(final E earlyBoundary) {
118        this.earlyBoundary = earlyBoundary;
119        return this;
120    }
121
122    /*
123     * (non-Javadoc)
124     *
125     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#setHolidays(java.util.Set)
126     */
127    @Override
128    public final HolidayCalendar<E> setHolidays(final Set<E> holidays) {
129
130        if (holidays == null) {
131            this.holidays = Collections.emptyMap();
132            return this;
133        }
134
135        final Map<String, E> newSet = new TreeMap<>();
136        for (final E e : holidays) {
137            newSet.put(toString(e), e);
138        }
139        this.holidays = Collections.unmodifiableMap(newSet);
140        return this;
141    }
142
143    /*
144     * (non-Javadoc)
145     *
146     * @see net.objectlab.kit.datecalc.common.HolidayCalendar#setLateBoundary(java.lang.Object)
147     */
148    @Override
149    public HolidayCalendar<E> setLateBoundary(final E lateBoundary) {
150        this.lateBoundary = lateBoundary;
151        return this;
152    }
153
154    @Override
155    public boolean isHoliday(final E date) {
156        return holidays.containsKey(toString(date));
157    }
158
159    private String toString(final E date) {
160        if (date instanceof Calendar) {
161            return new SimpleDateFormat("yyyy-MM-dd").format(((Calendar) date).getTime());
162        } else if (date instanceof Date) {
163            return new SimpleDateFormat("yyyy-MM-dd").format(date);
164        }
165
166        return date != null ? date.toString() : "";
167    }
168}