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 java.util.concurrent.Executor;
022
023/**
024 * A collection of common removal listeners.
025 *
026 * @author Charles Fry
027 * @since 10.0
028 */
029public final class RemovalListeners {
030
031  private RemovalListeners() {}
032
033  /**
034   * Returns a {@code RemovalListener} which processes all eviction
035   * notifications using {@code executor}.
036   *
037   * @param listener the backing listener
038   * @param executor the executor with which removal notifications are
039   *     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(new Runnable() {
049          @Override
050          public void run() {
051            listener.onRemoval(notification);
052          }
053        });
054      }
055    };
056  }
057
058}