78f4a6172893fa8d3a49c9c6b34e3c2aee8e210d
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / TimeTaken.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 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  */\r
21 \r
22 package org.onap.aaf.misc.env;\r
23 \r
24 /**\r
25  * <h1>TimeTaken</h1>\r
26  * This simple interface allows for many different kinds of \r
27  * Audit Logs to be accomplished, by assuming that the creation\r
28  * of this object indicates "start", and the calling of "done" \r
29  * ends.\r
30  * \r
31  * The implementor of this class can easily be stored in efficient\r
32  * mechanisms to minimize impact of Auditing on performance.\r
33  * \r
34  * @author Jonathan\r
35  *\r
36  */\r
37 public abstract class TimeTaken {\r
38         public final long start;\r
39         protected long end, size;\r
40         public final int flag;\r
41         public final String name;\r
42         \r
43         /**\r
44          * The name is as it will appear when written to output (abstract method)\r
45          * \r
46          * The flag is an integer which can be System type (XML, REMOTE, etc), or End User defined for reporting purposes \r
47          * \r
48          * @param name\r
49          * @param flag\r
50          */\r
51         public TimeTaken(String name, int flag) {\r
52                 start = System.nanoTime();\r
53                 this.flag = flag;\r
54                 this.name = name;\r
55                 size = -1;\r
56         }\r
57 \r
58 \r
59         /**\r
60          * Call this when process is done to state ending time.<p>\r
61          * \r
62          * It is <i>exceedingly prudent</i> to wrap the process called with a try-finally:<p>\r
63          * \r
64          * <pre>\r
65          *   TimeTaken tt = env.startSubTime();\r
66          *   try {\r
67          *       process.me(); // code to be timed.\r
68          *   } finally {\r
69          *       tt.done();\r
70          *   }\r
71          * </pre>\r
72          */\r
73         public void done() {\r
74                 end = System.nanoTime();\r
75         }\r
76         \r
77         \r
78         /**\r
79          * For sizable contents, set the size.  Implementations can simply write a no-op if they don't wish to \r
80          * store the size. \r
81          * \r
82          * @param size\r
83          */\r
84         public void size(long theSize) {\r
85                 size = theSize;\r
86         }\r
87         \r
88         /**\r
89          * Give readonly access to End, which isn't final\r
90          * @return\r
91          */\r
92         public long end() {\r
93                 return end;\r
94         }\r
95         \r
96         /**\r
97          * Time is taken in NanoSeconds.  This method converts to decimals of Milliseconds\r
98          * @return\r
99          */\r
100         public float millis() {\r
101                 return (end-start)/1000000f;\r
102         }\r
103         /**\r
104          * Write self to a String Builder (for making Audits)\r
105          * @param sb\r
106          */\r
107         public abstract void output(StringBuilder sb);\r
108         \r
109         /**\r
110          * For Debugging\r
111          */\r
112         public String toString() {\r
113                 return name + ' ' + millis() + "ms " + (size>0?Long.toString(size):"");\r
114         }\r
115         \r
116 }               \r