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