1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
27 import java.util.HashSet;
28 import java.util.Iterator;
31 * Manage a list of tasks to be executed when an event occurs. This makes the following guarantees:
33 * <li>Tasks can be safely added and removed in the middle of a run.</li>
34 * <li>No task will be returned more than once during a run.</li>
35 * <li>No task will be returned when it is not, at that moment, in the list of tasks.</li>
36 * <li>At the moment when next() returns null, all tasks on the list have been returned during the run.</li>
37 * <li>Initially and once next() returns null during a run, next() will continue to return null until startRun() is
43 private Iterator<Runnable> runlist;
44 private HashSet<Runnable> tasks = new HashSet<>();
45 private HashSet<Runnable> togo;
46 private HashSet<Runnable> sofar;
47 private HashSet<Runnable> added;
48 private HashSet<Runnable> removed;
51 * Start executing the sequence of tasks.
53 synchronized void startRun() {
54 sofar = new HashSet<>();
55 added = new HashSet<>();
56 removed = new HashSet<>();
57 togo = new HashSet<>(tasks);
58 runlist = togo.iterator();
62 * Get the next task to execute.
64 synchronized Runnable next() {
65 while (runlist != null) {
66 if (runlist.hasNext()) {
67 Runnable task = runlist.next();
68 if (addTaskToSoFar(task)) {
72 if (!added.isEmpty()) {
74 added = new HashSet<>();
76 runlist = togo.iterator();
89 * Add a task to the list of tasks to run whenever the event occurs.
91 synchronized void addTask(Runnable task) {
92 if (runlist != null) {
100 * Remove a task from the list of tasks to run whenever the event occurs.
102 synchronized void removeTask(Runnable task) {
103 if (runlist != null) {
110 private boolean addTaskToSoFar(Runnable task) {
111 if (removed.contains(task)) {
114 if (sofar.contains(task)) {