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.

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

mercurial