Fix return building on policy get
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / MyExec.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.impl;
22
23 import java.util.LinkedList;
24 import java.util.Queue;
25 import java.util.concurrent.Executor;
26
27 /**
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.
30  */
31 public class MyExec implements Executor {
32
33     // TODO move this to policy-common/utils-test
34
35     private final int maxTasks;
36     private final Queue<Runnable> commands = new LinkedList<>();
37
38     public MyExec(int maxTasks) {
39         this.maxTasks = maxTasks;
40     }
41
42     public int getQueueLength() {
43         return commands.size();
44     }
45
46     @Override
47     public void execute(Runnable command) {
48         commands.add(command);
49     }
50
51     /**
52      * Runs all tasks until the queue is empty or the maximum number of tasks have been
53      * reached.
54      *
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
57      */
58     public boolean runAll() {
59         for (int count = 0; count < maxTasks && !commands.isEmpty(); ++count) {
60             commands.remove().run();
61         }
62
63         return commands.isEmpty();
64     }
65 }