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.impl;
23 import java.util.LinkedList;
24 import java.util.Queue;
25 import java.util.concurrent.Executor;
28 * Executor that will run tasks until the queue is empty or a maximum number of tasks have
29 * been executed. Doesn't actually run anything until {@link #runAll()} is invoked.
31 public class MyExec implements Executor {
33 // TODO move this to policy-common/utils-test
35 private final int maxTasks;
36 private final Queue<Runnable> commands = new LinkedList<>();
38 public MyExec(int maxTasks) {
39 this.maxTasks = maxTasks;
42 public int getQueueLength() {
43 return commands.size();
47 public void execute(Runnable command) {
48 commands.add(command);
52 * Runs all tasks until the queue is empty or the maximum number of tasks have been
55 * @return {@code true} if the queue is empty, {@code false} if the maximum number of
56 * tasks have been reached before the queue was completed
58 public boolean runAll() {
59 for (int count = 0; count < maxTasks && !commands.isEmpty(); ++count) {
60 commands.remove().run();
63 return commands.isEmpty();