001/*
002 * Copyright (C) 2011 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
017package com.google.common.cache;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.Beta;
022
023import java.util.concurrent.Executor;
024
025/**
026 * A collection of common removal listeners.
027 *
028 * @author Charles Fry
029 * @since 10.0
030 */
031@Beta
032public final class RemovalListeners {
033
034  private RemovalListeners() {}
035
036  /**
037   * Returns a {@code RemovalListener} which processes all eviction
038   * notifications using {@code executor}.
039   *
040   * @param listener the backing listener
041   * @param executor the executor with which removal notifications are
042   *     asynchronously executed
043   */
044  public static <K, V> RemovalListener<K, V> asynchronous(
045      final RemovalListener<K, V> listener, final Executor executor) {
046    checkNotNull(listener);
047    checkNotNull(executor);
048    return new RemovalListener<K, V>() {
049      @Override
050      public void onRemoval(final RemovalNotification<K, V> notification) {
051        executor.execute(new Runnable() {
052          @Override
053          public void run() {
054            listener.onRemoval(notification);
055          }
056        });
057      }
058    };
059  }
060
061}