001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016
017import java.util.*;
018
019/**
020 * Represents the enablement settings of a feature.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 * </ul>
024 */
025public enum Enablement {
026
027   /**
028    * Feature is always enabled.
029    */
030   ALWAYS,
031
032   /**
033    * Feature is enabled per HTTP request.
034    */
035   CONDITIONAL,
036
037   /**
038    * Feature is disabled.
039    */
040   NEVER;
041
042   /**
043    * Retrieves this enum using case-insensitive matching.
044    *
045    * @param s The enum name to resolve.
046    * @return The resolved value, or <jk>null</jk> if no match found.
047    */
048   public static Enablement fromString(String s) {
049      return MAP.get(emptyIfNull(s).toUpperCase());
050   }
051
052   private static final Map<String,Enablement> MAP = new HashMap<>();
053   static {
054      MAP.put("TRUE",ALWAYS);
055      MAP.put("ALWAYS",ALWAYS);
056      MAP.put("FALSE",NEVER);
057      MAP.put("NEVER",NEVER);
058      MAP.put("CONDITIONAL",CONDITIONAL);
059   }
060
061   /**
062    * Returns <jk>true</jk> if this enum is one of the specified values.
063    *
064    * @param values The values to check against.
065    * @return <jk>true</jk> if this enum is one of the specified values.
066    */
067   public boolean isOneOf(Enablement...values) {
068      for (Enablement v : values)
069          if (this == v)
070             return true;
071      return false;
072   }
073
074   /**
075    * Tests for enablement.
076    *
077    * @param def The default value to use if this is {@link #CONDITIONAL}.
078    * @return <jk>true</jk> if this is {@link #ALWAYS} or {@link #CONDITIONAL} and <c>def</c> is <jk>true</jk>.
079    */
080   public boolean isEnabled(boolean def) {
081      if (this == ALWAYS)
082         return true;
083      if (this == NEVER)
084         return false;
085      return def;
086   }
087}
088