michael@0: /**
michael@0: * Copyright (c) 2012, Ben Fortuna
michael@0: * All rights reserved.
michael@0: *
michael@0: * Redistribution and use in source and binary forms, with or without
michael@0: * modification, are permitted provided that the following conditions
michael@0: * are met:
michael@0: *
michael@0: * o Redistributions of source code must retain the above copyright
michael@0: * notice, this list of conditions and the following disclaimer.
michael@0: *
michael@0: * o Redistributions in binary form must reproduce the above copyright
michael@0: * notice, this list of conditions and the following disclaimer in the
michael@0: * documentation and/or other materials provided with the distribution.
michael@0: *
michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors
michael@0: * may be used to endorse or promote products derived from this software
michael@0: * without specific prior written permission.
michael@0: *
michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0: */
michael@0: package net.fortuna.ical4j.model;
michael@0:
michael@0: import java.io.Serializable;
michael@0: import java.util.Calendar;
michael@0:
michael@0: import net.fortuna.ical4j.util.Numbers;
michael@0:
michael@4: import org.apache.commons.lang3.ObjectUtils;
michael@4: import org.apache.commons.lang3.builder.HashCodeBuilder;
michael@0:
michael@0: /**
michael@0: * $Id$
michael@0: *
michael@0: * Created: 19/12/2004
michael@0: *
michael@0: * Defines a day of the week with a possible offset related to
michael@0: * a MONTHLY or YEARLY occurrence.
michael@0: *
michael@0: * @author Ben Fortuna
michael@0: */
michael@0: public class WeekDay implements Serializable {
michael@0:
michael@0: private static final long serialVersionUID = -4412000990022011469L;
michael@0:
michael@0: /**
michael@0: * Sunday.
michael@0: */
michael@0: public static final WeekDay SU = new WeekDay("SU", 0);
michael@0:
michael@0: /**
michael@0: * Monday.
michael@0: */
michael@0: public static final WeekDay MO = new WeekDay("MO", 0);
michael@0:
michael@0: /**
michael@0: * Tuesday.
michael@0: */
michael@0: public static final WeekDay TU = new WeekDay("TU", 0);
michael@0:
michael@0: /**
michael@0: * Wednesday.
michael@0: */
michael@0: public static final WeekDay WE = new WeekDay("WE", 0);
michael@0:
michael@0: /**
michael@0: * Thursday.
michael@0: */
michael@0: public static final WeekDay TH = new WeekDay("TH", 0);
michael@0:
michael@0: /**
michael@0: * Friday.
michael@0: */
michael@0: public static final WeekDay FR = new WeekDay("FR", 0);
michael@0:
michael@0: /**
michael@0: * Saturday.
michael@0: */
michael@0: public static final WeekDay SA = new WeekDay("SA", 0);
michael@0:
michael@0: private String day;
michael@0:
michael@0: private int offset;
michael@0:
michael@0: /**
michael@0: * @param value a string representation of a week day
michael@0: */
michael@0: public WeekDay(final String value) {
michael@0: if (value.length() > 2) {
michael@0: offset = Numbers.parseInt(value.substring(0, value.length() - 2));
michael@0: }
michael@0: else {
michael@0: offset = 0;
michael@0: }
michael@0: day = value.substring(value.length() - 2);
michael@0: validateDay();
michael@0: }
michael@0:
michael@0: /**
michael@0: * @param day a string representation of a week day
michael@0: * @param offset a month offset value
michael@0: */
michael@0: private WeekDay(final String day, final int offset) {
michael@0: this.day = day;
michael@0: this.offset = offset;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Constructs a new weekday instance based on the specified
michael@0: * instance and offset.
michael@0: * @param weekDay a week day template for the instance
michael@0: * @param offset a month offset value
michael@0: */
michael@0: public WeekDay(final WeekDay weekDay, final int offset) {
michael@0: this.day = weekDay.getDay();
michael@0: this.offset = offset;
michael@0: }
michael@0:
michael@0: private void validateDay() {
michael@0: if (!SU.day.equals(day)
michael@0: && !MO.day.equals(day)
michael@0: && !TU.day.equals(day)
michael@0: && !WE.day.equals(day)
michael@0: && !TH.day.equals(day)
michael@0: && !FR.day.equals(day)
michael@0: && !SA.day.equals(day)) {
michael@0: throw new IllegalArgumentException("Invalid day: " + day);
michael@0: }
michael@0: }
michael@0: /**
michael@0: * @return Returns the day.
michael@0: */
michael@0: public final String getDay() {
michael@0: return day;
michael@0: }
michael@0:
michael@0: /**
michael@0: * @return Returns the offset.
michael@0: */
michael@0: public final int getOffset() {
michael@0: return offset;
michael@0: }
michael@0:
michael@0: /**
michael@0: * {@inheritDoc}
michael@0: */
michael@0: public final String toString() {
michael@0: final StringBuffer b = new StringBuffer();
michael@0: if (getOffset() != 0) {
michael@0: b.append(getOffset());
michael@0: }
michael@0: b.append(getDay());
michael@0: return b.toString();
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns a weekday representation of the specified calendar.
michael@0: * @param cal a calendar (java.util)
michael@0: * @return a weekday instance representing the specified calendar
michael@0: */
michael@0: public static final WeekDay getWeekDay(final Calendar cal) {
michael@0: return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), 0);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns a weekday/offset representation of the specified calendar.
michael@0: * @param cal a calendar (java.util)
michael@0: * @return a weekday instance representing the specified calendar
michael@0: */
michael@0: public static final WeekDay getMonthlyOffset(final Calendar cal) {
michael@0: return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns a weekday/negative offset representation of the specified calendar.
michael@0: * @param cal a calendar (java.util)
michael@0: * @return a weekday instance representing the specified calendar
michael@0: */
michael@0: public static final WeekDay getNegativeMonthlyOffset(final Calendar cal) {
michael@0: return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 6);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the corresponding day constant to the specified
michael@0: * java.util.Calendar.DAY_OF_WEEK property.
michael@0: * @param calDay a property value of java.util.Calendar.DAY_OF_WEEK
michael@0: * @return a string, or null if an invalid DAY_OF_WEEK property is
michael@0: * specified
michael@0: */
michael@0: public static WeekDay getDay(final int calDay) {
michael@0: WeekDay day = null;
michael@0: switch (calDay) {
michael@0: case Calendar.SUNDAY:
michael@0: day = SU;
michael@0: break;
michael@0: case Calendar.MONDAY:
michael@0: day = MO;
michael@0: break;
michael@0: case Calendar.TUESDAY:
michael@0: day = TU;
michael@0: break;
michael@0: case Calendar.WEDNESDAY:
michael@0: day = WE;
michael@0: break;
michael@0: case Calendar.THURSDAY:
michael@0: day = TH;
michael@0: break;
michael@0: case Calendar.FRIDAY:
michael@0: day = FR;
michael@0: break;
michael@0: case Calendar.SATURDAY:
michael@0: day = SA;
michael@0: break;
michael@0: default:
michael@0: break;
michael@0: }
michael@0: return day;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns the corresponding java.util.Calendar.DAY_OF_WEEK
michael@0: * constant for the specified WeekDay
.
michael@0: * @param weekday a week day instance
michael@0: * @return the corresponding java.util.Calendar
day
michael@0: */
michael@0: public static int getCalendarDay(final WeekDay weekday) {
michael@0: int calendarDay = -1;
michael@0: if (SU.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.SUNDAY;
michael@0: }
michael@0: else if (MO.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.MONDAY;
michael@0: }
michael@0: else if (TU.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.TUESDAY;
michael@0: }
michael@0: else if (WE.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.WEDNESDAY;
michael@0: }
michael@0: else if (TH.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.THURSDAY;
michael@0: }
michael@0: else if (FR.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.FRIDAY;
michael@0: }
michael@0: else if (SA.getDay().equals(weekday.getDay())) {
michael@0: calendarDay = Calendar.SATURDAY;
michael@0: }
michael@0: return calendarDay;
michael@0: }
michael@0:
michael@0: /**
michael@0: * {@inheritDoc}
michael@0: */
michael@0: public final boolean equals(final Object arg0) {
michael@0: if (arg0 == null) {
michael@0: return false;
michael@0: }
michael@0: if (!(arg0 instanceof WeekDay)) {
michael@0: return false;
michael@0: }
michael@0: final WeekDay wd = (WeekDay) arg0;
michael@0: return ObjectUtils.equals(wd.getDay(), getDay())
michael@0: && wd.getOffset() == getOffset();
michael@0: }
michael@0:
michael@0: /**
michael@0: * {@inheritDoc}
michael@0: */
michael@0: public final int hashCode() {
michael@0: return new HashCodeBuilder().append(getDay())
michael@0: .append(getOffset()).toHashCode();
michael@0: }
michael@0: }