LogWatch.java

  1. /*
  2.  * Copyright (C) 2003-2013 eXo Platform SAS.
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU Affero General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU Affero General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Affero General Public License
  15.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package org.exoplatform.social.common.service.utils;

  18. import static java.util.concurrent.TimeUnit.NANOSECONDS;

  19. import java.util.concurrent.TimeUnit;



  20. public class LogWatch implements Cloneable {

  21.   private static final long NANOS_IN_A_MILLI = 1000000L;

  22.   private long              startTime;

  23.   private long              nanoStartTime;

  24.   private long              elapsedTime;

  25.   private String            process;

  26.   private String            message;

  27.   public LogWatch() {
  28.     this("", null);
  29.   }

  30.   public LogWatch(String tag) {
  31.     this(tag, null);
  32.   }

  33.   public LogWatch(String tag, String message) {
  34.     this(System.currentTimeMillis(), -1L, tag, message);
  35.   }

  36.   public LogWatch(long startTime, long elapsedTime, String process, String message) {
  37.     this.startTime = startTime;
  38.     this.nanoStartTime = (elapsedTime == -1L) ? System.nanoTime() : -1L;
  39.     this.elapsedTime = elapsedTime;
  40.     this.process = process;
  41.     this.message = message;
  42.   }

  43.   public void start() {
  44.     startTime = System.currentTimeMillis();
  45.     nanoStartTime = System.nanoTime();
  46.     elapsedTime = -1L;
  47.   }
  48.  
  49.   public long getStartTime() {
  50.     return startTime;
  51.   }

  52.   public long elapsedTime() {
  53.     elapsedTime = (elapsedTime == -1L) ? (System.nanoTime() - nanoStartTime) / NANOS_IN_A_MILLI : elapsedTime;
  54.     return elapsedTime;
  55.   }
  56.  
  57.   public long getElapsedTime() {
  58.     return elapsedTime;
  59.   }
  60.  
  61.   public long elapsedTime(TimeUnit timeUnit) {
  62.     return timeUnit.convert(getElapsedTime(), NANOSECONDS);
  63.   }

  64.   public String stop() {
  65.     elapsedTime = elapsedTime();
  66.     return this.toString();
  67.   }
  68.  
  69.   public String getMessage() {
  70.     return message;
  71.   }

  72.   public String toString() {
  73.     String message = getMessage();
  74.     return "start[" + getStartTime() + "] time[" + elapsedTime() + "] process[" + getProcess()
  75.         + ((message == null) ? "]" : "] message[" + message + "]");
  76.   }

  77.   private String getProcess() {
  78.     return process;
  79.   }

  80.   public LogWatch clone() {
  81.     try {
  82.       return (LogWatch) super.clone();
  83.     } catch (CloneNotSupportedException cnse) {
  84.       throw new Error("Unexpected CloneNotSupportedException");
  85.     }
  86.   }
  87.  
  88.   public String toString(long value, TimeUnit timeUnit) {
  89.     return String.format("%s %s", value, abbreviate(timeUnit));
  90.   }

  91.   private static String abbreviate(TimeUnit unit) {
  92.     switch (unit) {
  93.       case NANOSECONDS:
  94.         return "ns";
  95.       case MILLISECONDS:
  96.         return "ms";
  97.       case SECONDS:
  98.         return "sec";
  99.       default:
  100.         throw new AssertionError();
  101.     }
  102.   }


  103.   public boolean equals(Object o) {
  104.     if (this == o) {
  105.       return true;
  106.     }
  107.     if (!(o instanceof LogWatch)) {
  108.       return false;
  109.     }

  110.     LogWatch stopWatch = (LogWatch) o;

  111.     if (elapsedTime != stopWatch.elapsedTime) {
  112.       return false;
  113.     }
  114.     if (startTime != stopWatch.startTime) {
  115.       return false;
  116.     }
  117.     if (nanoStartTime != stopWatch.nanoStartTime) {
  118.       return false;
  119.     }
  120.     if (message != null ? !message.equals(stopWatch.message) : stopWatch.message != null) {
  121.       return false;
  122.     }
  123.     if (process != null ? !process.equals(stopWatch.process) : stopWatch.process != null) {
  124.       return false;
  125.     }

  126.     return true;
  127.   }

  128.   public int hashCode() {
  129.     int result = (int) (startTime ^ (startTime >>> 32));
  130.     result = 31 * result + (int) (nanoStartTime ^ (nanoStartTime >>> 32));
  131.     result = 31 * result + (int) (elapsedTime ^ (elapsedTime >>> 32));
  132.     result = 31 * result + (process != null ? process.hashCode() : 0);
  133.     result = 31 * result + (message != null ? message.hashCode() : 0);
  134.     return result;
  135.   }

  136. }