001 /*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package com.google.common.collect;
018
019 import com.google.common.annotations.Beta;
020
021 import java.util.concurrent.Executor;
022
023 import javax.annotation.Nullable;
024
025 /**
026 * A collection of common eviction listeners.
027 *
028 * @author Charles Fry
029 * @since 7
030 */
031 @Beta
032 public final class EvictionListeners {
033
034 private EvictionListeners() {}
035
036 /**
037 * Returns an asynchronous {@code MapEvictionListener} which processes all
038 * eviction notifications asynchronously, using {@code executor}.
039 *
040 * @param listener the backing listener
041 * @param executor the executor with which eviciton notifications are
042 * asynchronously executed
043 */
044 public static <K, V> MapEvictionListener<K, V> asynchronous(
045 final MapEvictionListener<K, V> listener, final Executor executor) {
046 return new MapEvictionListener<K, V>() {
047 @Override
048 public void onEviction(@Nullable final K key, @Nullable final V value) {
049 executor.execute(new Runnable() {
050 @Override
051 public void run() {
052 listener.onEviction(key, value);
053 }
054 });
055 }
056 };
057 }
058
059 }