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;
30 * Manage a list of tasks to be executed when an event occurs.
31 * 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 called.
40 public class TaskList {
41 private Iterator<Runnable> runlist;
42 private HashSet<Runnable> tasks = new HashSet<Runnable>();
43 private HashSet<Runnable> togo;
44 private HashSet<Runnable> sofar;
45 private HashSet<Runnable> added;
46 private HashSet<Runnable> removed;
49 * Construct a new TaskList
55 * Start executing the sequence of tasks.
57 public synchronized void startRun() {
58 sofar = new HashSet<Runnable>();
59 added = new HashSet<Runnable>();
60 removed = new HashSet<Runnable>();
61 togo = new HashSet<Runnable>(tasks);
62 runlist = togo.iterator();
66 * Get the next task to execute
68 public synchronized Runnable next() {
69 while (runlist != null) {
70 if (runlist.hasNext()) {
71 Runnable task = runlist.next();
72 if (removed.contains(task)) {
75 if (sofar.contains(task)) {
81 if (added.size() != 0) {
83 added = new HashSet<Runnable>();
85 runlist = togo.iterator();
98 * Add a task to the list of tasks to run whenever the event occurs.
100 public synchronized void addTask(Runnable task) {
101 if (runlist != null) {
103 removed.remove(task);
109 * Remove a task from the list of tasks to run whenever the event occurs.
111 public synchronized void removeTask(Runnable task) {
112 if (runlist != null) {