2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider;
23 import java.time.Instant;
24 import java.util.concurrent.atomic.AtomicReference;
27 * Manager for "start" and "end" callbacks.
29 public class CallbackManager implements Runnable {
30 private final AtomicReference<Instant> startTime = new AtomicReference<>();
31 private final AtomicReference<Instant> endTime = new AtomicReference<>();
34 * Determines if the "start" callback can be invoked. If so, it sets the
35 * {@link #startTime} to the current time.
37 * @return {@code true} if the "start" callback can be invoked, {@code false}
40 public boolean canStart() {
41 return startTime.compareAndSet(null, Instant.now());
45 * Determines if the "end" callback can be invoked. If so, it sets the
46 * {@link #endTime} to the current time.
48 * @return {@code true} if the "end" callback can be invoked, {@code false}
51 public boolean canEnd() {
52 return endTime.compareAndSet(null, Instant.now());
56 * Gets the start time.
58 * @return the start time, or {@code null} if {@link #canStart()} has not been
61 public Instant getStartTime() {
62 return startTime.get();
68 * @return the end time, or {@code null} if {@link #canEnd()} has not been invoked
71 public Instant getEndTime() {
76 * Prevents further callbacks from being executed by setting {@link #startTime}
77 * and {@link #endTime}.