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    package org.apache.logging.log4j.core.lookup;
018    
019    import org.apache.logging.log4j.core.LogEvent;
020    
021    /**
022     * Lookup a String key to a String value.
023     * <p>
024     * This class represents the simplest form of a string to string map.
025     * It has a benefit over a map in that it can create the result on
026     * demand based on the key.
027     * <p>
028     * This class comes complete with various factory methods.
029     * If these do not suffice, you can subclass and implement your own matcher.
030     * <p>
031     * For example, it would be possible to implement a lookup that used the
032     * key as a primary key, and looked up the value on demand from the database
033     *
034     * @author Apache Software Foundation
035     * @version $Id$
036     *
037     * @param <V> The type of the value that is being queried.
038     */
039    public interface StrLookup<V> {
040        /**
041         * Looks up a String key to a String value.
042         * <p>
043         * The internal implementation may use any mechanism to return the value.
044         * The simplest implementation is to use a Map. However, virtually any
045         * implementation is possible.
046         * <p>
047         * For example, it would be possible to implement a lookup that used the
048         * key as a primary key, and looked up the value on demand from the database
049         * Or, a numeric based implementation could be created that treats the key
050         * as an integer, increments the value and return the result as a string -
051         * converting 1 to 2, 15 to 16 etc.
052         * <p>
053         * The {@link #lookup(String)} method always returns a String, regardless of
054         * the underlying data, by converting it as necessary. For example:
055         * <pre>
056         * Map<String, Object> map = new HashMap<String, Object>();
057         * map.put("number", new Integer(2));
058         * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
059         * </pre>
060         * @param key  the key to be looked up, may be null
061         * @return the matching value, null if no match
062         */
063        String lookup(String key);
064    
065        /**
066         * Looks up a String key to a String value possibly using the current LogEvent.
067         * <p>
068         * The internal implementation may use any mechanism to return the value.
069         * The simplest implementation is to use a Map. However, virtually any
070         * implementation is possible.
071         * <p>
072         * For example, it would be possible to implement a lookup that used the
073         * key as a primary key, and looked up the value on demand from the database
074         * Or, a numeric based implementation could be created that treats the key
075         * as an integer, increments the value and return the result as a string -
076         * converting 1 to 2, 15 to 16 etc.
077         * <p>
078         * The {@link #lookup(String)} method always returns a String, regardless of
079         * the underlying data, by converting it as necessary. For example:
080         * <pre>
081         * Map<String, Object> map = new HashMap<String, Object>();
082         * map.put("number", new Integer(2));
083         * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
084         * </pre>
085         * @param event The current LogEvent.
086         * @param key  the key to be looked up, may be null
087         * @return the matching value, null if no match
088         */
089        String lookup(LogEvent event, String key);
090    }