2 * ============LICENSE_START====================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 package org.onap.aaf.misc.env;
\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
31 * The implementor of this class can easily be stored in efficient
\r
32 * mechanisms to minimize impact of Auditing on performance.
\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 public final Object[] values;
\r
45 * The name is as it will appear when written to output (abstract method)
\r
47 * The flag is an integer which can be System type (XML, REMOTE, etc), or End User defined for reporting purposes
\r
53 public TimeTaken(String name, int flag, Object ... values) {
\r
54 start = System.nanoTime();
\r
57 this.values = values;
\r
63 * Call this when process is done to state ending time.<p>
\r
65 * It is <i>exceedingly prudent</i> to wrap the process called with a try-finally:<p>
\r
68 * TimeTaken tt = env.startSubTime();
\r
70 * process.me(); // code to be timed.
\r
76 public void done() {
\r
77 end = System.nanoTime();
\r
82 * For sizable contents, set the size. Implementations can simply write a no-op if they don't wish to
\r
87 public void size(long theSize) {
\r
92 * Give readonly access to End, which isn't final
\r
100 * Time is taken in NanoSeconds. This method converts to decimals of Milliseconds
\r
103 public float millis() {
\r
104 return (end-start)/1000000f;
\r
107 * Write self to a String Builder (for making Audits)
\r
110 public abstract void output(StringBuilder sb);
\r
115 public String toString() {
\r
116 return name + ' ' + millis() + "ms " + (size>0?Long.toString(size):"");
\r