001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 As of 6.2.0, moved to {@link
058     *             com.liferay.portal.kernel.cal.DayAndPosition}
059     */
060    public class DayAndPosition implements Cloneable, Serializable {
061    
062            /**
063             * Field NO_WEEKDAY
064             */
065            public static final int NO_WEEKDAY = 0;
066    
067            /**
068             * Returns <code>true</code> if the day is a valid day of the week.
069             *
070             * @param  d the day of the week in terms of {@link java.util.Calendar} or
071             *         {@link #NO_WEEKDAY}
072             * @return <code>true</code> if the day is a valid day of the week;
073             *         <code>false</code> otherwise
074             */
075            public static boolean isValidDayOfWeek(int d) {
076                    switch (d) {
077    
078                            case NO_WEEKDAY :
079                            case Calendar.SUNDAY :
080                            case Calendar.MONDAY :
081                            case Calendar.TUESDAY :
082                            case Calendar.WEDNESDAY :
083                            case Calendar.THURSDAY :
084                            case Calendar.FRIDAY :
085                            case Calendar.SATURDAY :
086                                    return true;
087    
088                            default :
089                                    return false;
090                    }
091            }
092    
093            /**
094             * Returns <code>true</code> if the day position is valid.
095             *
096             * @param  p the day position
097             * @return <code>true</code> if the day position is valid;
098             *         <code>false</code> otherwise
099             */
100            public static boolean isValidDayPosition(int p) {
101                    return ((p >= -53) && (p <= 53));
102            }
103    
104            /**
105             * Constructs a DayAndPosition
106             */
107            public DayAndPosition() {
108                    _day = NO_WEEKDAY;
109                    _position = 0;
110            }
111    
112            /**
113             * Constructs a DayAndPosition with the day of the week and day position.
114             */
115            public DayAndPosition(int d, int p) {
116                    if (!isValidDayOfWeek(d)) {
117                            throw new IllegalArgumentException("Invalid day of week");
118                    }
119    
120                    if (!isValidDayPosition(p)) {
121                            throw new IllegalArgumentException("Invalid day position");
122                    }
123    
124                    _day = d;
125                    _position = p;
126            }
127    
128            /**
129             * Returns a clone of this DayAndPosition.
130             *
131             * @return a clone of this DayAndPosition
132             */
133            @Override
134            public Object clone() {
135                    try {
136                            DayAndPosition other = (DayAndPosition)super.clone();
137    
138                            other._day = _day;
139                            other._position = _position;
140    
141                            return other;
142                    }
143                    catch (CloneNotSupportedException cnse) {
144                            throw new InternalError();
145                    }
146            }
147    
148            /**
149             * Returns <code>true</code> if the object equals this DayAndPosition.
150             *
151             * @param  obj the other object
152             * @return <code>true</code> if the object equals this DayAndPosition,
153             *         <code>false</code> otherwise
154             */
155            @Override
156            public boolean equals(Object obj) {
157                    if (obj == null) {
158                            return false;
159                    }
160    
161                    if (this == obj) {
162                            return true;
163                    }
164    
165                    if (!(obj instanceof DayAndPosition)) {
166                            return false;
167                    }
168    
169                    DayAndPosition that = (DayAndPosition)obj;
170    
171                    return
172                            (getDayOfWeek() == that.getDayOfWeek()) &&
173                            (getDayPosition() == that.getDayPosition());
174            }
175    
176            /**
177             * Returns the day of the week.
178             *
179             * @return the day of the week
180             */
181            public int getDayOfWeek() {
182                    return _day;
183            }
184    
185            /**
186             * Returns the day position.
187             *
188             * @return the day position
189             */
190            public int getDayPosition() {
191                    return _position;
192            }
193    
194            /**
195             * Returns the hash code of this DayAndPosition.
196             *
197             * @return the hash code of this DayAndPosition
198             */
199            @Override
200            public int hashCode() {
201                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
202    
203                    hashCode.append(_day);
204                    hashCode.append(_position);
205    
206                    return hashCode.toHashCode();
207            }
208    
209            /**
210             * Sets the day of the week
211             *
212             * @param d the day of the week
213             */
214            public void setDayOfWeek(int d) {
215                    if (!isValidDayOfWeek(d)) {
216                            throw new IllegalArgumentException("Invalid day of week");
217                    }
218    
219                    _day = d;
220            }
221    
222            /**
223             * Sets the day position
224             *
225             * @param p the day position
226             */
227            public void setDayPosition(int p) {
228                    if (!isValidDayPosition(p)) {
229                            throw new IllegalArgumentException();
230                    }
231    
232                    _position = p;
233            }
234    
235            /**
236             * Returns a string representation of the DayAndPosition
237             *
238             * @return a string representation of the DayAndPosition
239             */
240            @Override
241            public String toString() {
242                    StringBundler sb = new StringBundler(6);
243    
244                    sb.append(getClass().getName());
245                    sb.append("[day=");
246                    sb.append(_day);
247                    sb.append(",position=");
248                    sb.append(_position);
249                    sb.append("]");
250    
251                    return sb.toString();
252            }
253    
254            /**
255             * Field day
256             */
257            private int _day;
258    
259            /**
260             * Field position
261             */
262            private int _position;
263    
264    }