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