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