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 */ 017 018package org.apache.commons.text.lookup; 019 020import java.util.Map; 021 022/** 023 * Provides access to lookups defined in this package. 024 * 025 * @since 1.3 026 */ 027public final class StringLookupFactory { 028 029 /** 030 * Defines the singleton for this class. 031 */ 032 public static final StringLookupFactory INSTANCE = new StringLookupFactory(); 033 034 /** 035 * No need to build instances for now. 036 */ 037 private StringLookupFactory() { 038 // empty 039 } 040 041 /** 042 * Returns the DateStringLookup singleton instance to format the current date with the format given in the key in a 043 * format compatible with {@link java.text.SimpleDateFormat}. 044 * 045 * @return the DateStringLookup singleton instance. 046 */ 047 public StringLookup dateStringLookup() { 048 return DateStringLookup.INSTANCE; 049 } 050 051 /** 052 * Returns the EnvironmentVariableStringLookup singleton instance where the lookup key is an environment variable 053 * name. 054 * 055 * @return the EnvironmentVariableStringLookup singleton instance. 056 */ 057 public StringLookup environmentVariableStringLookup() { 058 return EnvironmentVariableStringLookup.INSTANCE; 059 } 060 061 /** 062 * Returns a new InterpolatorStringLookup. 063 * <p> 064 * The following lookups are used by default: 065 * </p> 066 * <ul> 067 * <li>"sys" for the {@link SystemPropertyStringLookup}.</li> 068 * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li> 069 * <li>"java" for the {@link JavaPlatformStringLookup}.</li> 070 * <li>"date" for the {@link DateStringLookup}.</li> 071 * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> 072 * </ul> 073 * 074 * @return a new InterpolatorStringLookup. 075 */ 076 public StringLookup interpolatorStringLookup() { 077 return new InterpolatorStringLookup(); 078 } 079 080 /** 081 * Returns a new InterpolatorStringLookup. 082 * <p> 083 * The following lookups are used by default: 084 * </p> 085 * <ul> 086 * <li>"sys" for the {@link SystemPropertyStringLookup}.</li> 087 * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li> 088 * <li>"java" for the {@link JavaPlatformStringLookup}.</li> 089 * <li>"date" for the {@link DateStringLookup}.</li> 090 * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> 091 * </ul> 092 * 093 * @param <V> 094 * the value type the default string lookup's map. 095 * @param map 096 * the default map for string lookups. 097 * @return a new InterpolatorStringLookup. 098 */ 099 public <V> StringLookup interpolatorStringLookup(final Map<String, V> map) { 100 return new InterpolatorStringLookup(map); 101 } 102 103 /** 104 * Returns a new InterpolatorStringLookup. 105 * <p> 106 * The following lookups are used by default: 107 * </p> 108 * <ul> 109 * <li>"sys" for the {@link SystemPropertyStringLookup}.</li> 110 * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li> 111 * <li>"java" for the {@link JavaPlatformStringLookup}.</li> 112 * <li>"date" for the {@link DateStringLookup}.</li> 113 * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> 114 * </ul> 115 * 116 * @param defaultStringLookup 117 * the default string lookup. 118 * @return a new InterpolatorStringLookup. 119 */ 120 public StringLookup interpolatorStringLookup(final StringLookup defaultStringLookup) { 121 return new InterpolatorStringLookup(defaultStringLookup); 122 } 123 124 /** 125 * Returns a new InterpolatorStringLookup. 126 * <p> 127 * If {@code addDefaultLookups} is true, the following lookups are used in addition to the ones provided in 128 * {@code stringLookupMap}: 129 * </p> 130 * <ul> 131 * <li>"sys" for the {@link SystemPropertyStringLookup}.</li> 132 * <li>"env" for the {@link EnvironmentVariableStringLookup}.</li> 133 * <li>"java" for the {@link JavaPlatformStringLookup}.</li> 134 * <li>"date" for the {@link DateStringLookup}.</li> 135 * <li>"localhost" for the {@link LocalHostStringLookup}, see {@link #localHostStringLookup()} for key names.</li> 136 * </ul> 137 * 138 * @param stringLookupMap 139 * the map of string lookups. 140 * @param defaultStringLookup 141 * the default string lookup. 142 * @param addDefaultLookups 143 * whether to use lookups as described above. 144 * @return a new InterpolatorStringLookup. 145 * @since 1.4 146 */ 147 public StringLookup interpolatorStringLookup(final Map<String, StringLookup> stringLookupMap, 148 final StringLookup defaultStringLookup, final boolean addDefaultLookups) { 149 return new InterpolatorStringLookup(stringLookupMap, defaultStringLookup, addDefaultLookups); 150 } 151 152 /** 153 * Returns the JavaPlatformStringLookup singleton instance. 154 * 155 * @return the JavaPlatformStringLookup singleton instance. 156 */ 157 public StringLookup javaPlatformStringLookup() { 158 return JavaPlatformStringLookup.INSTANCE; 159 } 160 161 /** 162 * Returns the LocalHostStringLookup singleton instance where the lookup key is one of: 163 * <ul> 164 * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</li> 165 * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li> 166 * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li> 167 * </ul> 168 * 169 * @return the DateStringLookup singleton instance. 170 */ 171 public StringLookup localHostStringLookup() { 172 return LocalHostStringLookup.INSTANCE; 173 } 174 175 /** 176 * Returns a new map-based lookup where the request for a lookup is answered with the value for that key. 177 * 178 * @param <V> 179 * the map value type. 180 * @param map 181 * the map. 182 * @return a new MapStringLookup. 183 */ 184 public <V> StringLookup mapStringLookup(final Map<String, V> map) { 185 return MapStringLookup.on(map); 186 } 187 188 /** 189 * Returns the NullStringLookup singleton instance which always returns null. 190 * 191 * @return the NullStringLookup singleton instance. 192 */ 193 public StringLookup nullStringLookup() { 194 return NullStringLookup.INSTANCE; 195 } 196 197 /** 198 * Returns the ResourceBundleStringLookup singleton instance. 199 * <p> 200 * Looks up the value for a given key in the format "BundleName:BundleKey". 201 * </p> 202 * <p> 203 * For example: "com.domain.messages:MyKey". 204 * </p> 205 * 206 * @return the ResourceBundleStringLookup singleton instance. 207 */ 208 public StringLookup resourceBundleStringLookup() { 209 return ResourceBundleStringLookup.INSTANCE; 210 } 211 212 /** 213 * Returns the SystemPropertyStringLookup singleton instance where the lookup key is a system property name. 214 * 215 * @return the SystemPropertyStringLookup singleton instance. 216 */ 217 public StringLookup systemPropertyStringLookup() { 218 return SystemPropertyStringLookup.INSTANCE; 219 } 220}