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