1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.HashCode;
48  import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
49  import com.liferay.portal.kernel.util.StringBundler;
50  
51  import java.io.Serializable;
52  
53  import java.util.Calendar;
54  
55  /**
56   * <a href="DayAndPosition.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author     Jonathan Lennox
59   * @deprecated This class has been repackaged at
60   *             <code>com.liferay.portal.kernel.cal</code>.
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 static final int NO_WEEKDAY = 0;
78  
79      /**
80       * Constructor DayAndPosition
81       */
82      public DayAndPosition() {
83          day = NO_WEEKDAY;
84          position = 0;
85      }
86  
87      /**
88       * Constructor DayAndPosition
89       */
90      public DayAndPosition(int d, int p) {
91          if (!isValidDayOfWeek(d)) {
92              throw new IllegalArgumentException("Invalid day of week");
93          }
94  
95          if (!isValidDayPosition(p)) {
96              throw new IllegalArgumentException("Invalid day position");
97          }
98  
99          day = d;
100         position = p;
101     }
102 
103     /**
104      * Method getDayOfWeek
105      *
106      * @return int
107      */
108     public int getDayOfWeek() {
109         return day;
110     }
111 
112     /**
113      * Method setDayOfWeek
114      */
115     public void setDayOfWeek(int d) {
116         if (!isValidDayOfWeek(d)) {
117             throw new IllegalArgumentException("Invalid day of week");
118         }
119 
120         day = d;
121     }
122 
123     /**
124      * Method getDayPosition
125      *
126      * @return int
127      */
128     public int getDayPosition() {
129         return position;
130     }
131 
132     /**
133      * Method setDayPosition
134      */
135     public void setDayPosition(int p) {
136         if (!isValidDayPosition(p)) {
137             throw new IllegalArgumentException();
138         }
139 
140         position = p;
141     }
142 
143     /**
144      * Method equals
145      *
146      * @return boolean
147      */
148     public boolean equals(Object obj) {
149         if (obj == null) {
150             return false;
151         }
152 
153         if (this == obj) {
154             return true;
155         }
156 
157         if (!(obj instanceof DayAndPosition)) {
158             return false;
159         }
160 
161         DayAndPosition that = (DayAndPosition)obj;
162 
163         return (getDayOfWeek() == that.getDayOfWeek())
164                && (getDayPosition() == that.getDayPosition());
165     }
166 
167     public int hashCode() {
168         HashCode hashCode = HashCodeFactoryUtil.getHashCode();
169 
170         hashCode.append(day);
171         hashCode.append(position);
172 
173         return hashCode.toHashCode();
174     }
175 
176     /**
177      * Method isValidDayOfWeek
178      *
179      * @return boolean
180      */
181     public static boolean isValidDayOfWeek(int d) {
182         switch (d) {
183 
184             case NO_WEEKDAY :
185             case Calendar.SUNDAY :
186             case Calendar.MONDAY :
187             case Calendar.TUESDAY :
188             case Calendar.WEDNESDAY :
189             case Calendar.THURSDAY :
190             case Calendar.FRIDAY :
191             case Calendar.SATURDAY :
192                 return true;
193 
194             default :
195                 return false;
196         }
197     }
198 
199     /**
200      * Method isValidDayPosition
201      *
202      * @return boolean
203      */
204     public static boolean isValidDayPosition(int p) {
205         return ((p >= -53) && (p <= 53));
206     }
207 
208     /**
209      * Method clone
210      *
211      * @return Object
212      */
213     public Object clone() {
214         try {
215             DayAndPosition other = (DayAndPosition)super.clone();
216 
217             other.day = day;
218             other.position = position;
219 
220             return other;
221         }
222         catch (CloneNotSupportedException e) {
223             throw new InternalError();
224         }
225     }
226 
227     /**
228      * Method toString
229      *
230      * @return String
231      */
232     public String toString() {
233         StringBundler sb = new StringBundler(6);
234 
235         sb.append(getClass().getName());
236         sb.append("[day=");
237         sb.append(day);
238         sb.append(",position=");
239         sb.append(position);
240         sb.append("]");
241 
242         return sb.toString();
243     }
244 
245 }