001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.authorization.dseecompat;
029    
030    import java.util.Calendar;
031    
032    /**
033     * This class provides an enumeration of the allowed dayofweek types.
034     */
035    public enum EnumDayOfWeek {
036    
037        /**
038         * The enumeration type when the bind rule has specified dayofweek type of
039         * "mon".
040         */
041        DAY_MONDAY      ("mon"),
042        /**
043         * The enumeration type when the bind rule has specified dayofweek type of
044         * "tue" .
045         */
046        DAY_TUESDAY     ("tue"),
047        /**
048         * The enumeration type when the bind rule has specified dayofweek type of
049         * "wed".
050         */
051        DAY_WEDNESDAY   ("wed"),
052        /**
053         * The enumeration type when the bind rule has specified dayofweek type of
054         * "thu".
055         */
056        DAY_THURSDAY    ("thu"),
057        /**
058         * The enumeration type when the bind rule has specified dayofweek type of
059         * "fri".
060         */
061        DAY_FRIDAY      ("fri"),
062        /**
063         * The enumeration type when the bind rule has specified dayofweek type of
064         * "sat".
065         */
066        DAY_SATURDAY    ("sat"),
067        /**
068         * The enumeration type when the bind rule has specified dayofweek type of
069         * "sun".
070         */
071        DAY_SUNDAY      ("sun");
072    
073        /*
074        * The bind rule dayofweek type name.
075         */
076        private String day = null;
077    
078        /**
079         * Creates a new enumeration type for the specified bind rule dayofweek
080         * type.
081         * @param day  The day name.
082         */
083        EnumDayOfWeek (String day){
084            this.day = day;
085        }
086    
087        /**
088         * Creates a new enumeration type for the specified bind rule dayofweek
089         * type.
090         * @param day  The boolean type name.
091         * @return  True if the keyword is equal to the specified name.
092         */
093        public boolean isDayOfWeek(String day){
094            return day.equalsIgnoreCase(this.day);
095        }
096    
097        /**
098         * Create a new enumeration type for the specified dayofweek type name.
099         * @param day  The name of the enumeration to create.
100         * @return A new enumeration type for the name or null if the name is
101         * not valid.
102         */
103        public static EnumDayOfWeek createDayOfWeek(String day)
104        {
105            if (day != null){
106                for (EnumDayOfWeek t : EnumDayOfWeek.values()){
107                    if (t.isDayOfWeek(day)){
108                        return t;
109                    }
110                }
111            }
112            return null;
113        }
114    
115        /*
116         * TODO Evaluate supporting alternative forms for days of the week.
117         *
118         *  Should we support alternate forms for the names of the days of the
119         *  week in the isDayOfWeek() or createdayOfWeek() method?  In particular,
120         *  should we handle the case in which the user provided the full name
121         *  (e.g., "monday" instead of "mon")?
122         */
123        /**
124         *  Return a enumeration relating to a Calendar day of week field.
125         * @param day The day of week index to get.
126         * @return  An enumeration corresponding to the wanted day of the week or
127         * null if the day index is invalid.
128         */
129        public static EnumDayOfWeek getDayOfWeek(int day)
130        {
131            switch(day){
132            case Calendar.SUNDAY:
133                return DAY_SUNDAY;
134    
135            case Calendar.MONDAY:
136                return DAY_MONDAY;
137    
138            case Calendar.TUESDAY:
139                return DAY_TUESDAY;
140    
141            case Calendar.WEDNESDAY:
142                return DAY_WEDNESDAY;
143    
144            case Calendar.THURSDAY:
145                return DAY_THURSDAY;
146    
147            case Calendar.FRIDAY:
148                return DAY_FRIDAY;
149    
150            case Calendar.SATURDAY:
151                return DAY_SATURDAY;
152            }
153            return null;
154        }
155    }