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
029@ElementTypesAreNonnullByDefault
030public final class RemovalListeners {
031
032  private RemovalListeners() {}
033
034  /**
035   * Returns a {@code RemovalListener} which processes all eviction notifications using {@code
036   * executor}.
037   *
038   * @param listener the backing listener
039   * @param executor the executor with which removal notifications are asynchronously executed
040   */
041  public static <K, V> RemovalListener<K, V> asynchronous(
042      final RemovalListener<K, V> listener, final Executor executor) {
043    checkNotNull(listener);
044    checkNotNull(executor);
045    return new RemovalListener<K, V>() {
046      @Override
047      public void onRemoval(final RemovalNotification<K, V> notification) {
048        executor.execute(
049            new Runnable() {
050              @Override
051              public void run() {
052                listener.onRemoval(notification);
053              }
054            });
055      }
056    };
057  }
058}