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