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.commons.utils.Utils.*;
020
021import java.util.*;
022
023/**
024 * Represents the enablement settings of a feature.
025 *
026 */
027public enum Enablement {
028
029   /**
030    * Feature is always enabled.
031    */
032   ALWAYS,
033
034   /**
035    * Feature is enabled per HTTP request.
036    */
037   CONDITIONAL,
038
039   /**
040    * Feature is disabled.
041    */
042   NEVER;
043
044   private static final Map<String,Enablement> MAP = new HashMap<>();
045
046   static {
047      MAP.put("TRUE", ALWAYS);
048      MAP.put("ALWAYS", ALWAYS);
049      MAP.put("FALSE", NEVER);
050      MAP.put("NEVER", NEVER);
051      MAP.put("CONDITIONAL", CONDITIONAL);
052   }
053
054   /**
055    * Retrieves this enum using case-insensitive matching.
056    *
057    * @param s The enum name to resolve.
058    * @return The resolved value, or <jk>null</jk> if no match found.
059    */
060   public static Enablement fromString(String s) {
061      return MAP.get(emptyIfNull(s).toUpperCase());
062   }
063
064   /**
065    * Tests for enablement.
066    *
067    * @param def The default value to use if this is {@link #CONDITIONAL}.
068    * @return <jk>true</jk> if this is {@link #ALWAYS} or {@link #CONDITIONAL} and <c>def</c> is <jk>true</jk>.
069    */
070   public boolean isEnabled(boolean def) {
071      if (this == ALWAYS)
072         return true;
073      if (this == NEVER)
074         return false;
075      return def;
076   }
077
078   /**
079    * Returns <jk>true</jk> if this enum is one of the specified values.
080    *
081    * @param values The values to check against.
082    * @return <jk>true</jk> if this enum is one of the specified values.
083    */
084   public boolean isOneOf(Enablement...values) {
085      for (var v : values)
086         if (this == v)
087            return true;
088      return false;
089   }
090}