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.commons.lang3.text; 018 019import java.util.Map; 020 021import org.apache.commons.lang3.SystemProperties; 022 023/** 024 * Lookup a String key to a String value. 025 * <p> 026 * This class represents the simplest form of a string to string map. 027 * It has a benefit over a map in that it can create the result on 028 * demand based on the key. 029 * </p> 030 * <p> 031 * This class comes complete with various factory methods. 032 * If these do not suffice, you can subclass and implement your own matcher. 033 * </p> 034 * <p> 035 * For example, it would be possible to implement a lookup that used the 036 * key as a primary key, and looked up the value on demand from the database. 037 * </p> 038 * 039 * @param <V> Unused. 040 * @since 2.2 041 * @deprecated As of 3.6, use Apache Commons Text 042 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/lookup/StringLookupFactory.html"> 043 * StringLookupFactory</a> instead 044 */ 045@Deprecated 046public abstract class StrLookup<V> { 047 048 /** 049 * Lookup that always returns null. 050 */ 051 private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null); 052 053 /** 054 * Lookup based on system properties. 055 */ 056 private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup(); 057 058 /** 059 * Returns a lookup which always returns null. 060 * 061 * @return a lookup that always returns null, not null 062 */ 063 public static StrLookup<?> noneLookup() { 064 return NONE_LOOKUP; 065 } 066 067 /** 068 * Returns a new lookup which uses a copy of the current 069 * {@link System#getProperties() System properties}. 070 * <p> 071 * If a security manager blocked access to system properties, then null will 072 * be returned from every lookup. 073 * </p> 074 * <p> 075 * If a null key is used, this lookup will throw a NullPointerException. 076 * </p> 077 * 078 * @return a lookup using system properties, not null 079 */ 080 public static StrLookup<String> systemPropertiesLookup() { 081 return SYSTEM_PROPERTIES_LOOKUP; 082 } 083 084 /** 085 * Returns a lookup which looks up values using a map. 086 * <p> 087 * If the map is null, then null will be returned from every lookup. 088 * The map result object is converted to a string using toString(). 089 * </p> 090 * 091 * @param <V> the type of the values supported by the lookup 092 * @param map the map of keys to values, may be null 093 * @return a lookup using the map, not null 094 */ 095 public static <V> StrLookup<V> mapLookup(final Map<String, V> map) { 096 return new MapStrLookup<>(map); 097 } 098 099 /** 100 * Constructor. 101 */ 102 protected StrLookup() { 103 } 104 105 /** 106 * Looks up a String key to a String value. 107 * <p> 108 * The internal implementation may use any mechanism to return the value. 109 * The simplest implementation is to use a Map. However, virtually any 110 * implementation is possible. 111 * </p> 112 * <p> 113 * For example, it would be possible to implement a lookup that used the 114 * key as a primary key, and looked up the value on demand from the database 115 * Or, a numeric based implementation could be created that treats the key 116 * as an integer, increments the value and return the result as a string - 117 * converting 1 to 2, 15 to 16 etc. 118 * </p> 119 * <p> 120 * The {@link #lookup(String)} method always returns a String, regardless of 121 * the underlying data, by converting it as necessary. For example: 122 * </p> 123 * <pre> 124 * Map<String, Object> map = new HashMap<String, Object>(); 125 * map.put("number", Integer.valueOf(2)); 126 * assertEquals("2", StrLookup.mapLookup(map).lookup("number")); 127 * </pre> 128 * @param key the key to be looked up, may be null 129 * @return the matching value, null if no match 130 */ 131 public abstract String lookup(String key); 132 133 /** 134 * Lookup implementation that uses a Map. 135 * 136 * @param <V> the type of mapped values. 137 */ 138 static class MapStrLookup<V> extends StrLookup<V> { 139 140 /** Map keys are variable names and value. */ 141 private final Map<String, V> map; 142 143 /** 144 * Creates a new instance backed by a Map. 145 * 146 * @param map the map of keys to values, may be null 147 */ 148 MapStrLookup(final Map<String, V> map) { 149 this.map = map; 150 } 151 152 /** 153 * Looks up a String key to a String value using the map. 154 * <p> 155 * If the map is null, then null is returned. 156 * The map result object is converted to a string using toString(). 157 * </p> 158 * 159 * @param key the key to be looked up, may be null 160 * @return the matching value, null if no match 161 */ 162 @Override 163 public String lookup(final String key) { 164 if (map == null) { 165 return null; 166 } 167 final Object obj = map.get(key); 168 if (obj == null) { 169 return null; 170 } 171 return obj.toString(); 172 } 173 } 174 175 /** 176 * Lookup implementation based on system properties. 177 */ 178 private static class SystemPropertiesStrLookup extends StrLookup<String> { 179 /** 180 * {@inheritDoc} This implementation directly accesses system properties. 181 */ 182 @Override 183 public String lookup(final String key) { 184 return SystemProperties.getProperty(key); 185 } 186 } 187}