001    /**
002     * Copyright (c) 2000-present 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    @Deprecated
061    public class DayAndPosition implements Cloneable, Serializable {
062    
063            /**
064             * Field NO_WEEKDAY
065             */
066            public static final int NO_WEEKDAY = 0;
067    
068            /**
069             * Returns <code>true</code> if the day is a valid day of the week.
070             *
071             * @param  d the day of the week in terms of {@link Calendar} or {@link
072             *         #NO_WEEKDAY}
073             * @return <code>true</code> if the day is a valid day of the week;
074             *         <code>false</code> otherwise
075             */
076            public static boolean isValidDayOfWeek(int d) {
077                    switch (d) {
078    
079                            case NO_WEEKDAY :
080                            case Calendar.SUNDAY :
081                            case Calendar.MONDAY :
082                            case Calendar.TUESDAY :
083                            case Calendar.WEDNESDAY :
084                            case Calendar.THURSDAY :
085                            case Calendar.FRIDAY :
086                            case Calendar.SATURDAY :
087                                    return true;
088    
089                            default :
090                                    return false;
091                    }
092            }
093    
094            /**
095             * Returns <code>true</code> if the day position is valid.
096             *
097             * @param  p the day position
098             * @return <code>true</code> if the day position is valid;
099             *         <code>false</code> otherwise
100             */
101            public static boolean isValidDayPosition(int p) {
102                    return ((p >= -53) && (p <= 53));
103            }
104    
105            /**
106             * Constructs a DayAndPosition
107             */
108            public DayAndPosition() {
109                    _day = NO_WEEKDAY;
110                    _position = 0;
111            }
112    
113            /**
114             * Constructs a DayAndPosition with the day of the week and day position.
115             */
116            public DayAndPosition(int d, int p) {
117                    if (!isValidDayOfWeek(d)) {
118                            throw new IllegalArgumentException("Invalid day of week");
119                    }
120    
121                    if (!isValidDayPosition(p)) {
122                            throw new IllegalArgumentException("Invalid day position");
123                    }
124    
125                    _day = d;
126                    _position = p;
127            }
128    
129            /**
130             * Returns a clone of this DayAndPosition.
131             *
132             * @return a clone of this DayAndPosition
133             */
134            @Override
135            public Object clone() {
136                    try {
137                            DayAndPosition other = (DayAndPosition)super.clone();
138    
139                            other._day = _day;
140                            other._position = _position;
141    
142                            return other;
143                    }
144                    catch (CloneNotSupportedException cnse) {
145                            throw new InternalError();
146                    }
147            }
148    
149            /**
150             * Returns <code>true</code> if the object equals this DayAndPosition.
151             *
152             * @param  obj the other object
153             * @return <code>true</code> if the object equals this DayAndPosition,
154             *         <code>false</code> otherwise
155             */
156            @Override
157            public boolean equals(Object obj) {
158                    if (obj == null) {
159                            return false;
160                    }
161    
162                    if (this == obj) {
163                            return true;
164                    }
165    
166                    if (!(obj instanceof DayAndPosition)) {
167                            return false;
168                    }
169    
170                    DayAndPosition that = (DayAndPosition)obj;
171    
172                    return
173                            (getDayOfWeek() == that.getDayOfWeek()) &&
174                            (getDayPosition() == that.getDayPosition());
175            }
176    
177            /**
178             * Returns the day of the week.
179             *
180             * @return the day of the week
181             */
182            public int getDayOfWeek() {
183                    return _day;
184            }
185    
186            /**
187             * Returns the day position.
188             *
189             * @return the day position
190             */
191            public int getDayPosition() {
192                    return _position;
193            }
194    
195            /**
196             * Returns the hash code of this DayAndPosition.
197             *
198             * @return the hash code of this DayAndPosition
199             */
200            @Override
201            public int hashCode() {
202                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
203    
204                    hashCode.append(_day);
205                    hashCode.append(_position);
206    
207                    return hashCode.toHashCode();
208            }
209    
210            /**
211             * Sets the day of the week
212             *
213             * @param d the day of the week
214             */
215            public void setDayOfWeek(int d) {
216                    if (!isValidDayOfWeek(d)) {
217                            throw new IllegalArgumentException("Invalid day of week");
218                    }
219    
220                    _day = d;
221            }
222    
223            /**
224             * Sets the day position
225             *
226             * @param p the day position
227             */
228            public void setDayPosition(int p) {
229                    if (!isValidDayPosition(p)) {
230                            throw new IllegalArgumentException();
231                    }
232    
233                    _position = p;
234            }
235    
236            /**
237             * Returns a string representation of the DayAndPosition
238             *
239             * @return a string representation of the DayAndPosition
240             */
241            @Override
242            public String toString() {
243                    StringBundler sb = new StringBundler(6);
244    
245                    sb.append(getClass().getName());
246                    sb.append("[day=");
247                    sb.append(_day);
248                    sb.append(",position=");
249                    sb.append(_position);
250                    sb.append("]");
251    
252                    return sb.toString();
253            }
254    
255            /**
256             * Field day
257             */
258            private int _day;
259    
260            /**
261             * Field position
262             */
263            private int _position;
264    
265    }