BUG-5280: introduce the notion of client actor time
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / actors / client / ClientActorContext.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.datastore.actors.client;
9
10 import akka.actor.ActorRef;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Ticker;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
16 import org.opendaylight.yangtools.concepts.Identifiable;
17
18 /**
19  * An actor context associated with this {@link AbstractClientActor}.
20  *
21  * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
22  * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
23  * a {@link Ticker}, which can be obtained via {@link #ticker()}.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
29     private final ClientIdentifier identifier;
30
31     // Hidden to avoid subclassing
32     ClientActorContext(final ActorRef self, final String persistenceId, final ClientIdentifier identifier) {
33         super(self, persistenceId);
34         this.identifier = Preconditions.checkNotNull(identifier);
35     }
36
37     @Override
38     public @Nonnull ClientIdentifier getIdentifier() {
39         return identifier;
40     }
41
42     /**
43      * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
44      * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
45      * {@link com.google.common.base.Stopwatch}.
46      *
47      * @return Client actor time source
48      */
49     public @Nonnull Ticker ticker() {
50         return Ticker.systemTicker();
51     }
52 }