Bump upstream versions
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import java.util.function.Consumer;
16 import org.opendaylight.controller.cluster.access.concepts.Request;
17 import org.opendaylight.controller.cluster.access.concepts.Response;
18 import org.opendaylight.yangtools.concepts.Immutable;
19
20 /**
21  * Single entry in a {@link AbstractClientConnection}. Tracks the request, the associated callback and time when
22  * the request was first enqueued.
23  *
24  * @author Robert Varga
25  */
26 @Beta
27 public class ConnectionEntry implements Immutable {
28     private final Consumer<Response<?, ?>> callback;
29     private final Request<?, ?> request;
30     private final long enqueuedTicks;
31
32     ConnectionEntry(final Request<?, ?> request, final Consumer<Response<?, ?>> callback, final long now) {
33         this.request = requireNonNull(request);
34         this.callback = requireNonNull(callback);
35         this.enqueuedTicks = now;
36     }
37
38     ConnectionEntry(final ConnectionEntry entry) {
39         this(entry.request, entry.callback, entry.enqueuedTicks);
40     }
41
42     public final Consumer<Response<?, ?>> getCallback() {
43         return callback;
44     }
45
46     public final Request<?, ?> getRequest() {
47         return request;
48     }
49
50     public void complete(final Response<?, ?> response) {
51         callback.accept(response);
52     }
53
54     public final long getEnqueuedTicks() {
55         return enqueuedTicks;
56     }
57
58     @Override
59     public final String toString() {
60         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
61     }
62
63     ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
64         return toStringHelper.add("request", request).add("enqueuedTicks", enqueuedTicks);
65     }
66 }