Jordan Halterman | 9bdc24f | 2017-04-19 23:45:12 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.concurrent.CompletableFuture; |
| 20 | import java.util.concurrent.Executor; |
| 21 | import java.util.function.Consumer; |
| 22 | |
| 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.store.service.AsyncAtomicValue; |
| 25 | import org.onosproject.store.service.Task; |
| 26 | import org.onosproject.store.service.WorkQueue; |
| 27 | import org.onosproject.store.service.WorkQueueStats; |
| 28 | |
| 29 | /** |
| 30 | * {@link AsyncAtomicValue} that executes asynchronous callbacks on a user provided |
| 31 | * {@link Executor}. |
| 32 | */ |
| 33 | public class ExecutingWorkQueue<E> extends ExecutingDistributedPrimitive implements WorkQueue<E> { |
| 34 | private final WorkQueue<E> delegateQueue; |
| 35 | private final Executor executor; |
| 36 | |
| 37 | public ExecutingWorkQueue(WorkQueue<E> delegateQueue, Executor executor) { |
| 38 | super(delegateQueue, executor); |
| 39 | this.delegateQueue = delegateQueue; |
| 40 | this.executor = executor; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public CompletableFuture<Void> addMultiple(Collection<E> items) { |
| 45 | return Tools.asyncFuture(delegateQueue.addMultiple(items), executor); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public CompletableFuture<Collection<Task<E>>> take(int maxItems) { |
| 50 | return Tools.asyncFuture(delegateQueue.take(maxItems), executor); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public CompletableFuture<Void> complete(Collection<String> taskIds) { |
| 55 | return Tools.asyncFuture(delegateQueue.complete(taskIds), executor); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public CompletableFuture<Void> registerTaskProcessor( |
| 60 | Consumer<E> taskProcessor, int parallelism, Executor executor) { |
| 61 | return Tools.asyncFuture( |
| 62 | delegateQueue.registerTaskProcessor(taskProcessor, parallelism, executor), |
| 63 | this.executor); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public CompletableFuture<Void> stopProcessing() { |
| 68 | return Tools.asyncFuture(delegateQueue.stopProcessing(), executor); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public CompletableFuture<WorkQueueStats> stats() { |
| 73 | return Tools.asyncFuture(delegateQueue.stats(), executor); |
| 74 | } |
| 75 | } |