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