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     * @version $Id$
035     */
036    public interface StrLookup {
037        /**
038         * Looks up a String key to a String value.
039         * <p>
040         * The internal implementation may use any mechanism to return the value.
041         * The simplest implementation is to use a Map. However, virtually any
042         * implementation is possible.
043         * <p>
044         * For example, it would be possible to implement a lookup that used the
045         * key as a primary key, and looked up the value on demand from the database
046         * Or, a numeric based implementation could be created that treats the key
047         * as an integer, increments the value and return the result as a string -
048         * converting 1 to 2, 15 to 16 etc.
049         * <p>
050         * The {@link #lookup(String)} method always returns a String, regardless of
051         * the underlying data, by converting it as necessary. For example:
052         * <pre>
053         * Map<String, Object> map = new HashMap<String, Object>();
054         * map.put("number", new Integer(2));
055         * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
056         * </pre>
057         * @param key  the key to be looked up, may be null
058         * @return the matching value, null if no match
059         */
060        String lookup(String key);
061    
062        /**
063         * Looks up a String key to a String value possibly using the current LogEvent.
064         * <p>
065         * The internal implementation may use any mechanism to return the value.
066         * The simplest implementation is to use a Map. However, virtually any
067         * implementation is possible.
068         * <p>
069         * For example, it would be possible to implement a lookup that used the
070         * key as a primary key, and looked up the value on demand from the database
071         * Or, a numeric based implementation could be created that treats the key
072         * as an integer, increments the value and return the result as a string -
073         * converting 1 to 2, 15 to 16 etc.
074         * <p>
075         * The {@link #lookup(String)} method always returns a String, regardless of
076         * the underlying data, by converting it as necessary. For example:
077         * <pre>
078         * Map<String, Object> map = new HashMap<String, Object>();
079         * map.put("number", new Integer(2));
080         * assertEquals("2", StrLookup.mapLookup(map).lookup("number"));
081         * </pre>
082         * @param event The current LogEvent.
083         * @param key  the key to be looked up, may be null
084         * @return the matching value, null if no match
085         */
086        String lookup(LogEvent event, String key);
087    }