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.camel;
018
019/**
020 * Represents a map of values which timeout after a period of inactivity.
021 */
022public interface TimeoutMap<K, V> extends Service {
023
024    /**
025     * Looks up the value in the map by the given key.
026     *
027     * @param key the key of the value to search for
028     * @return the value for the given key or <tt>null</tt> if it is not present (or has timed out)
029     */
030    V get(K key);
031
032    /**
033     * Returns the size of the map
034     *
035     * @return the size
036     */
037    int size();
038
039    /**
040     * Adds the key value pair into the map such that some time after the given
041     * timeout the entry will be evicted
042     *
043     * @param key   the key
044     * @param value the value
045     * @param timeoutMillis  timeout in millis
046     * @return the previous value associated with <tt>key</tt>, or
047     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
048     */
049    V put(K key, V value, long timeoutMillis);
050    
051    /**
052     * Adds the key value pair into the map if the specified key is not already associated with a value
053     * such that some time after the given timeout the entry will be evicted. Expiry time of an existing mapping
054     * is left unchanged.
055     *
056     * @param key   the key
057     * @param value the value
058     * @param timeoutMillis  timeout in millis
059     * @return the value associated with <tt>key</tt>, or
060     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
061     */
062    V putIfAbsent(K key, V value, long timeoutMillis);
063
064    /**
065     * Removes the object with the given key
066     *
067     * @param key  key for the object to remove
068     * @return the value for the given key or <tt>null</tt> if it is not present (or has timed out)
069     */
070    V remove(K key);
071
072    /**
073     * Assign the (singular) {@link Listener}
074     *
075     * @param listener the new listener
076     */
077    void addListener(Listener<K, V> listener);
078
079    @FunctionalInterface
080    interface Listener<K, V> {
081
082        enum Type { Put, Remove, Evict }
083
084        /**
085         * Callback when the map changes content
086         *
087         * @param key the item key
088         * @param value the item value
089         */
090        void timeoutMapEvent(Type type, K key, V value);
091
092    }
093}