| DayAndPosition.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 /*
24 * Copyright (c) 2000, Columbia University. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions are met:
28 *
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 *
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 *
36 * 3. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
41 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
42 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
44 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
47 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
48 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
50 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 */
52
53 package com.liferay.util.cal;
54
55 import java.io.Serializable;
56
57 import java.util.Calendar;
58
59 /**
60 * <a href="DayAndPosition.java.html"><b><i>View Source</i></b></a>
61 *
62 * @author Jonathan Lennox
63 *
64 * @deprecated This class has been repackaged at
65 * <code>com.liferay.portal.kernel.cal</code>.
66 *
67 */
68 public class DayAndPosition implements Cloneable, Serializable {
69
70 /**
71 * Field day
72 */
73 private int day;
74
75 /**
76 * Field position
77 */
78 private int position;
79
80 /**
81 * Field NO_WEEKDAY
82 */
83 public final static int NO_WEEKDAY = 0;
84
85 /**
86 * Constructor DayAndPosition
87 *
88 *
89 */
90 public DayAndPosition() {
91 day = NO_WEEKDAY;
92 position = 0;
93 }
94
95 /**
96 * Constructor DayAndPosition
97 *
98 *
99 * @param d
100 * @param p
101 *
102 */
103 public DayAndPosition(int d, int p) {
104 if (!isValidDayOfWeek(d)) {
105 throw new IllegalArgumentException("Invalid day of week");
106 }
107
108 if (!isValidDayPosition(p)) {
109 throw new IllegalArgumentException("Invalid day position");
110 }
111
112 day = d;
113 position = p;
114 }
115
116 /**
117 * Method getDayOfWeek
118 *
119 *
120 * @return int
121 *
122 */
123 public int getDayOfWeek() {
124 return day;
125 }
126
127 /**
128 * Method setDayOfWeek
129 *
130 *
131 * @param d
132 *
133 */
134 public void setDayOfWeek(int d) {
135 if (!isValidDayOfWeek(d)) {
136 throw new IllegalArgumentException("Invalid day of week");
137 }
138
139 day = d;
140 }
141
142 /**
143 * Method getDayPosition
144 *
145 *
146 * @return int
147 *
148 */
149 public int getDayPosition() {
150 return position;
151 }
152
153 /**
154 * Method setDayPosition
155 *
156 *
157 * @param p
158 *
159 */
160 public void setDayPosition(int p) {
161 if (!isValidDayPosition(p)) {
162 throw new IllegalArgumentException();
163 }
164
165 position = p;
166 }
167
168 /**
169 * Method equals
170 *
171 *
172 * @param obj
173 *
174 * @return boolean
175 *
176 */
177 public boolean equals(Object obj) {
178 if (obj == null) {
179 return false;
180 }
181
182 if (this == obj) {
183 return true;
184 }
185
186 if (!(obj instanceof DayAndPosition)) {
187 return false;
188 }
189
190 DayAndPosition that = (DayAndPosition)obj;
191
192 return (getDayOfWeek() == that.getDayOfWeek())
193 && (getDayPosition() == that.getDayPosition());
194 }
195
196 /**
197 * Method isValidDayOfWeek
198 *
199 *
200 * @param d
201 *
202 * @return boolean
203 *
204 */
205 public static boolean isValidDayOfWeek(int d) {
206 switch (d) {
207
208 case NO_WEEKDAY :
209 case Calendar.SUNDAY :
210 case Calendar.MONDAY :
211 case Calendar.TUESDAY :
212 case Calendar.WEDNESDAY :
213 case Calendar.THURSDAY :
214 case Calendar.FRIDAY :
215 case Calendar.SATURDAY :
216 return true;
217
218 default :
219 return false;
220 }
221 }
222
223 /**
224 * Method isValidDayPosition
225 *
226 *
227 * @param p
228 *
229 * @return boolean
230 *
231 */
232 public static boolean isValidDayPosition(int p) {
233 return ((p >= -53) && (p <= 53));
234 }
235
236 /**
237 * Method clone
238 *
239 *
240 * @return Object
241 *
242 */
243 public Object clone() {
244 try {
245 DayAndPosition other = (DayAndPosition)super.clone();
246
247 other.day = day;
248 other.position = position;
249
250 return other;
251 }
252 catch (CloneNotSupportedException e) {
253 throw new InternalError();
254 }
255 }
256
257 /**
258 * Method toString
259 *
260 *
261 * @return String
262 *
263 */
264 public String toString() {
265 StringBuilder sb = new StringBuilder();
266
267 sb.append(getClass().getName());
268 sb.append("[day=");
269 sb.append(day);
270 sb.append(",position=");
271 sb.append(position);
272 sb.append("]");
273
274 return sb.toString();
275 }
276
277 }