    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.text;
    018
    019import java.util.Collections;
    020import java.util.Map;
    021import java.util.Objects;
    022import java.util.ResourceBundle;
    023
    024import org.apache.commons.text.lookup.StringLookup;
    025import org.apache.commons.text.lookup.StringLookupFactory;
    026
    027/**
    028 * Lookup a String key to a String value.
    029 * <p>
    030 * This class represents the simplest form of a string to string map. It has a benefit over a map in that it can create
    031 * the result on demand based on the key.
    032 * </p>
    033 * <p>
    034 * This class comes complete with various factory methods. If these do not suffice, you can subclass and implement your
    035 * own matcher.
    036 * </p>
    037 * <p>
    038 * For example, it would be possible to implement a lookup that used the key as a primary key, and looked up the value
    039 * on demand from the database
    040 * </p>
    041 *
    042 * @param <V> the type of the values supported by the lookup
    043 * @since 1.0
    044 * @deprecated Deprecated as of 1.3, use {@link StringLookupFactory} instead. This class will be removed in 2.0.
    045 */
    046@Deprecated
    047public abstract class StrLookup<V> implements StringLookup {
    048
    049    /**
    050     * Lookup implementation that uses a Map.
    051     *
    052     * @param <V> the type of the values supported by the lookup
    053     */
    054    static class MapStrLookup<V> extends StrLookup<V> {
    055
    056        /** Map keys are variable names and value. */
    057        private final Map<String, V> map;
    058
    059        /**
    060         * Creates a new instance backed by a Map.
    061         *
    062         * @param map the map of keys to values, may be null
    063         */
    064        MapStrLookup(final Map<String, V> map) {
    065            this.map = map != null ? map : Collections.emptyMap();
    066        }
    067
    068        /**
    069         * Looks up a String key to a String value using the map.
    070         * <p>
    071         * If the map is null, then null is returned. The map result object is converted to a string using toString().
    072         * </p>
    073         *
    074         * @param key the key to be looked up, may be null
    075         * @return The matching value, null if no match
    076         */
    077        @Override
    078        public String lookup(final String key) {
    079            return Objects.toString(map.get(key), null);
    080        }
    081
    082        @Override
    083        public String toString() {
    084            return super.toString() + " [map=" + map + "]";
    085        }
    086    }
    087
    088    /**
    089     * Lookup implementation based on a ResourceBundle.
    090     */
    091    private static final class ResourceBundleLookup extends StrLookup<String> {
    092
    093        /** ResourceBundle keys are variable names and value. */
    094        private final ResourceBundle resourceBundle;
    095
    096        /**
    097         * Creates a new instance backed by a ResourceBundle.
    098         *
    099         * @param resourceBundle the ResourceBundle of keys to values, may be null
    100         */
    101        private ResourceBundleLookup(final ResourceBundle resourceBundle) {
    102            this.resourceBundle = resourceBundle;
    103        }
    104
    105        @Override
    106        public String lookup(final String key) {
    107            if (resourceBundle == null || key == null || !resourceBundle.containsKey(key)) {
    108                return null;
    109            }
    110            return resourceBundle.getString(key);
    111        }
    112
    113        @Override
    114        public String toString() {
    115            return super.toString() + " [resourceBundle=" + resourceBundle + "]";
    116        }
    117
    118    }
    119
    120    /**
    121     * Lookup implementation based on system properties.
    122     */
    123    private static final class SystemPropertiesStrLookup extends StrLookup<String> {
    124        /**
    125         * {@inheritDoc} This implementation directly accesses system properties.
    126         */
    127        @Override
    128        public String lookup(final String key) {
    129            if (!key.isEmpty()) {
    130                try {
    131                    return System.getProperty(key);
    132                } catch (final SecurityException ignored) {
    133                    // Noop: All lookup(String) will return null.
    134                }
    135            }
    136            return null;
    137        }
    138    }
    139
    140    /**
    141     * Lookup that always returns null.
    142     */
    143    private static final StrLookup<String> NONE_LOOKUP = new MapStrLookup<>(null);
    144
    145    /**
    146     * Lookup based on system properties.
    147     */
    148    private static final StrLookup<String> SYSTEM_PROPERTIES_LOOKUP = new SystemPropertiesStrLookup();
    149
    150    /**
    151     * Returns a lookup which looks up values using a map.
    152     * <p>
    153     * If the map is null, then null will be returned from every lookup. The map result object is converted to a string
    154     * using toString().
    155     * </p>
    156     *
    157     * @param <V> the type of the values supported by the lookup
    158     * @param map the map of keys to values, may be null
    159     * @return a lookup using the map, not null
    160     */
    161    public static <V> StrLookup<V> mapLookup(final Map<String, V> map) {
    162        return new MapStrLookup<>(map);
    163    }
    164
    165    /**
    166     * Returns a lookup which always returns null.
    167     *
    168     * @return a lookup that always returns null, not null
    169     */
    170    public static StrLookup<?> noneLookup() {
    171        return NONE_LOOKUP;
    172    }
    173
    174    /**
    175     * Returns a lookup which looks up values using a ResourceBundle.
    176     * <p>
    177     * If the ResourceBundle is null, then null will be returned from every lookup. The map result object is converted
    178     * to a string using toString().
    179     * </p>
    180     *
    181     * @param resourceBundle the map of keys to values, may be null
    182     * @return a lookup using the map, not null
    183     * @see StringLookupFactory#resourceBundleStringLookup(String)
    184     */
    185    public static StrLookup<String> resourceBundleLookup(final ResourceBundle resourceBundle) {
    186        return new ResourceBundleLookup(resourceBundle);
    187    }
    188
    189    /**
    190     * Returns a new lookup which uses a copy of the current {@link System#getProperties() System properties}.
    191     * <p>
    192     * If a security manager blocked access to system properties, then null will be returned from every lookup.
    193     * </p>
    194     * <p>
    195     * If a null key is used, this lookup will throw a NullPointerException.
    196     * </p>
    197     *
    198     * @return a lookup using system properties, not null
    199     */
    200    public static StrLookup<String> systemPropertiesLookup() {
    201        return SYSTEM_PROPERTIES_LOOKUP;
    202    }
    203
    204    /**
    205     * Constructor.
    206     */
    207    protected StrLookup() {
    208    }
    209}