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.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 java.util.Calendar} or
069             *         {@link #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                    return ((p >= -53) && (p <= 53));
100            }
101    
102            /**
103             * Constructs a DayAndPosition
104             */
105            public DayAndPosition() {
106                    _day = NO_WEEKDAY;
107                    _position = 0;
108            }
109    
110            /**
111             * Constructs a DayAndPosition with the day of the week and day position.
112             */
113            public DayAndPosition(int d, int p) {
114                    if (!isValidDayOfWeek(d)) {
115                            throw new IllegalArgumentException("Invalid day of week");
116                    }
117    
118                    if (!isValidDayPosition(p)) {
119                            throw new IllegalArgumentException("Invalid day position");
120                    }
121    
122                    _day = d;
123                    _position = p;
124            }
125    
126            /**
127             * Returns a clone of this DayAndPosition.
128             *
129             * @return a clone of this DayAndPosition
130             */
131            @Override
132            public Object clone() {
133                    try {
134                            DayAndPosition other = (DayAndPosition)super.clone();
135    
136                            other._day = _day;
137                            other._position = _position;
138    
139                            return other;
140                    }
141                    catch (CloneNotSupportedException cnse) {
142                            throw new InternalError();
143                    }
144            }
145    
146            /**
147             * Returns <code>true</code> if the object equals this DayAndPosition.
148             *
149             * @param  obj the other object
150             * @return <code>true</code> if the object equals this DayAndPosition,
151             *         <code>false</code> otherwise
152             */
153            @Override
154            public boolean equals(Object obj) {
155                    if (obj == null) {
156                            return false;
157                    }
158    
159                    if (this == obj) {
160                            return true;
161                    }
162    
163                    if (!(obj instanceof DayAndPosition)) {
164                            return false;
165                    }
166    
167                    DayAndPosition that = (DayAndPosition)obj;
168    
169                    return
170                            (getDayOfWeek() == that.getDayOfWeek()) &&
171                            (getDayPosition() == that.getDayPosition());
172            }
173    
174            /**
175             * Returns the day of the week.
176             *
177             * @return the day of the week
178             */
179            public int getDayOfWeek() {
180                    return _day;
181            }
182    
183            /**
184             * Returns the day position.
185             *
186             * @return the day position
187             */
188            public int getDayPosition() {
189                    return _position;
190            }
191    
192            /**
193             * Returns the hash code of this DayAndPosition.
194             *
195             * @return the hash code of this DayAndPosition
196             */
197            @Override
198            public int hashCode() {
199                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
200    
201                    hashCode.append(_day);
202                    hashCode.append(_position);
203    
204                    return hashCode.toHashCode();
205            }
206    
207            /**
208             * Sets the day of the week
209             *
210             * @param d the day of the week
211             */
212            public void setDayOfWeek(int d) {
213                    if (!isValidDayOfWeek(d)) {
214                            throw new IllegalArgumentException("Invalid day of week");
215                    }
216    
217                    _day = d;
218            }
219    
220            /**
221             * Sets the day position
222             *
223             * @param p the day position
224             */
225            public void setDayPosition(int p) {
226                    if (!isValidDayPosition(p)) {
227                            throw new IllegalArgumentException();
228                    }
229    
230                    _position = p;
231            }
232    
233            /**
234             * Returns a string representation of the DayAndPosition
235             *
236             * @return a string representation of the DayAndPosition
237             */
238            @Override
239            public String toString() {
240                    StringBundler sb = new StringBundler(6);
241    
242                    sb.append(getClass().getName());
243                    sb.append("[day=");
244                    sb.append(_day);
245                    sb.append(",position=");
246                    sb.append(_position);
247                    sb.append("]");
248    
249                    return sb.toString();
250            }
251    
252            /**
253             * Field day
254             */
255            private int _day;
256    
257            /**
258             * Field position
259             */
260            private int _position;
261    
262    }