blob: 678c63eb81983b140099ff1a4af523d42f0aaaa7 [file] [log] [blame]
Madan Jampani35708a92016-07-06 10:48:19 -07001/*
2 * Copyright 2016-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 */
16package org.onosproject.store.service;
17
18import java.util.function.Function;
19
20import com.google.common.base.MoreObjects;
21
22/**
23 * {@link WorkQueue} task.
24 *
25 * @param <E> task payload type.
26 */
27public 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 Milkeybb23e0b2016-08-02 17:00:21 -070064 *
65 * @param <F> future type
Madan Jampani35708a92016-07-06 10:48:19 -070066 * @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}