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