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