Merge "Fix Resources Jersey tests"
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / StopWatch.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.dmaap\r
4  * ================================================================================\r
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.onap.dmaap.dbcapi.service;\r
21 \r
22 import static com.att.eelf.configuration.Configuration.MDC_BEGIN_TIMESTAMP;\r
23 import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME;\r
24 import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP;\r
25 \r
26 import java.text.SimpleDateFormat;\r
27 import java.util.Date;\r
28 import java.util.TimeZone;\r
29 import org.slf4j.MDC;\r
30 \r
31 \r
32 public class StopWatch {\r
33 \r
34     private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";\r
35     private final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);\r
36     private final static TimeZone utc = TimeZone.getTimeZone("UTC");\r
37 \r
38     private long startTimestamp;\r
39     private long elapsedTime;\r
40 \r
41     static {\r
42         isoFormatter.setTimeZone(utc);\r
43     }\r
44 \r
45     public void start() {\r
46         startTimestamp = System.currentTimeMillis();\r
47         MDC.put(MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(startTimestamp)));\r
48     }\r
49 \r
50     public void stop() {\r
51         long endTimestamp = System.currentTimeMillis();\r
52         elapsedTime = endTimestamp - startTimestamp;\r
53         MDC.put(MDC_END_TIMESTAMP, isoFormatter.format(new Date(endTimestamp)));\r
54         MDC.put(MDC_ELAPSED_TIME, String.valueOf(elapsedTime));\r
55     }\r
56 \r
57     public void resetElapsedTime() {\r
58         elapsedTime = 0;\r
59     }\r
60 \r
61     public long getElapsedTime() {\r
62         return elapsedTime;\r
63     }\r
64 }\r