    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 the JavaPlatformStringLookup singleton instance.
    126     *
    127     * @return the JavaPlatformStringLookup singleton instance.
    128     */
    129    public StringLookup javaPlatformStringLookup() {
    130        return JavaPlatformStringLookup.INSTANCE;
    131    }
    132
    133    /**
    134     * Returns the LocalHostStringLookup singleton instance where the lookup key is one of:
    135     * <ul>
    136     * <li><b>name</b>: for the local host name, for example {@code EXAMPLE}.</li>
    137     * <li><b>canonical-name</b>: for the local canonical host name, for example {@code EXAMPLE.apache.org}.</li>
    138     * <li><b>address</b>: for the local host address, for example {@code 192.168.56.1}.</li>
    139     * </ul>
    140     *
    141     * @return the DateStringLookup singleton instance.
    142     */
    143    public StringLookup localHostStringLookup() {
    144        return LocalHostStringLookup.INSTANCE;
    145    }
    146
    147    /**
    148     * Returns a new map-based lookup where the request for a lookup is answered with the value for that key.
    149     *
    150     * @param <V>
    151     *            the map value type.
    152     * @param map
    153     *            the map.
    154     * @return a new MapStringLookup.
    155     */
    156    public <V> StringLookup mapStringLookup(final Map<String, V> map) {
    157        return MapStringLookup.on(map);
    158    }
    159
    160    /**
    161     * Returns the NullStringLookup singleton instance which always returns null.
    162     *
    163     * @return the NullStringLookup singleton instance.
    164     */
    165    public StringLookup nullStringLookup() {
    166        return NullStringLookup.INSTANCE;
    167    }
    168
    169    /**
    170     * Returns the ResourceBundleStringLookup singleton instance.
    171     * <p>
    172     * Looks up the value for a given key in the format "BundleName:BundleKey".
    173     * </p>
    174     * <p>
    175     * For example: "com.domain.messages:MyKey".
    176     * </p>
    177     *
    178     * @return the ResourceBundleStringLookup singleton instance.
    179     */
    180    public StringLookup resourceBundleStringLookup() {
    181        return ResourceBundleStringLookup.INSTANCE;
    182    }
    183
    184    /**
    185     * Returns the SystemPropertyStringLookup singleton instance where the lookup key is a system property name.
    186     *
    187     * @return the SystemPropertyStringLookup singleton instance.
    188     */
    189    public StringLookup systemPropertyStringLookup() {
    190        return SystemPropertyStringLookup.INSTANCE;
    191    }
    192}