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

changeset 0
fb9019fb1bf7
child 4
45d57ecba757
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/WeekDay.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012, Ben Fortuna
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *
    1.12 + *  o Redistributions of source code must retain the above copyright
    1.13 + * notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *  o Redistributions in binary form must reproduce the above copyright
    1.16 + * notice, this list of conditions and the following disclaimer in the
    1.17 + * documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 + * may be used to endorse or promote products derived from this software
    1.21 + * without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +package net.fortuna.ical4j.model;
    1.36 +
    1.37 +import java.io.Serializable;
    1.38 +import java.util.Calendar;
    1.39 +
    1.40 +import net.fortuna.ical4j.util.Numbers;
    1.41 +
    1.42 +import org.apache.commons.lang.ObjectUtils;
    1.43 +import org.apache.commons.lang.builder.HashCodeBuilder;
    1.44 +
    1.45 +/**
    1.46 + * $Id$
    1.47 + * 
    1.48 + * Created: 19/12/2004
    1.49 + *
    1.50 + * Defines a day of the week with a possible offset related to
    1.51 + * a MONTHLY or YEARLY occurrence.
    1.52 + * 
    1.53 + * @author Ben Fortuna
    1.54 + */
    1.55 +public class WeekDay implements Serializable {
    1.56 +    
    1.57 +    private static final long serialVersionUID = -4412000990022011469L;
    1.58 +
    1.59 +    /**
    1.60 +     * Sunday.
    1.61 +     */
    1.62 +    public static final WeekDay SU = new WeekDay("SU", 0);
    1.63 +
    1.64 +    /**
    1.65 +     * Monday.
    1.66 +     */
    1.67 +    public static final WeekDay MO = new WeekDay("MO", 0);
    1.68 +
    1.69 +    /**
    1.70 +     * Tuesday.
    1.71 +     */
    1.72 +    public static final WeekDay TU = new WeekDay("TU", 0);
    1.73 +
    1.74 +    /**
    1.75 +     * Wednesday.
    1.76 +     */
    1.77 +    public static final WeekDay WE = new WeekDay("WE", 0);
    1.78 +
    1.79 +    /**
    1.80 +     * Thursday.
    1.81 +     */
    1.82 +    public static final WeekDay TH = new WeekDay("TH", 0);
    1.83 +
    1.84 +    /**
    1.85 +     * Friday.
    1.86 +     */
    1.87 +    public static final WeekDay FR = new WeekDay("FR", 0);
    1.88 +
    1.89 +    /**
    1.90 +     * Saturday.
    1.91 +     */
    1.92 +    public static final WeekDay SA = new WeekDay("SA", 0);
    1.93 +
    1.94 +    private String day;
    1.95 +    
    1.96 +    private int offset;
    1.97 +    
    1.98 +    /**
    1.99 +     * @param value a string representation of a week day
   1.100 +     */
   1.101 +    public WeekDay(final String value) {
   1.102 +        if (value.length() > 2) {
   1.103 +            offset = Numbers.parseInt(value.substring(0, value.length() - 2));
   1.104 +        }
   1.105 +        else {
   1.106 +            offset = 0;
   1.107 +        }
   1.108 +        day = value.substring(value.length() - 2);
   1.109 +        validateDay();
   1.110 +    }
   1.111 +    
   1.112 +    /**
   1.113 +     * @param day a string representation of a week day
   1.114 +     * @param offset a month offset value
   1.115 +     */
   1.116 +    private WeekDay(final String day, final int offset) {
   1.117 +        this.day = day;
   1.118 +        this.offset = offset;
   1.119 +    }
   1.120 +    
   1.121 +    /**
   1.122 +     * Constructs a new weekday instance based on the specified
   1.123 +     * instance and offset.
   1.124 +     * @param weekDay a week day template for the instance
   1.125 +     * @param offset a month offset value
   1.126 +     */
   1.127 +    public WeekDay(final WeekDay weekDay, final int offset) {
   1.128 +        this.day = weekDay.getDay();
   1.129 +        this.offset = offset;
   1.130 +    }
   1.131 +    
   1.132 +    private void validateDay() {
   1.133 +        if (!SU.day.equals(day)
   1.134 +            && !MO.day.equals(day)
   1.135 +            && !TU.day.equals(day)
   1.136 +            && !WE.day.equals(day)
   1.137 +            && !TH.day.equals(day)
   1.138 +            && !FR.day.equals(day)
   1.139 +            && !SA.day.equals(day)) {
   1.140 +            throw new IllegalArgumentException("Invalid day: " + day);
   1.141 +        }
   1.142 +    }
   1.143 +    /**
   1.144 +     * @return Returns the day.
   1.145 +     */
   1.146 +    public final String getDay() {
   1.147 +        return day;
   1.148 +    }
   1.149 +    
   1.150 +    /**
   1.151 +     * @return Returns the offset.
   1.152 +     */
   1.153 +    public final int getOffset() {
   1.154 +        return offset;
   1.155 +    }
   1.156 +        
   1.157 +    /**
   1.158 +     * {@inheritDoc}
   1.159 +     */
   1.160 +    public final String toString() {
   1.161 +        final StringBuffer b = new StringBuffer();
   1.162 +        if (getOffset() != 0) {
   1.163 +            b.append(getOffset());
   1.164 +        }
   1.165 +        b.append(getDay());
   1.166 +        return b.toString();
   1.167 +    }
   1.168 +    
   1.169 +    /**
   1.170 +     * Returns a weekday representation of the specified calendar.
   1.171 +     * @param cal a calendar (java.util)
   1.172 +     * @return a weekday instance representing the specified calendar
   1.173 +     */
   1.174 +    public static final WeekDay getWeekDay(final Calendar cal) {
   1.175 +        return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), 0);
   1.176 +    }
   1.177 +    
   1.178 +    /**
   1.179 +     * Returns a weekday/offset representation of the specified calendar.
   1.180 +     * @param cal a calendar (java.util)
   1.181 +     * @return a weekday instance representing the specified calendar
   1.182 +     */
   1.183 +    public static final WeekDay getMonthlyOffset(final Calendar cal) {
   1.184 +        return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
   1.185 +    }
   1.186 +    
   1.187 +    /**
   1.188 +     * Returns a weekday/negative offset representation of the specified calendar.
   1.189 +     * @param cal a calendar (java.util)
   1.190 +     * @return a weekday instance representing the specified calendar
   1.191 +     */
   1.192 +    public static final WeekDay getNegativeMonthlyOffset(final Calendar cal) {
   1.193 +        return new WeekDay(getDay(cal.get(Calendar.DAY_OF_WEEK)), cal.get(Calendar.DAY_OF_WEEK_IN_MONTH) - 6);
   1.194 +    }
   1.195 +    
   1.196 +    /**
   1.197 +     * Returns the corresponding day constant to the specified
   1.198 +     * java.util.Calendar.DAY_OF_WEEK property.
   1.199 +     * @param calDay a property value of java.util.Calendar.DAY_OF_WEEK
   1.200 +     * @return a string, or null if an invalid DAY_OF_WEEK property is
   1.201 +     * specified
   1.202 +     */
   1.203 +    public static WeekDay getDay(final int calDay) {
   1.204 +        WeekDay day = null;
   1.205 +        switch (calDay) {
   1.206 +            case Calendar.SUNDAY:
   1.207 +                day = SU;
   1.208 +                break;
   1.209 +            case Calendar.MONDAY:
   1.210 +                day = MO;
   1.211 +                break;
   1.212 +            case Calendar.TUESDAY:
   1.213 +                day = TU;
   1.214 +                break;
   1.215 +            case Calendar.WEDNESDAY:
   1.216 +                day = WE;
   1.217 +                break;
   1.218 +            case Calendar.THURSDAY:
   1.219 +                day = TH;
   1.220 +                break;
   1.221 +            case Calendar.FRIDAY:
   1.222 +                day = FR;
   1.223 +                break;
   1.224 +            case Calendar.SATURDAY:
   1.225 +                day = SA;
   1.226 +                break;
   1.227 +            default:
   1.228 +                break;
   1.229 +        }
   1.230 +        return day;
   1.231 +    }
   1.232 +    
   1.233 +    /**
   1.234 +     * Returns the corresponding <code>java.util.Calendar.DAY_OF_WEEK</code>
   1.235 +     * constant for the specified <code>WeekDay</code>.
   1.236 +     * @param weekday a week day instance
   1.237 +     * @return the corresponding <code>java.util.Calendar</code> day
   1.238 +     */
   1.239 +    public static int getCalendarDay(final WeekDay weekday) {
   1.240 +        int calendarDay = -1;
   1.241 +        if (SU.getDay().equals(weekday.getDay())) {
   1.242 +            calendarDay = Calendar.SUNDAY;
   1.243 +        }
   1.244 +        else if (MO.getDay().equals(weekday.getDay())) {
   1.245 +            calendarDay = Calendar.MONDAY;
   1.246 +        }
   1.247 +        else if (TU.getDay().equals(weekday.getDay())) {
   1.248 +            calendarDay = Calendar.TUESDAY;
   1.249 +        }
   1.250 +        else if (WE.getDay().equals(weekday.getDay())) {
   1.251 +            calendarDay = Calendar.WEDNESDAY;
   1.252 +        }
   1.253 +        else if (TH.getDay().equals(weekday.getDay())) {
   1.254 +            calendarDay = Calendar.THURSDAY;
   1.255 +        }
   1.256 +        else if (FR.getDay().equals(weekday.getDay())) {
   1.257 +            calendarDay = Calendar.FRIDAY;
   1.258 +        }
   1.259 +        else if (SA.getDay().equals(weekday.getDay())) {
   1.260 +            calendarDay = Calendar.SATURDAY;
   1.261 +        }
   1.262 +        return calendarDay;
   1.263 +    }
   1.264 +    
   1.265 +    /**
   1.266 +     * {@inheritDoc}
   1.267 +     */
   1.268 +    public final boolean equals(final Object arg0) {
   1.269 +        if (arg0 == null) {
   1.270 +            return false;
   1.271 +        }
   1.272 +        if (!(arg0 instanceof WeekDay)) {
   1.273 +            return false;
   1.274 +        }
   1.275 +        final WeekDay wd = (WeekDay) arg0;
   1.276 +        return ObjectUtils.equals(wd.getDay(), getDay())
   1.277 +            && wd.getOffset() == getOffset();
   1.278 +    }
   1.279 +    
   1.280 +    /**
   1.281 +     * {@inheritDoc}
   1.282 +     */
   1.283 +    public final int hashCode() {
   1.284 +        return new HashCodeBuilder().append(getDay())
   1.285 +            .append(getOffset()).toHashCode();
   1.286 +    }
   1.287 +}

mercurial