| DayAndPosition.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 */
14
15 /*
16 * Copyright (c) 2000, Columbia University. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are met:
20 *
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 *
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * 3. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
33 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
39 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 package com.liferay.util.cal;
46
47 import com.liferay.portal.kernel.util.StringBundler;
48
49 import java.io.Serializable;
50
51 import java.util.Calendar;
52
53 /**
54 * <a href="DayAndPosition.java.html"><b><i>View Source</i></b></a>
55 *
56 * @author Jonathan Lennox
57 * @deprecated This class has been repackaged at
58 * <code>com.liferay.portal.kernel.cal</code>.
59 */
60 public class DayAndPosition implements Cloneable, Serializable {
61
62 /**
63 * Field day
64 */
65 private int day;
66
67 /**
68 * Field position
69 */
70 private int position;
71
72 /**
73 * Field NO_WEEKDAY
74 */
75 public final static int NO_WEEKDAY = 0;
76
77 /**
78 * Constructor DayAndPosition
79 */
80 public DayAndPosition() {
81 day = NO_WEEKDAY;
82 position = 0;
83 }
84
85 /**
86 * Constructor DayAndPosition
87 */
88 public DayAndPosition(int d, int p) {
89 if (!isValidDayOfWeek(d)) {
90 throw new IllegalArgumentException("Invalid day of week");
91 }
92
93 if (!isValidDayPosition(p)) {
94 throw new IllegalArgumentException("Invalid day position");
95 }
96
97 day = d;
98 position = p;
99 }
100
101 /**
102 * Method getDayOfWeek
103 *
104 * @return int
105 */
106 public int getDayOfWeek() {
107 return day;
108 }
109
110 /**
111 * Method setDayOfWeek
112 */
113 public void setDayOfWeek(int d) {
114 if (!isValidDayOfWeek(d)) {
115 throw new IllegalArgumentException("Invalid day of week");
116 }
117
118 day = d;
119 }
120
121 /**
122 * Method getDayPosition
123 *
124 * @return int
125 */
126 public int getDayPosition() {
127 return position;
128 }
129
130 /**
131 * Method setDayPosition
132 */
133 public void setDayPosition(int p) {
134 if (!isValidDayPosition(p)) {
135 throw new IllegalArgumentException();
136 }
137
138 position = p;
139 }
140
141 /**
142 * Method equals
143 *
144 * @return boolean
145 */
146 public boolean equals(Object obj) {
147 if (obj == null) {
148 return false;
149 }
150
151 if (this == obj) {
152 return true;
153 }
154
155 if (!(obj instanceof DayAndPosition)) {
156 return false;
157 }
158
159 DayAndPosition that = (DayAndPosition)obj;
160
161 return (getDayOfWeek() == that.getDayOfWeek())
162 && (getDayPosition() == that.getDayPosition());
163 }
164
165 /**
166 * Method isValidDayOfWeek
167 *
168 * @return boolean
169 */
170 public static boolean isValidDayOfWeek(int d) {
171 switch (d) {
172
173 case NO_WEEKDAY :
174 case Calendar.SUNDAY :
175 case Calendar.MONDAY :
176 case Calendar.TUESDAY :
177 case Calendar.WEDNESDAY :
178 case Calendar.THURSDAY :
179 case Calendar.FRIDAY :
180 case Calendar.SATURDAY :
181 return true;
182
183 default :
184 return false;
185 }
186 }
187
188 /**
189 * Method isValidDayPosition
190 *
191 * @return boolean
192 */
193 public static boolean isValidDayPosition(int p) {
194 return ((p >= -53) && (p <= 53));
195 }
196
197 /**
198 * Method clone
199 *
200 * @return Object
201 */
202 public Object clone() {
203 try {
204 DayAndPosition other = (DayAndPosition)super.clone();
205
206 other.day = day;
207 other.position = position;
208
209 return other;
210 }
211 catch (CloneNotSupportedException e) {
212 throw new InternalError();
213 }
214 }
215
216 /**
217 * Method toString
218 *
219 * @return String
220 */
221 public String toString() {
222 StringBundler sb = new StringBundler(6);
223
224 sb.append(getClass().getName());
225 sb.append("[day=");
226 sb.append(day);
227 sb.append(",position=");
228 sb.append(position);
229 sb.append("]");
230
231 return sb.toString();
232 }
233
234 }