64586f05ee9943382c9c10361a84915a06c868be
[controller.git] / opendaylight / md-sal / cds-access-client / src / main / java / org / opendaylight / controller / cluster / access / client / ConnectionEntry.java
1 /*
2  * Copyright (c) 2016 Cisco 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.controller.cluster.access.client;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.function.Consumer;
13 import org.opendaylight.controller.cluster.access.concepts.Request;
14 import org.opendaylight.controller.cluster.access.concepts.Response;
15 import org.opendaylight.yangtools.concepts.Immutable;
16
17 /**
18  * Single entry in a {@link AbstractClientConnection}. Tracks the request, the associated callback and time when
19  * the request was first enqueued.
20  *
21  * @author Robert Varga
22  */
23 @Beta
24 public class ConnectionEntry implements Immutable {
25     private final Consumer<Response<?, ?>> callback;
26     private final Request<?, ?> request;
27     private final long enqueuedTicks;
28
29     ConnectionEntry(final Request<?, ?> request, final Consumer<Response<?, ?>> callback, final long now) {
30         this.request = Preconditions.checkNotNull(request);
31         this.callback = Preconditions.checkNotNull(callback);
32         this.enqueuedTicks = now;
33     }
34
35     ConnectionEntry(final ConnectionEntry entry) {
36         this(entry.request, entry.callback, entry.enqueuedTicks);
37     }
38
39     public final Consumer<Response<?, ?>> getCallback() {
40         return callback;
41     }
42
43     public final Request<?, ?> getRequest() {
44         return request;
45     }
46
47     public void complete(final Response<?, ?> response) {
48         callback.accept(response);
49     }
50
51     final long getEnqueuedTicks() {
52         return enqueuedTicks;
53     }
54 }