001 /** 002 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. 003 * 004 * This library is free software; you can redistribute it and/or modify it under 005 * the terms of the GNU Lesser General Public License as published by the Free 006 * Software Foundation; either version 2.1 of the License, or (at your option) 007 * any later version. 008 * 009 * This library is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 011 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 012 * details. 013 */ 014 015 /* 016 * Copyright (c) 2000, Columbia University. All rights reserved. 017 * 018 * Redistribution and use in source and binary forms, with or without 019 * modification, are permitted provided that the following conditions are met: 020 * 021 * 1. Redistributions of source code must retain the above copyright 022 * notice, this list of conditions and the following disclaimer. 023 * 024 * 2. Redistributions in binary form must reproduce the above copyright 025 * notice, this list of conditions and the following disclaimer in the 026 * documentation and/or other materials provided with the distribution. 027 * 028 * 3. Neither the name of the University nor the names of its contributors 029 * may be used to endorse or promote products derived from this software 030 * without specific prior written permission. 031 * 032 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS 033 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 034 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 035 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR 036 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 037 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 038 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 039 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 040 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 041 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 042 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 043 */ 044 045 package com.liferay.portal.kernel.cal; 046 047 import com.liferay.portal.kernel.util.HashCode; 048 import com.liferay.portal.kernel.util.HashCodeFactoryUtil; 049 import com.liferay.portal.kernel.util.StringBundler; 050 051 import java.io.Serializable; 052 053 import java.util.Calendar; 054 055 /** 056 * @author Jonathan Lennox 057 */ 058 public class DayAndPosition implements Cloneable, Serializable { 059 060 /** 061 * Field day 062 */ 063 private int day; 064 065 /** 066 * Field position 067 */ 068 private int position; 069 070 /** 071 * Field NO_WEEKDAY 072 */ 073 public static final int NO_WEEKDAY = 0; 074 075 /** 076 * Constructor DayAndPosition 077 */ 078 public DayAndPosition() { 079 day = NO_WEEKDAY; 080 position = 0; 081 } 082 083 /** 084 * Constructor DayAndPosition 085 */ 086 public DayAndPosition(int d, int p) { 087 if (!isValidDayOfWeek(d)) { 088 throw new IllegalArgumentException("Invalid day of week"); 089 } 090 091 if (!isValidDayPosition(p)) { 092 throw new IllegalArgumentException("Invalid day position"); 093 } 094 095 day = d; 096 position = p; 097 } 098 099 /** 100 * Method getDayOfWeek 101 * 102 * @return int 103 */ 104 public int getDayOfWeek() { 105 return day; 106 } 107 108 /** 109 * Method setDayOfWeek 110 */ 111 public void setDayOfWeek(int d) { 112 if (!isValidDayOfWeek(d)) { 113 throw new IllegalArgumentException("Invalid day of week"); 114 } 115 116 day = d; 117 } 118 119 /** 120 * Method getDayPosition 121 * 122 * @return int 123 */ 124 public int getDayPosition() { 125 return position; 126 } 127 128 /** 129 * Method setDayPosition 130 */ 131 public void setDayPosition(int p) { 132 if (!isValidDayPosition(p)) { 133 throw new IllegalArgumentException(); 134 } 135 136 position = p; 137 } 138 139 /** 140 * Method equals 141 * 142 * @return boolean 143 */ 144 public boolean equals(Object obj) { 145 if (obj == null) { 146 return false; 147 } 148 149 if (this == obj) { 150 return true; 151 } 152 153 if (!(obj instanceof DayAndPosition)) { 154 return false; 155 } 156 157 DayAndPosition that = (DayAndPosition)obj; 158 159 return (getDayOfWeek() == that.getDayOfWeek()) 160 && (getDayPosition() == that.getDayPosition()); 161 } 162 163 public int hashCode() { 164 HashCode hashCode = HashCodeFactoryUtil.getHashCode(); 165 166 hashCode.append(day); 167 hashCode.append(position); 168 169 return hashCode.toHashCode(); 170 } 171 172 /** 173 * Method isValidDayOfWeek 174 * 175 * @return boolean 176 */ 177 public static boolean isValidDayOfWeek(int d) { 178 switch (d) { 179 180 case NO_WEEKDAY : 181 case Calendar.SUNDAY : 182 case Calendar.MONDAY : 183 case Calendar.TUESDAY : 184 case Calendar.WEDNESDAY : 185 case Calendar.THURSDAY : 186 case Calendar.FRIDAY : 187 case Calendar.SATURDAY : 188 return true; 189 190 default : 191 return false; 192 } 193 } 194 195 /** 196 * Method isValidDayPosition 197 * 198 * @return boolean 199 */ 200 public static boolean isValidDayPosition(int p) { 201 return ((p >= -53) && (p <= 53)); 202 } 203 204 /** 205 * Method clone 206 * 207 * @return Object 208 */ 209 public Object clone() { 210 try { 211 DayAndPosition other = (DayAndPosition)super.clone(); 212 213 other.day = day; 214 other.position = position; 215 216 return other; 217 } 218 catch (CloneNotSupportedException e) { 219 throw new InternalError(); 220 } 221 } 222 223 /** 224 * Method toString 225 * 226 * @return String 227 */ 228 public String toString() { 229 StringBundler sb = new StringBundler(6); 230 231 sb.append(getClass().getName()); 232 sb.append("[day="); 233 sb.append(day); 234 sb.append(",position="); 235 sb.append(position); 236 sb.append("]"); 237 238 return sb.toString(); 239 } 240 241 }