Extract class StopWatch from ApiService
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / StopWatch.java
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java b/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java
new file mode 100644 (file)
index 0000000..6dc8fe9
--- /dev/null
@@ -0,0 +1,64 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.dmaap\r
+ * ================================================================================\r
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+package org.onap.dmaap.dbcapi.service;\r
+\r
+import static com.att.eelf.configuration.Configuration.MDC_BEGIN_TIMESTAMP;\r
+import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME;\r
+import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP;\r
+\r
+import java.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.TimeZone;\r
+import org.slf4j.MDC;\r
+\r
+\r
+public class StopWatch {\r
+\r
+    private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";\r
+    private final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);\r
+    private final static TimeZone utc = TimeZone.getTimeZone("UTC");\r
+\r
+    private long startTimestamp;\r
+    private long elapsedTime;\r
+\r
+    static {\r
+        isoFormatter.setTimeZone(utc);\r
+    }\r
+\r
+    public void start() {\r
+        startTimestamp = System.currentTimeMillis();\r
+        MDC.put(MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(startTimestamp)));\r
+    }\r
+\r
+    public void stop() {\r
+        long endTimestamp = System.currentTimeMillis();\r
+        elapsedTime = endTimestamp - startTimestamp;\r
+        MDC.put(MDC_END_TIMESTAMP, isoFormatter.format(new Date(endTimestamp)));\r
+        MDC.put(MDC_ELAPSED_TIME, String.valueOf(elapsedTime));\r
+    }\r
+\r
+    public void resetElapsedTime() {\r
+        elapsedTime = 0;\r
+    }\r
+\r
+    public long getElapsedTime() {\r
+        return elapsedTime;\r
+    }\r
+}\r