1   /**
2    * Copyright (c) 2000-2008 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.portal.kernel.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   */
65  public class DayAndPosition implements Cloneable, Serializable {
66  
67      /**
68       * Field day
69       */
70      private int day;
71  
72      /**
73       * Field position
74       */
75      private int position;
76  
77      /**
78       * Field NO_WEEKDAY
79       */
80      public final static int NO_WEEKDAY = 0;
81  
82      /**
83       * Constructor DayAndPosition
84       *
85       *
86       */
87      public DayAndPosition() {
88          day = NO_WEEKDAY;
89          position = 0;
90      }
91  
92      /**
93       * Constructor DayAndPosition
94       *
95       *
96       * @param   d
97       * @param   p
98       *
99       */
100     public DayAndPosition(int d, int p) {
101         if (!isValidDayOfWeek(d)) {
102             throw new IllegalArgumentException("Invalid day of week");
103         }
104 
105         if (!isValidDayPosition(p)) {
106             throw new IllegalArgumentException("Invalid day position");
107         }
108 
109         day = d;
110         position = p;
111     }
112 
113     /**
114      * Method getDayOfWeek
115      *
116      *
117      * @return  int
118      *
119      */
120     public int getDayOfWeek() {
121         return day;
122     }
123 
124     /**
125      * Method setDayOfWeek
126      *
127      *
128      * @param   d
129      *
130      */
131     public void setDayOfWeek(int d) {
132         if (!isValidDayOfWeek(d)) {
133             throw new IllegalArgumentException("Invalid day of week");
134         }
135 
136         day = d;
137     }
138 
139     /**
140      * Method getDayPosition
141      *
142      *
143      * @return  int
144      *
145      */
146     public int getDayPosition() {
147         return position;
148     }
149 
150     /**
151      * Method setDayPosition
152      *
153      *
154      * @param   p
155      *
156      */
157     public void setDayPosition(int p) {
158         if (!isValidDayPosition(p)) {
159             throw new IllegalArgumentException();
160         }
161 
162         position = p;
163     }
164 
165     /**
166      * Method equals
167      *
168      *
169      * @param   obj
170      *
171      * @return  boolean
172      *
173      */
174     public boolean equals(Object obj) {
175         if (obj == null) {
176             return false;
177         }
178 
179         if (this == obj) {
180             return true;
181         }
182 
183         if (!(obj instanceof DayAndPosition)) {
184             return false;
185         }
186 
187         DayAndPosition that = (DayAndPosition)obj;
188 
189         return (getDayOfWeek() == that.getDayOfWeek())
190                && (getDayPosition() == that.getDayPosition());
191     }
192 
193     /**
194      * Method isValidDayOfWeek
195      *
196      *
197      * @param   d
198      *
199      * @return  boolean
200      *
201      */
202     public static boolean isValidDayOfWeek(int d) {
203         switch (d) {
204 
205             case NO_WEEKDAY :
206             case Calendar.SUNDAY :
207             case Calendar.MONDAY :
208             case Calendar.TUESDAY :
209             case Calendar.WEDNESDAY :
210             case Calendar.THURSDAY :
211             case Calendar.FRIDAY :
212             case Calendar.SATURDAY :
213                 return true;
214 
215             default :
216                 return false;
217         }
218     }
219 
220     /**
221      * Method isValidDayPosition
222      *
223      *
224      * @param   p
225      *
226      * @return  boolean
227      *
228      */
229     public static boolean isValidDayPosition(int p) {
230         return ((p >= -53) && (p <= 53));
231     }
232 
233     /**
234      * Method clone
235      *
236      *
237      * @return  Object
238      *
239      */
240     public Object clone() {
241         try {
242             DayAndPosition other = (DayAndPosition)super.clone();
243 
244             other.day = day;
245             other.position = position;
246 
247             return other;
248         }
249         catch (CloneNotSupportedException e) {
250             throw new InternalError();
251         }
252     }
253 
254     /**
255      * Method toString
256      *
257      *
258      * @return  String
259      *
260      */
261     public String toString() {
262         StringBuilder sb = new StringBuilder();
263 
264         sb.append(getClass().getName());
265         sb.append("[day=");
266         sb.append(day);
267         sb.append(",position=");
268         sb.append(position);
269         sb.append("]");
270 
271         return sb.toString();
272     }
273 
274 }