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