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