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