001/* 002 * JKNIV, SQLegance keeping queries maintainable. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.sqlegance.builder.xml; 021 022import java.util.concurrent.atomic.AtomicLong; 023 024import net.sf.jkniv.sqlegance.Statistical; 025 026/** 027 * 028 * @author Alisson Gomes 029 * @since 0.6.0 030 */ 031public abstract class AbstractStats implements Statistical 032{ 033 private AtomicLong max; 034 private AtomicLong min; 035 private AtomicLong total; 036 private AtomicLong count; 037 private AtomicLong firstTime; 038 private AtomicLong lastTime; 039 private AtomicLong totalException; 040 private Exception firstException; 041 private Exception lastException; 042 043 public AbstractStats() 044 { 045 this.max = new AtomicLong(); 046 this.min = new AtomicLong(Long.MAX_VALUE); 047 this.total = new AtomicLong(); 048 this.firstTime = null; 049 this.lastTime = new AtomicLong(); 050 this.count = new AtomicLong(); 051 this.totalException = new AtomicLong(); 052 } 053 054 @Override 055 public void add(long time) 056 { 057 if (time > this.max.get()) 058 this.max.set(time); 059 if (time < this.min.get()) 060 this.min.set(time); 061 if (firstTime == null) 062 this.firstTime = new AtomicLong(time); 063 this.lastTime.set(time); 064 this.count.getAndIncrement(); 065 this.total.addAndGet(time); 066 } 067 068 @Override 069 public void add(Exception e) 070 { 071 this.totalException.incrementAndGet(); 072 if (e != null) 073 { 074 if (this.firstException == null) 075 this.firstException = e; 076 077 this.lastException = e; 078 } 079 } 080 081 @Override 082 public long getMaxTime() 083 { 084 return this.max.get(); 085 } 086 087 @Override 088 public long getMinTime() 089 { 090 return this.min.get(); 091 } 092 093 @Override 094 public long getAvgTime() 095 { 096 return this.total.get() / this.count.get(); 097 } 098 099 @Override 100 public long getTotalTime() 101 { 102 return this.total.get(); 103 } 104 105 @Override 106 public long getCount() 107 { 108 return this.count.get(); 109 } 110 111 @Override 112 public long getFirstTime() 113 { 114 return (this.firstTime != null ? this.firstTime.get() : 0L); 115 } 116 117 @Override 118 public long getLastTime() 119 { 120 return this.lastTime.get(); 121 } 122 123 @Override 124 public long getTotalException() 125 { 126 return this.totalException.get(); 127 } 128 129 @Override 130 public Exception getFirstException() 131 { 132 return this.firstException; 133 } 134 135 @Override 136 public Exception getLastException() 137 { 138 return this.lastException; 139 } 140 141 @Override 142 public String toString() 143 { 144 return "AbstractStats [max=" + max + ", min=" + min + ", total=" + total + ", count=" + count + ", firstTime=" 145 + firstTime + ", lastTime=" + lastTime + ", totalException=" + totalException + ", firstException=" 146 + (firstException != null ? firstException.getMessage() : "") 147 + ", lastException=" + (lastException != null ? lastException.getMessage() : "") + "]"; 148 } 149 150 151}