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.logging.log4j.util;
018
019import java.io.Serializable;
020import java.util.Map;
021
022/**
023 * A read-only collection of String keys mapped to values of arbitrary type.
024 *
025 * @since 2.7
026 */
027public interface ReadOnlyStringMap extends Serializable {
028    
029    /**
030     * Returns a non-{@code null} mutable {@code Map<String, String>} containing a snapshot of this data structure.
031     *
032     * @return a mutable copy of this data structure in {@code Map<String, String>} form.
033     */
034    Map<String, String> toMap();
035
036    /**
037     * Returns {@code true} if this data structure contains the specified key, {@code false} otherwise.
038     *
039     * @param key the key whose presence to check. May be {@code null}.
040     * @return {@code true} if this data structure contains the specified key, {@code false} otherwise.
041     */
042    boolean containsKey(String key);
043
044    /**
045     * Performs the given action for each key-value pair in this data structure
046     * until all entries have been processed or the action throws an exception.
047     * <p>
048     * Some implementations may not support structural modifications (adding new elements or removing elements) while
049     * iterating over the contents. In such implementations, attempts to add or remove elements from the
050     * {@code BiConsumer}'s {@link BiConsumer#accept(Object, Object)} accept} method may cause a
051     * {@code ConcurrentModificationException} to be thrown.
052     * </p>
053     *
054     * @param action The action to be performed for each key-value pair in this collection.
055     * @param <V> type of the value.
056     * @throws java.util.ConcurrentModificationException some implementations may not support structural modifications
057     *          to this data structure while iterating over the contents with {@link #forEach(BiConsumer)} or
058     *          {@link #forEach(TriConsumer, Object)}.
059     */
060    <V> void forEach(final BiConsumer<String, ? super V> action);
061
062    /**
063     * Performs the given action for each key-value pair in this data structure
064     * until all entries have been processed or the action throws an exception.
065     * <p>
066     * The third parameter lets callers pass in a stateful object to be modified with the key-value pairs,
067     * so the TriConsumer implementation itself can be stateless and potentially reusable.
068     * </p>
069     * <p>
070     * Some implementations may not support structural modifications (adding new elements or removing elements) while
071     * iterating over the contents. In such implementations, attempts to add or remove elements from the
072     * {@code TriConsumer}'s {@link TriConsumer#accept(Object, Object, Object) accept} method may cause a
073     * {@code ConcurrentModificationException} to be thrown.
074     * </p>
075     *
076     * @param action The action to be performed for each key-value pair in this collection.
077     * @param state the object to be passed as the third parameter to each invocation on the specified
078     *          triconsumer.
079     * @param <V> type of the value.
080     * @param <S> type of the third parameter.
081     * @throws java.util.ConcurrentModificationException some implementations may not support structural modifications
082     *          to this data structure while iterating over the contents with {@link #forEach(BiConsumer)} or
083     *          {@link #forEach(TriConsumer, Object)}.
084     */
085    <V, S> void forEach(final TriConsumer<String, ? super V, S> action, final S state);
086
087    /**
088     * Returns the value for the specified key, or {@code null} if the specified key does not exist in this collection.
089     *
090     * @param key the key whose value to return.
091     * @return the value for the specified key or {@code null}.
092     */
093    <V> V getValue(final String key);
094
095    /**
096     * Returns {@code true} if this collection is empty (size is zero), {@code false} otherwise.
097     * @return {@code true} if this collection is empty (size is zero).
098     */
099    boolean isEmpty();
100
101    /**
102     * Returns the number of key-value pairs in this collection.
103     *
104     * @return the number of key-value pairs in this collection.
105     */
106    int size();
107}