1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.portal.kernel.cal;
46  
47  import com.liferay.portal.kernel.util.StringBundler;
48  
49  import java.io.Serializable;
50  
51  import java.util.Calendar;
52  
53  /**
54   * <a href="DayAndPosition.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Jonathan Lennox
57   */
58  public class DayAndPosition implements Cloneable, Serializable {
59  
60      /**
61       * Field day
62       */
63      private int day;
64  
65      /**
66       * Field position
67       */
68      private int position;
69  
70      /**
71       * Field NO_WEEKDAY
72       */
73      public final static int NO_WEEKDAY = 0;
74  
75      /**
76       * Constructor DayAndPosition
77       */
78      public DayAndPosition() {
79          day = NO_WEEKDAY;
80          position = 0;
81      }
82  
83      /**
84       * Constructor DayAndPosition
85       */
86      public DayAndPosition(int d, int p) {
87          if (!isValidDayOfWeek(d)) {
88              throw new IllegalArgumentException("Invalid day of week");
89          }
90  
91          if (!isValidDayPosition(p)) {
92              throw new IllegalArgumentException("Invalid day position");
93          }
94  
95          day = d;
96          position = p;
97      }
98  
99      /**
100      * Method getDayOfWeek
101      *
102      * @return int
103      */
104     public int getDayOfWeek() {
105         return day;
106     }
107 
108     /**
109      * Method setDayOfWeek
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      * @return int
123      */
124     public int getDayPosition() {
125         return position;
126     }
127 
128     /**
129      * Method setDayPosition
130      */
131     public void setDayPosition(int p) {
132         if (!isValidDayPosition(p)) {
133             throw new IllegalArgumentException();
134         }
135 
136         position = p;
137     }
138 
139     /**
140      * Method equals
141      *
142      * @return boolean
143      */
144     public boolean equals(Object obj) {
145         if (obj == null) {
146             return false;
147         }
148 
149         if (this == obj) {
150             return true;
151         }
152 
153         if (!(obj instanceof DayAndPosition)) {
154             return false;
155         }
156 
157         DayAndPosition that = (DayAndPosition)obj;
158 
159         return (getDayOfWeek() == that.getDayOfWeek())
160                && (getDayPosition() == that.getDayPosition());
161     }
162 
163     /**
164      * Method isValidDayOfWeek
165      *
166      * @return boolean
167      */
168     public static boolean isValidDayOfWeek(int d) {
169         switch (d) {
170 
171             case NO_WEEKDAY :
172             case Calendar.SUNDAY :
173             case Calendar.MONDAY :
174             case Calendar.TUESDAY :
175             case Calendar.WEDNESDAY :
176             case Calendar.THURSDAY :
177             case Calendar.FRIDAY :
178             case Calendar.SATURDAY :
179                 return true;
180 
181             default :
182                 return false;
183         }
184     }
185 
186     /**
187      * Method isValidDayPosition
188      *
189      * @return boolean
190      */
191     public static boolean isValidDayPosition(int p) {
192         return ((p >= -53) && (p <= 53));
193     }
194 
195     /**
196      * Method clone
197      *
198      * @return Object
199      */
200     public Object clone() {
201         try {
202             DayAndPosition other = (DayAndPosition)super.clone();
203 
204             other.day = day;
205             other.position = position;
206 
207             return other;
208         }
209         catch (CloneNotSupportedException e) {
210             throw new InternalError();
211         }
212     }
213 
214     /**
215      * Method toString
216      *
217      * @return String
218      */
219     public String toString() {
220         StringBundler sb = new StringBundler(6);
221 
222         sb.append(getClass().getName());
223         sb.append("[day=");
224         sb.append(day);
225         sb.append(",position=");
226         sb.append(position);
227         sb.append("]");
228 
229         return sb.toString();
230     }
231 
232 }