Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / workflow / service / WorkflowContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.bpmn.common.workflow.service;
22
23 import java.util.concurrent.Delayed;
24 import java.util.concurrent.TimeUnit;
25
26 import org.jboss.resteasy.spi.AsynchronousResponse;
27
28 /**
29  * @version 1.0
30  * Workflow context object used to send timeout response, if workflow instance does not write the response in time
31  */
32 public class WorkflowContext implements Delayed {
33         private final String processKey;
34         private final String requestId;
35         private final AsynchronousResponse asynchronousResponse;
36         private final long startTime;
37         private final long timeout;
38         
39         public WorkflowContext(String processKey, String requestId,
40                         AsynchronousResponse asynchronousResponse, long timeout) {
41                 this.processKey = processKey;
42                 this.requestId = requestId;
43                 this.asynchronousResponse = asynchronousResponse;
44                 this.timeout = timeout;
45                 this.startTime = System.currentTimeMillis();
46         }
47         
48         public String getRequestId() {
49                 return requestId;
50         }
51
52         public String getProcessKey() {
53                 return processKey;
54         }
55
56         public AsynchronousResponse getAsynchronousResponse() {
57                 return asynchronousResponse;
58         }
59
60         public long getTimeout() {
61                 return timeout;
62         }
63
64         public long getStartTime() {
65                 return startTime;
66         }
67
68         /**
69          * Required implementation by Delay queue
70          * Returns the elapsed time for this context
71          */
72         @Override
73         public long getDelay(TimeUnit unit) {
74                 // 0 or negative means this object is considered to be expired
75                 return unit.convert(startTime + timeout - System.currentTimeMillis(), unit);
76         }
77
78         /**
79          * Required implementation by Delay queue
80          * Compares the object to determine whether the object can be marked as expired
81          */
82         @Override
83         public int compareTo(Delayed object) {
84                 WorkflowContext that = (WorkflowContext) object;
85                 long thisEndTime = startTime + timeout;
86                 long thatEndTime = that.startTime + that.timeout;
87
88                 if (thisEndTime < thatEndTime) {
89                         return -1;
90                 } else if (thisEndTime > thatEndTime) {
91                         return 1;
92                 } else {
93                         return 0;
94                 }
95         }
96 }