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.config;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.util.*;
018
019import org.apache.juneau.config.encode.*;
020
021/**
022 * Identifies the supported modification types for config entries.
023 */
024public enum ConfigMod {
025
026   /**
027    * Encoded using the registered {@link ConfigEncoder}.
028    */
029   ENCODED("*");
030
031
032   private final String c;
033
034   private ConfigMod(String c) {
035      this.c = c;
036   }
037
038   /**
039    * Converts an array of modifiers to a modifier string.
040    *
041    * @param mods The modifiers.
042    * @return A modifier string, or an empty string if there are no modifiers.
043    */
044   public static String asString(ConfigMod...mods) {
045      if (mods.length == 0)
046         return "";
047      if (mods.length == 1)
048         return mods[0].c;
049      StringBuilder sb = new StringBuilder(mods.length);
050      for (ConfigMod m : mods)
051         sb.append(m.c);
052      return sb.toString();
053   }
054
055   private static ConfigMod fromChar(char c) {
056      if (c == '*')
057         return ENCODED;
058      return null;
059   }
060
061   /**
062    * Converts a modifier string (e.g. <js>"^*"</js>) into a list of {@link ConfigMod Modifiers}
063    * in reverse order of how they appear in the string.
064    *
065    * @param s The modifier string.
066    * @return The list of modifiers, or an empty list if the string is empty or <jk>null</jk>.
067    */
068   public static List<ConfigMod> asModifiersReverse(String s) {
069      if (isEmpty(s))
070         return Collections.emptyList();
071      if (s.length() == 1) {
072         ConfigMod m = fromChar(s.charAt(0));
073         return m == null ? Collections.<ConfigMod>emptyList() : Collections.singletonList(m);
074      }
075      List<ConfigMod> l = new ArrayList<>(s.length());
076      for (int i = s.length()-1; i >= 0; i--) {
077         ConfigMod m = fromChar(s.charAt(i));
078         if (m != null)
079            l.add(m);
080      }
081      return l;
082   }
083
084   /**
085    * Converts a modifier string (e.g. <js>"^*"</js>) into a list of {@link ConfigMod Modifiers}.
086    *
087    * @param s The modifier string.
088    * @return The list of modifiers, or an empty list if the string is empty or <jk>null</jk>.
089    */
090   public static List<ConfigMod> asModifiers(String s) {
091      if (isEmpty(s))
092         return Collections.emptyList();
093      if (s.length() == 1) {
094         ConfigMod m = fromChar(s.charAt(0));
095         return m == null ? Collections.<ConfigMod>emptyList() : Collections.singletonList(m);
096      }
097      List<ConfigMod> l = new ArrayList<>(s.length());
098      for (int i = 0; i < s.length(); i++) {
099         ConfigMod m = fromChar(s.charAt(i));
100         if (m != null)
101            l.add(m);
102      }
103      return l;
104   }
105}