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