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