[AAF-23] Initial code import
[aaf/inno.git] / env / src / main / java / com / att / inno / env / TimeTaken.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.inno.env;\r
25 \r
26 /**\r
27  * <h1>TimeTaken</h1>\r
28  * This simple interface allows for many different kinds of \r
29  * Audit Logs to be accomplished, by assuming that the creation\r
30  * of this object indicates "start", and the calling of "done" \r
31  * ends.\r
32  * \r
33  * The implementor of this class can easily be stored in efficient\r
34  * mechanisms to minimize impact of Auditing on performance.\r
35  * \r
36  *\r
37  */\r
38 public abstract class TimeTaken {\r
39         public final long start;\r
40         protected long end, size;\r
41         public final int flag;\r
42         public final String name;\r
43         \r
44         /**\r
45          * The name is as it will appear when written to output (abstract method)\r
46          * \r
47          * The flag is an integer which can be System type (XML, REMOTE, etc), or End User defined for reporting purposes \r
48          * \r
49          * @param name\r
50          * @param flag\r
51          */\r
52         public TimeTaken(String name, int flag) {\r
53                 start = System.nanoTime();\r
54                 this.flag = flag;\r
55                 this.name = name;\r
56                 size = -1;\r
57         }\r
58 \r
59 \r
60         /**\r
61          * Call this when process is done to state ending time.<p>\r
62          * \r
63          * It is <i>exceedingly prudent</i> to wrap the process called with a try-finally:<p>\r
64          * \r
65          * <pre>\r
66          *   TimeTaken tt = env.startSubTime();\r
67          *   try {\r
68          *       process.me(); // code to be timed.\r
69          *   } finally {\r
70          *       tt.done();\r
71          *   }\r
72          * </pre>\r
73          */\r
74         public void done() {\r
75                 end = System.nanoTime();\r
76         }\r
77         \r
78         \r
79         /**\r
80          * For sizable contents, set the size.  Implementations can simply write a no-op if they don't wish to \r
81          * store the size. \r
82          * \r
83          * @param size\r
84          */\r
85         public void size(long theSize) {\r
86                 size = theSize;\r
87         }\r
88         \r
89         /**\r
90          * Give readonly access to End, which isn't final\r
91          * @return\r
92          */\r
93         public long end() {\r
94                 return end;\r
95         }\r
96         \r
97         /**\r
98          * Time is taken in NanoSeconds.  This method converts to decimals of Milliseconds\r
99          * @return\r
100          */\r
101         public float millis() {\r
102                 return (end-start)/1000000f;\r
103         }\r
104         /**\r
105          * Write self to a String Builder (for making Audits)\r
106          * @param sb\r
107          */\r
108         public abstract void output(StringBuilder sb);\r
109         \r
110         /**\r
111          * For Debugging\r
112          */\r
113         public String toString() {\r
114                 return name + ' ' + millis() + "ms " + (size>0?Long.toString(size):"");\r
115         }\r
116         \r
117 }               \r