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