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