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 Runnable {
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 a copy of the keys in the map
034     *
035     * @return the keys
036     */
037    Object[] getKeys();
038
039    /**
040     * Returns the size of the map
041     *
042     * @return the size
043     */
044    int size();
045
046    /**
047     * Adds the key value pair into the map such that some time after the given
048     * timeout the entry will be evicted
049     *
050     * @param key   the key
051     * @param value the value
052     * @param timeoutMillis  timeout in millis
053     * @return the previous value associated with <tt>key</tt>, or
054     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
055     */
056    V put(K key, V value, long timeoutMillis);
057    
058    /**
059     * Adds the key value pair into the map if the specified key is not already associated with a value
060     * such that some time after the given timeout the entry will be evicted
061     *
062     * @param key   the key
063     * @param value the value
064     * @param timeoutMillis  timeout in millis
065     * @return the value associated with <tt>key</tt>, or
066     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
067     */
068    V putIfAbsent(K key, V value, long timeoutMillis);
069
070    /**
071     * Callback when the value has been evicted
072     *
073     * @param key the key
074     * @param value the value
075     * @return <tt>true</tt> to remove the evicted value,
076     *         or <tt>false</tt> to veto the eviction and thus keep the value.
077     */
078    boolean onEviction(K key, V value);
079
080    /**
081     * Removes the object with the given key
082     *
083     * @param key  key for the object to remove
084     * @return the value for the given key or <tt>null</tt> if it is not present (or has timed out)
085     */
086    V remove(K key);
087
088    /**
089     * Purges any old entries from the map
090     */
091    void purge();
092}