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$
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.text.ParseException;
036import java.text.SimpleDateFormat;
037import java.util.Calendar;
038import java.util.Date;
039import java.util.HashSet;
040import java.util.List;
041import java.util.Set;
042import java.util.stream.Collectors;
043
044/**
045 * Utilities class for <code>Date/Calendar</code> conversions
046 *
047 * @author Marcin Jekot
048 *
049 */
050public final class Utils {
051    private static final String DATE_PATTERN = "yyyy-MM-dd";
052
053    private Utils() {
054    }
055
056    /**
057     * Removes set's all "time" fields to zero, leaving only the date portion of
058     * the Calendar. The Calendar passe
059     *
060     * @param cal
061     *            to Calendar object to blast, note, it will be modified
062     * @return the calendar object modified (same instance)
063     */
064    public static Calendar blastTime(final Calendar cal) {
065        cal.set(Calendar.HOUR_OF_DAY, 0);
066        cal.set(Calendar.MINUTE, 0);
067        cal.set(Calendar.SECOND, 0);
068        cal.set(Calendar.MILLISECOND, 0);
069        return cal;
070    }
071
072    /**
073     * Creates a Date object given a string representation of it
074     *
075     * @param dateStr
076     *            string (return today if string is null)
077     * @return today if string is null, a Date object representing the string
078     *         otherwise
079     * @throws IllegalArgumentException
080     *             if the string cannot be parsed.
081     */
082    public static Date createDate(final String dateStr) {
083        if (dateStr == null) {
084            return createCalendar(null).getTime();
085        }
086        final Calendar cal = getCal(dateStr);
087        return cal != null ? cal.getTime() : null;
088    }
089
090    // -----------------------------------------------------------------------
091    //
092    // ObjectLab, world leaders in the design and development of bespoke
093    // applications for the securities financing markets.
094    // www.ObjectLab.co.uk
095    //
096    // -----------------------------------------------------------------------
097
098    /**
099     * get a new Calendar based on the string date.
100     * @param dateStr
101     *            the date string
102     * @return a new Calendar
103     * @throws IllegalArgumentException
104     *             if the string cannot be parsed.
105     */
106    public static Calendar createCalendar(final String dateStr) {
107        if (dateStr == null) {
108            return blastTime(Calendar.getInstance());
109        }
110        return getCal(dateStr);
111    }
112
113    public static Calendar getCal(final String dateStr) {
114        try {
115            final Date date = new SimpleDateFormat(DATE_PATTERN).parse(dateStr);
116            return getCal(date);
117        } catch (final ParseException e) {
118            throw new IllegalArgumentException("\"" + dateStr + "\"" + " is an invalid date, the pattern is : " + DATE_PATTERN, e);
119        }
120    }
121
122    /**
123     * Get a Calendar object for a given Date representation.
124     *
125     * @param date
126     * @return the Calendar
127     */
128    public static Calendar getCal(final Date date) {
129        if (date == null) {
130            return null;
131        }
132        final Calendar cal = Calendar.getInstance();
133        cal.setTime(date);
134        return blastTime(cal);
135    }
136
137    /**
138     * Converts a Set of Date objects to a Set of Calendar objects.
139     *
140     * @param dates
141     * @return the converted Set&lt;Calendar&gt;
142     */
143    public static Set<Calendar> toCalendarSet(final Set<Date> dates) {
144        return dates.stream().map(Utils::getCal).collect(Collectors.toSet());
145    }
146
147    /**
148     * Converts a Set of Date objects to a Set of Calendar objects.
149     *
150     * @param dates
151     * @return the converted Set&lt;Calendar&gt;
152     */
153    public static HolidayCalendar<Calendar> toHolidayCalendarSet(final HolidayCalendar<Date> dates) {
154        final Set<Calendar> calendars = new HashSet<>();
155        for (final Date date : dates.getHolidays()) {
156            calendars.add(getCal(date));
157        }
158        return new DefaultHolidayCalendar<>(calendars, getCal(dates.getEarlyBoundary()), getCal(dates.getLateBoundary()));
159    }
160
161    /**
162     * Converts a Set of Calendar objects to a Set of Date objects
163     *
164     * @param calendars
165     * @return the converset Set&lt;Date&gt;
166     */
167    public static Set<Date> toDateSet(final Set<Calendar> calendars) {
168        return calendars.stream().map(Calendar::getTime).collect(Collectors.toSet());
169    }
170
171    /**
172     * Converts a <code>List</code> of Calendar objects to a <code>List</code>
173     * of dates
174     *
175     * @param dates
176     * @return the converted List&lt;Date&gt;
177     */
178    public static List<Date> toDateList(final List<Calendar> dates) {
179        return dates.stream().map(Calendar::getTime).collect(Collectors.toList());
180    }
181}
182
183/*
184 * ObjectLab, http://www.objectlab.co.uk/open is sponsoring the ObjectLab Kit.
185 *
186 * Based in London, we are world leaders in the design and development
187 * of bespoke applications for the securities financing markets.
188 *
189 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
190 *           ___  _     _           _   _          _
191 *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
192 *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
193 *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
194 *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
195 *                   |__/
196 *
197 *                     www.ObjectLab.co.uk
198 */