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