Remove dmaap from models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / CallbackManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider;
22
23 import java.time.Instant;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 /**
27  * Manager for "start" and "end" callbacks.
28  */
29 public class CallbackManager implements Runnable {
30     private final AtomicReference<Instant> startTime = new AtomicReference<>();
31     private final AtomicReference<Instant> endTime = new AtomicReference<>();
32
33     /**
34      * Determines if the "start" callback can be invoked. If so, it sets the
35      * {@link #startTime} to the current time.
36      *
37      * @return {@code true} if the "start" callback can be invoked, {@code false}
38      *         otherwise
39      */
40     public boolean canStart() {
41         return startTime.compareAndSet(null, Instant.now());
42     }
43
44     /**
45      * Determines if the "end" callback can be invoked. If so, it sets the
46      * {@link #endTime} to the current time.
47      *
48      * @return {@code true} if the "end" callback can be invoked, {@code false}
49      *         otherwise
50      */
51     public boolean canEnd() {
52         return endTime.compareAndSet(null, Instant.now());
53     }
54
55     /**
56      * Gets the start time.
57      *
58      * @return the start time, or {@code null} if {@link #canStart()} has not been
59      *         invoked yet.
60      */
61     public Instant getStartTime() {
62         return startTime.get();
63     }
64
65     /**
66      * Gets the end time.
67      *
68      * @return the end time, or {@code null} if {@link #canEnd()} has not been invoked
69      *         yet.
70      */
71     public Instant getEndTime() {
72         return endTime.get();
73     }
74
75     /**
76      * Prevents further callbacks from being executed by setting {@link #startTime}
77      * and {@link #endTime}.
78      */
79     @Override
80     public void run() {
81         canStart();
82         canEnd();
83     }
84 }