Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 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.service; |
| 17 | |
| 18 | import java.util.function.Function; |
| 19 | |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | |
| 22 | /** |
| 23 | * {@link WorkQueue} task. |
| 24 | * |
| 25 | * @param <E> task payload type. |
| 26 | */ |
| 27 | public class Task<E> { |
| 28 | private final E payload; |
| 29 | private final String taskId; |
| 30 | |
| 31 | private Task() { |
| 32 | payload = null; |
| 33 | taskId = null; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Constructs a new task instance. |
| 38 | * @param taskId task identifier |
| 39 | * @param payload task payload |
| 40 | */ |
| 41 | public Task(String taskId, E payload) { |
| 42 | this.taskId = taskId; |
| 43 | this.payload = payload; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the task identifier. |
| 48 | * @return task id |
| 49 | */ |
| 50 | public String taskId() { |
| 51 | return taskId; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the task payload. |
| 56 | * @return task payload |
| 57 | */ |
| 58 | public E payload() { |
| 59 | return payload; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Maps task from one payload type to another. |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 64 | * |
| 65 | * @param <F> future type |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 66 | * @param mapper type mapper. |
| 67 | * @return mapped task. |
| 68 | */ |
| 69 | public <F> Task<F> map(Function<E, F> mapper) { |
| 70 | return new Task<>(taskId, mapper.apply(payload)); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String toString() { |
| 75 | return MoreObjects.toStringHelper(getClass()) |
| 76 | .add("taskId", taskId) |
| 77 | .add("payload", payload) |
| 78 | .toString(); |
| 79 | } |
| 80 | } |