1/*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.cache;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.annotations.Beta;
22
23import java.util.concurrent.Executor;
24
25/**
26 * A collection of common removal listeners.
27 *
28 * @author Charles Fry
29 * @since 10.0
30 */
31@Beta
32public final class RemovalListeners {
33
34  private RemovalListeners() {}
35
36  /**
37   * Returns a {@code RemovalListener} which processes all eviction
38   * notifications using {@code executor}.
39   *
40   * @param listener the backing listener
41   * @param executor the executor with which removal notifications are
42   *     asynchronously executed
43   */
44  public static <K, V> RemovalListener<K, V> asynchronous(
45      final RemovalListener<K, V> listener, final Executor executor) {
46    checkNotNull(listener);
47    checkNotNull(executor);
48    return new RemovalListener<K, V>() {
49      @Override
50      public void onRemoval(final RemovalNotification<K, V> notification) {
51        executor.execute(new Runnable() {
52          @Override
53          public void run() {
54            listener.onRemoval(notification);
55          }
56        });
57      }
58    };
59  }
60
61}
62