TraceElement.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 java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.concurrent.TimeUnit;
  21. import java.util.concurrent.atomic.AtomicLong;

  22. import org.apache.commons.lang.StringUtils;

  23. public class TraceElement {
  24.  
  25.   private AtomicLong elapsedTime = new AtomicLong(0);
  26.   private final String traceName;
  27.   private final LogWatch logWatch;
  28.   private TraceElement parent;
  29.  
  30.   private List<TraceElement> children;

  31.   private TraceElement(String traceName) {
  32.     logWatch = new LogWatch(traceName);
  33.     this.traceName = traceName;
  34.   }
  35.  
  36.   public List<TraceElement> getChildren() {
  37.     if (children == null) {
  38.       children = new ArrayList<TraceElement>();
  39.     }
  40.     return children;
  41.   }
  42.  
  43.   public static TraceElement getInstance(String traceName) {
  44.     return new TraceElement(traceName);
  45.   }
  46.  
  47.   public TraceElement addChild(String traceName) {
  48.     TraceElement child = getInstance(traceName);
  49.     child.setParrent(this);
  50.    
  51.     //
  52.     getChildren().add(child);
  53.    
  54.     //
  55.     return child;
  56.   }
  57.  
  58.   public void start() {
  59.     logWatch.start();
  60.   }
  61.  
  62.   public void end() {
  63.     this.elapsedTime.set(logWatch.elapsedTime());
  64.   }
  65.  
  66.   public long getElapsedTime() {
  67.     return this.elapsedTime.get();
  68.   }
  69.  
  70.   public TraceElement getParent() {
  71.     return parent;
  72.   }
  73.  
  74.   public boolean hasParent() {
  75.     return this.parent != null;
  76.   }
  77.  
  78.   public void setParrent(TraceElement parent) {
  79.     this.parent = parent;
  80.   }
  81.  
  82.   public String getName() {
  83.     return this.traceName;
  84.   }
  85.  
  86.   @Override
  87.   public boolean equals(Object o) {
  88.     if (this == o) {
  89.       return true;
  90.     }
  91.    
  92.     if (!(o instanceof TraceElement)) {
  93.       return false;
  94.     }

  95.     TraceElement element = (TraceElement) o;

  96.     if (traceName != element.traceName) {
  97.       return false;
  98.     }

  99.     return true;
  100.   }
  101.  
  102.   @Override
  103.   public int hashCode() {
  104.     int result = super.hashCode();
  105.     result = 31 * result + (traceName != null ? traceName.hashCode() : 0);
  106.     return result;
  107.   }
  108.  
  109.   @Override
  110.   public String toString() {
  111.     return String.format("%s - %s ", this.traceName, logWatch.toString(getElapsedTime(), TimeUnit.MILLISECONDS));
  112.   }
  113.  
  114.   public String toReport() {
  115.     StringBuffer out = new StringBuffer();
  116.     if (getChildren().size() > 0) {
  117.       out.append("{");
  118.      
  119.       int len = getChildren().size();
  120.       int index = 0;
  121.      
  122.       for(TraceElement e : getChildren()) {
  123.         if (++index > 1) out.append("{space}");
  124.          
  125.         out.append(e.toString());
  126.        
  127.         if (index < len) out.append("\n");
  128.        
  129.         elapsedTime.addAndGet(e.getElapsedTime());
  130.        
  131.       }
  132.       out.append("}");
  133.     }
  134.    
  135.     String head = String.format("%s - %s ", this.traceName, logWatch.toString(getElapsedTime(), TimeUnit.MILLISECONDS));
  136.     String tail = StringUtils.replace(out.toString(), "{space}", StringUtils.repeat(" ", head.length() + 1));
  137.    
  138.     return head + tail;
  139.   }
  140. }