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 public static final int NO_WEEKDAY = 0;
064
065
070 public static boolean isValidDayOfWeek(int d) {
071 switch (d) {
072
073 case NO_WEEKDAY :
074 case Calendar.SUNDAY :
075 case Calendar.MONDAY :
076 case Calendar.TUESDAY :
077 case Calendar.WEDNESDAY :
078 case Calendar.THURSDAY :
079 case Calendar.FRIDAY :
080 case Calendar.SATURDAY :
081 return true;
082
083 default :
084 return false;
085 }
086 }
087
088
093 public static boolean isValidDayPosition(int p) {
094 return ((p >= -53) && (p <= 53));
095 }
096
097
100 public DayAndPosition() {
101 _day = NO_WEEKDAY;
102 _position = 0;
103 }
104
105
108 public DayAndPosition(int d, int p) {
109 if (!isValidDayOfWeek(d)) {
110 throw new IllegalArgumentException("Invalid day of week");
111 }
112
113 if (!isValidDayPosition(p)) {
114 throw new IllegalArgumentException("Invalid day position");
115 }
116
117 _day = d;
118 _position = p;
119 }
120
121
126 @Override
127 public Object clone() {
128 try {
129 DayAndPosition other = (DayAndPosition)super.clone();
130
131 other._day = _day;
132 other._position = _position;
133
134 return other;
135 }
136 catch (CloneNotSupportedException cnse) {
137 throw new InternalError();
138 }
139 }
140
141
146 @Override
147 public boolean equals(Object obj) {
148 if (obj == null) {
149 return false;
150 }
151
152 if (this == obj) {
153 return true;
154 }
155
156 if (!(obj instanceof DayAndPosition)) {
157 return false;
158 }
159
160 DayAndPosition that = (DayAndPosition)obj;
161
162 return
163 (getDayOfWeek() == that.getDayOfWeek()) &&
164 (getDayPosition() == that.getDayPosition());
165 }
166
167
172 public int getDayOfWeek() {
173 return _day;
174 }
175
176
181 public int getDayPosition() {
182 return _position;
183 }
184
185 @Override
186 public int hashCode() {
187 HashCode hashCode = HashCodeFactoryUtil.getHashCode();
188
189 hashCode.append(_day);
190 hashCode.append(_position);
191
192 return hashCode.toHashCode();
193 }
194
195
198 public void setDayOfWeek(int d) {
199 if (!isValidDayOfWeek(d)) {
200 throw new IllegalArgumentException("Invalid day of week");
201 }
202
203 _day = d;
204 }
205
206
209 public void setDayPosition(int p) {
210 if (!isValidDayPosition(p)) {
211 throw new IllegalArgumentException();
212 }
213
214 _position = p;
215 }
216
217
222 @Override
223 public String toString() {
224 StringBundler sb = new StringBundler(6);
225
226 sb.append(getClass().getName());
227 sb.append("[day=");
228 sb.append(_day);
229 sb.append(",position=");
230 sb.append(_position);
231 sb.append("]");
232
233 return sb.toString();
234 }
235
236
239 private int _day;
240
241
244 private int _position;
245
246 }