Separate out {Identity,Equality}QueuedNotificationManager
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / concurrent / QueuedNotificationManager.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util.concurrent;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.concurrent.Executor;
12 import org.eclipse.jdt.annotation.NonNull;
13
14 /**
15  * {@inheritDoc}
16  *
17  * <p>
18  * This class is pessimistic about listener type and uses identity mapping for comparing them. This is defensive versus
19  * reused objects, maintaining semantics. This may not always be intended, for example if {@code L} is a {@code String}
20  * which is being dynamically determined. In that case we do not want to use identity, but equality, as otherwise
21  * the caller is forced to use {@link String#intern()} -- leading to interning in lookup, which is absolutely
22  * unnecessary. In such use cases, use {@link EqualityQueuedNotificationManager} instead.
23  *
24  * @author Thomas Pantelis
25  */
26 public final class QueuedNotificationManager<L, N> extends IdentityQueuedNotificationManager<L, N> {
27     @FunctionalInterface
28     public interface BatchedInvoker<L, N> {
29         /**
30          * Called to invoke a listener with a notification.
31          *
32          * @param listener the listener to invoke
33          * @param notifications notifications to send
34          */
35         void invokeListener(@NonNull L listener, @NonNull ImmutableList<N> notifications);
36     }
37
38     QueuedNotificationManager(final @NonNull Executor executor, final @NonNull BatchedInvoker<L, N> listenerInvoker,
39             final int maxQueueCapacity, final @NonNull String name) {
40         super(name, executor, maxQueueCapacity, listenerInvoker);
41     }
42
43     /**
44      * Create a new notification manager.
45      *
46      * @param executor the {@link Executor} to use for notification tasks
47      * @param listenerInvoker the {@link BatchedInvoker} to use for invoking listeners
48      * @param maxQueueCapacity the capacity of each listener queue
49      * @param name the name of this instance for logging info
50      */
51     public static <L, N> QueuedNotificationManager<L, N> create(final @NonNull Executor executor,
52             final@NonNull  BatchedInvoker<L, N> listenerInvoker, final int maxQueueCapacity,
53             final @NonNull String name) {
54         return new QueuedNotificationManager<>(executor, listenerInvoker, maxQueueCapacity, name);
55     }
56 }