001
014
015
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
058 public class DayAndPosition implements Cloneable, Serializable {
059
060
063 private int day;
064
065
068 private int position;
069
070
073 public static final int NO_WEEKDAY = 0;
074
075
078 public DayAndPosition() {
079 day = NO_WEEKDAY;
080 position = 0;
081 }
082
083
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
104 public int getDayOfWeek() {
105 return day;
106 }
107
108
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
124 public int getDayPosition() {
125 return position;
126 }
127
128
131 public void setDayPosition(int p) {
132 if (!isValidDayPosition(p)) {
133 throw new IllegalArgumentException();
134 }
135
136 position = p;
137 }
138
139
144 @Override
145 public boolean equals(Object obj) {
146 if (obj == null) {
147 return false;
148 }
149
150 if (this == obj) {
151 return true;
152 }
153
154 if (!(obj instanceof DayAndPosition)) {
155 return false;
156 }
157
158 DayAndPosition that = (DayAndPosition)obj;
159
160 return (getDayOfWeek() == that.getDayOfWeek())
161 && (getDayPosition() == that.getDayPosition());
162 }
163
164 @Override
165 public int hashCode() {
166 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
167
168 hashCode.append(day);
169 hashCode.append(position);
170
171 return hashCode.toHashCode();
172 }
173
174
179 public static boolean isValidDayOfWeek(int d) {
180 switch (d) {
181
182 case NO_WEEKDAY :
183 case Calendar.SUNDAY :
184 case Calendar.MONDAY :
185 case Calendar.TUESDAY :
186 case Calendar.WEDNESDAY :
187 case Calendar.THURSDAY :
188 case Calendar.FRIDAY :
189 case Calendar.SATURDAY :
190 return true;
191
192 default :
193 return false;
194 }
195 }
196
197
202 public static boolean isValidDayPosition(int p) {
203 return ((p >= -53) && (p <= 53));
204 }
205
206
211 @Override
212 public Object clone() {
213 try {
214 DayAndPosition other = (DayAndPosition)super.clone();
215
216 other.day = day;
217 other.position = position;
218
219 return other;
220 }
221 catch (CloneNotSupportedException e) {
222 throw new InternalError();
223 }
224 }
225
226
231 @Override
232 public String toString() {
233 StringBundler sb = new StringBundler(6);
234
235 sb.append(getClass().getName());
236 sb.append("[day=");
237 sb.append(day);
238 sb.append(",position=");
239 sb.append(position);
240 sb.append("]");
241
242 return sb.toString();
243 }
244
245 }