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