src/net/fortuna/ical4j/model/WeekDay.java

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
child 4
45d57ecba757
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

     1 /**
     2  * Copyright (c) 2012, Ben Fortuna
     3  * All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions
     7  * are met:
     8  *
     9  *  o Redistributions of source code must retain the above copyright
    10  * notice, this list of conditions and the following disclaimer.
    11  *
    12  *  o Redistributions in binary form must reproduce the above copyright
    13  * notice, this list of conditions and the following disclaimer in the
    14  * documentation and/or other materials provided with the distribution.
    15  *
    16  *  o Neither the name of Ben Fortuna nor the names of any other contributors
    17  * may be used to endorse or promote products derived from this software
    18  * without specific prior written permission.
    19  *
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  */
    32 package net.fortuna.ical4j.model;
    34 import java.io.Serializable;
    35 import java.util.Calendar;
    37 import net.fortuna.ical4j.util.Numbers;
    39 import org.apache.commons.lang.ObjectUtils;
    40 import org.apache.commons.lang.builder.HashCodeBuilder;
    42 /**
    43  * $Id$
    44  * 
    45  * Created: 19/12/2004
    46  *
    47  * Defines a day of the week with a possible offset related to
    48  * a MONTHLY or YEARLY occurrence.
    49  * 
    50  * @author Ben Fortuna
    51  */
    52 public class WeekDay implements Serializable {
    54     private static final long serialVersionUID = -4412000990022011469L;
    56     /**
    57      * Sunday.
    58      */
    59     public static final WeekDay SU = new WeekDay("SU", 0);
    61     /**
    62      * Monday.
    63      */
    64     public static final WeekDay MO = new WeekDay("MO", 0);
    66     /**
    67      * Tuesday.
    68      */
    69     public static final WeekDay TU = new WeekDay("TU", 0);
    71     /**
    72      * Wednesday.
    73      */
    74     public static final WeekDay WE = new WeekDay("WE", 0);
    76     /**
    77      * Thursday.
    78      */
    79     public static final WeekDay TH = new WeekDay("TH", 0);
    81     /**
    82      * Friday.
    83      */
    84     public static final WeekDay FR = new WeekDay("FR", 0);
    86     /**
    87      * Saturday.
    88      */
    89     public static final WeekDay SA = new WeekDay("SA", 0);
    91     private String day;
    93     private int offset;
    95     /**
    96      * @param value a string representation of a week day
    97      */
    98     public WeekDay(final String value) {
    99         if (value.length() > 2) {
   100             offset = Numbers.parseInt(value.substring(0, value.length() - 2));
   101         }
   102         else {
   103             offset = 0;
   104         }
   105         day = value.substring(value.length() - 2);
   106         validateDay();
   107     }
   109     /**
   110      * @param day a string representation of a week day
   111      * @param offset a month offset value
   112      */
   113     private WeekDay(final String day, final int offset) {
   114         this.day = day;
   115         this.offset = offset;
   116     }
   118     /**
   119      * Constructs a new weekday instance based on the specified
   120      * instance and offset.
   121      * @param weekDay a week day template for the instance
   122      * @param offset a month offset value
   123      */
   124     public WeekDay(final WeekDay weekDay, final int offset) {
   125         this.day = weekDay.getDay();
   126         this.offset = offset;
   127     }
   129     private void validateDay() {
   130         if (!SU.day.equals(day)
   131             && !MO.day.equals(day)
   132             && !TU.day.equals(day)
   133             && !WE.day.equals(day)
   134             && !TH.day.equals(day)
   135             && !FR.day.equals(day)
   136             && !SA.day.equals(day)) {
   137             throw new IllegalArgumentException("Invalid day: " + day);
   138         }
   139     }
   140     /**
   141      * @return Returns the day.
   142      */
   143     public final String getDay() {
   144         return day;
   145     }
   147     /**
   148      * @return Returns the offset.
   149      */
   150     public final int getOffset() {
   151         return offset;
   152     }
   154     /**
   155      * {@inheritDoc}
   156      */
   157     public final String toString() {
   158         final StringBuffer b = new StringBuffer();
   159         if (getOffset() != 0) {
   160             b.append(getOffset());
   161         }
   162         b.append(getDay());
   163         return b.toString();
   164     }
   166     /**
   167      * Returns a weekday representation of the specified calendar.
   168      * @param cal a calendar (java.util)
   169      * @return a weekday instance representing the specified calendar
   170      */
   171     public static final WeekDay getWeekDay(final Calendar cal) {
   172         return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), 0);
   173     }
   175     /**
   176      * Returns a weekday/offset representation of the specified calendar.
   177      * @param cal a calendar (java.util)
   178      * @return a weekday instance representing the specified calendar
   179      */
   180     public static final WeekDay getMonthlyOffset(final Calendar cal) {
   181         return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
   182     }
   184     /**
   185      * Returns a weekday/negative offset representation of the specified calendar.
   186      * @param cal a calendar (java.util)
   187      * @return a weekday instance representing the specified calendar
   188      */
   189     public static final WeekDay getNegativeMonthlyOffset(final Calendar cal) {
   190         return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 6);
   191     }
   193     /**
   194      * Returns the corresponding day constant to the specified
   195      * java.util.Calendar.DAY_OF_WEEK property.
   196      * @param calDay a property value of java.util.Calendar.DAY_OF_WEEK
   197      * @return a string, or null if an invalid DAY_OF_WEEK property is
   198      * specified
   199      */
   200     public static WeekDay getDay(final int calDay) {
   201         WeekDay day = null;
   202         switch (calDay) {
   203             case Calendar.SUNDAY:
   204                 day = SU;
   205                 break;
   206             case Calendar.MONDAY:
   207                 day = MO;
   208                 break;
   209             case Calendar.TUESDAY:
   210                 day = TU;
   211                 break;
   212             case Calendar.WEDNESDAY:
   213                 day = WE;
   214                 break;
   215             case Calendar.THURSDAY:
   216                 day = TH;
   217                 break;
   218             case Calendar.FRIDAY:
   219                 day = FR;
   220                 break;
   221             case Calendar.SATURDAY:
   222                 day = SA;
   223                 break;
   224             default:
   225                 break;
   226         }
   227         return day;
   228     }
   230     /**
   231      * Returns the corresponding <code>java.util.Calendar.DAY_OF_WEEK</code>
   232      * constant for the specified <code>WeekDay</code>.
   233      * @param weekday a week day instance
   234      * @return the corresponding <code>java.util.Calendar</code> day
   235      */
   236     public static int getCalendarDay(final WeekDay weekday) {
   237         int calendarDay = -1;
   238         if (SU.getDay().equals(weekday.getDay())) {
   239             calendarDay = Calendar.SUNDAY;
   240         }
   241         else if (MO.getDay().equals(weekday.getDay())) {
   242             calendarDay = Calendar.MONDAY;
   243         }
   244         else if (TU.getDay().equals(weekday.getDay())) {
   245             calendarDay = Calendar.TUESDAY;
   246         }
   247         else if (WE.getDay().equals(weekday.getDay())) {
   248             calendarDay = Calendar.WEDNESDAY;
   249         }
   250         else if (TH.getDay().equals(weekday.getDay())) {
   251             calendarDay = Calendar.THURSDAY;
   252         }
   253         else if (FR.getDay().equals(weekday.getDay())) {
   254             calendarDay = Calendar.FRIDAY;
   255         }
   256         else if (SA.getDay().equals(weekday.getDay())) {
   257             calendarDay = Calendar.SATURDAY;
   258         }
   259         return calendarDay;
   260     }
   262     /**
   263      * {@inheritDoc}
   264      */
   265     public final boolean equals(final Object arg0) {
   266         if (arg0 == null) {
   267             return false;
   268         }
   269         if (!(arg0 instanceof WeekDay)) {
   270             return false;
   271         }
   272         final WeekDay wd = (WeekDay) arg0;
   273         return ObjectUtils.equals(wd.getDay(), getDay())
   274             && wd.getOffset() == getOffset();
   275     }
   277     /**
   278      * {@inheritDoc}
   279      */
   280     public final int hashCode() {
   281         return new HashCodeBuilder().append(getDay())
   282             .append(getOffset()).toHashCode();
   283     }
   284 }

mercurial