001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    /*
016     * Copyright (c) 2000, Columbia University.  All rights reserved.
017     *
018     * Redistribution and use in source and binary forms, with or without
019     * modification, are permitted provided that the following conditions are met:
020     *
021     * 1. Redistributions of source code must retain the above copyright
022     *        notice, this list of conditions and the following disclaimer.
023     *
024     * 2. Redistributions in binary form must reproduce the above copyright
025     *        notice, this list of conditions and the following disclaimer in the
026     *        documentation and/or other materials provided with the distribution.
027     *
028     * 3. Neither the name of the University nor the names of its contributors
029     *        may be used to endorse or promote products derived from this software
030     *        without specific prior written permission.
031     *
032     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
033     * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
034     * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
035     * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
036     * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
037     * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
038     * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
039     * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
040     * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
041     * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
042     * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
043     */
044    
045    package com.liferay.util.cal;
046    
047    import com.liferay.portal.kernel.util.HashCode;
048    import com.liferay.portal.kernel.util.HashCodeFactoryUtil;
049    import com.liferay.portal.kernel.util.StringBundler;
050    
051    import java.io.Serializable;
052    
053    import java.util.Calendar;
054    
055    /**
056     * @author     Jonathan Lennox
057     * @deprecated Moved to {@link com.liferay.portal.kernel.cal.DayAndPosition}
058     */
059    public class DayAndPosition implements Cloneable, Serializable {
060    
061            /**
062             * Field day
063             */
064            private int day;
065    
066            /**
067             * Field position
068             */
069            private int position;
070    
071            /**
072             * Field NO_WEEKDAY
073             */
074            public static final int NO_WEEKDAY = 0;
075    
076            /**
077             * Constructor DayAndPosition
078             */
079            public DayAndPosition() {
080                    day = NO_WEEKDAY;
081                    position = 0;
082            }
083    
084            /**
085             * Constructor DayAndPosition
086             */
087            public DayAndPosition(int d, int p) {
088                    if (!isValidDayOfWeek(d)) {
089                            throw new IllegalArgumentException("Invalid day of week");
090                    }
091    
092                    if (!isValidDayPosition(p)) {
093                            throw new IllegalArgumentException("Invalid day position");
094                    }
095    
096                    day = d;
097                    position = p;
098            }
099    
100            /**
101             * Method getDayOfWeek
102             *
103             * @return int
104             */
105            public int getDayOfWeek() {
106                    return day;
107            }
108    
109            /**
110             * Method setDayOfWeek
111             */
112            public void setDayOfWeek(int d) {
113                    if (!isValidDayOfWeek(d)) {
114                            throw new IllegalArgumentException("Invalid day of week");
115                    }
116    
117                    day = d;
118            }
119    
120            /**
121             * Method getDayPosition
122             *
123             * @return int
124             */
125            public int getDayPosition() {
126                    return position;
127            }
128    
129            /**
130             * Method setDayPosition
131             */
132            public void setDayPosition(int p) {
133                    if (!isValidDayPosition(p)) {
134                            throw new IllegalArgumentException();
135                    }
136    
137                    position = p;
138            }
139    
140            /**
141             * Method equals
142             *
143             * @return boolean
144             */
145            @Override
146            public boolean equals(Object obj) {
147                    if (obj == null) {
148                            return false;
149                    }
150    
151                    if (this == obj) {
152                            return true;
153                    }
154    
155                    if (!(obj instanceof DayAndPosition)) {
156                            return false;
157                    }
158    
159                    DayAndPosition that = (DayAndPosition)obj;
160    
161                    return (getDayOfWeek() == that.getDayOfWeek())
162                               && (getDayPosition() == that.getDayPosition());
163            }
164    
165            @Override
166            public int hashCode() {
167                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
168    
169                    hashCode.append(day);
170                    hashCode.append(position);
171    
172                    return hashCode.toHashCode();
173            }
174    
175            /**
176             * Method isValidDayOfWeek
177             *
178             * @return boolean
179             */
180            public static boolean isValidDayOfWeek(int d) {
181                    switch (d) {
182    
183                            case NO_WEEKDAY :
184                            case Calendar.SUNDAY :
185                            case Calendar.MONDAY :
186                            case Calendar.TUESDAY :
187                            case Calendar.WEDNESDAY :
188                            case Calendar.THURSDAY :
189                            case Calendar.FRIDAY :
190                            case Calendar.SATURDAY :
191                                    return true;
192    
193                            default :
194                                    return false;
195                    }
196            }
197    
198            /**
199             * Method isValidDayPosition
200             *
201             * @return boolean
202             */
203            public static boolean isValidDayPosition(int p) {
204                    return ((p >= -53) && (p <= 53));
205            }
206    
207            /**
208             * Method clone
209             *
210             * @return Object
211             */
212            @Override
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            @Override
233            public String toString() {
234                    StringBundler sb = new StringBundler(6);
235    
236                    sb.append(getClass().getName());
237                    sb.append("[day=");
238                    sb.append(day);
239                    sb.append(",position=");
240                    sb.append(position);
241                    sb.append("]");
242    
243                    return sb.toString();
244            }
245    
246    }