1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DayAndPosition implements Cloneable, Serializable {
65  
66      /**
67       * Field day
68       */
69      private int day;
70  
71      /**
72       * Field position
73       */
74      private int position;
75  
76      /**
77       * Field NO_WEEKDAY
78       */
79      public final static int NO_WEEKDAY = 0;
80  
81      /**
82       * Constructor DayAndPosition
83       */
84      public DayAndPosition() {
85          day = NO_WEEKDAY;
86          position = 0;
87      }
88  
89      /**
90       * Constructor DayAndPosition
91       */
92      public DayAndPosition(int d, int p) {
93          if (!isValidDayOfWeek(d)) {
94              throw new IllegalArgumentException("Invalid day of week");
95          }
96  
97          if (!isValidDayPosition(p)) {
98              throw new IllegalArgumentException("Invalid day position");
99          }
100 
101         day = d;
102         position = p;
103     }
104 
105     /**
106      * Method getDayOfWeek
107      *
108      * @return int
109      */
110     public int getDayOfWeek() {
111         return day;
112     }
113 
114     /**
115      * Method setDayOfWeek
116      */
117     public void setDayOfWeek(int d) {
118         if (!isValidDayOfWeek(d)) {
119             throw new IllegalArgumentException("Invalid day of week");
120         }
121 
122         day = d;
123     }
124 
125     /**
126      * Method getDayPosition
127      *
128      * @return int
129      */
130     public int getDayPosition() {
131         return position;
132     }
133 
134     /**
135      * Method setDayPosition
136      */
137     public void setDayPosition(int p) {
138         if (!isValidDayPosition(p)) {
139             throw new IllegalArgumentException();
140         }
141 
142         position = p;
143     }
144 
145     /**
146      * Method equals
147      *
148      * @return boolean
149      */
150     public boolean equals(Object obj) {
151         if (obj == null) {
152             return false;
153         }
154 
155         if (this == obj) {
156             return true;
157         }
158 
159         if (!(obj instanceof DayAndPosition)) {
160             return false;
161         }
162 
163         DayAndPosition that = (DayAndPosition)obj;
164 
165         return (getDayOfWeek() == that.getDayOfWeek())
166                && (getDayPosition() == that.getDayPosition());
167     }
168 
169     /**
170      * Method isValidDayOfWeek
171      *
172      * @return boolean
173      */
174     public static boolean isValidDayOfWeek(int d) {
175         switch (d) {
176 
177             case NO_WEEKDAY :
178             case Calendar.SUNDAY :
179             case Calendar.MONDAY :
180             case Calendar.TUESDAY :
181             case Calendar.WEDNESDAY :
182             case Calendar.THURSDAY :
183             case Calendar.FRIDAY :
184             case Calendar.SATURDAY :
185                 return true;
186 
187             default :
188                 return false;
189         }
190     }
191 
192     /**
193      * Method isValidDayPosition
194      *
195      * @return boolean
196      */
197     public static boolean isValidDayPosition(int p) {
198         return ((p >= -53) && (p <= 53));
199     }
200 
201     /**
202      * Method clone
203      *
204      * @return Object
205      */
206     public Object clone() {
207         try {
208             DayAndPosition other = (DayAndPosition)super.clone();
209 
210             other.day = day;
211             other.position = position;
212 
213             return other;
214         }
215         catch (CloneNotSupportedException e) {
216             throw new InternalError();
217         }
218     }
219 
220     /**
221      * Method toString
222      *
223      * @return String
224      */
225     public String toString() {
226         StringBuilder sb = new StringBuilder();
227 
228         sb.append(getClass().getName());
229         sb.append("[day=");
230         sb.append(day);
231         sb.append(",position=");
232         sb.append(position);
233         sb.append("]");
234 
235         return sb.toString();
236     }
237 
238 }