/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2008 Alejandro P. Revilla
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.util;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Simple Profiler
 * @author Alejandro P. Revilla
 * @version $Id$
 */
public class Profiler implements Loggeable {
    long start, parcial;
    Collection events;
    public Profiler () {
        super();
        reset();
    }
    /**
     * reset timers
     */
    public void reset() {
        start = parcial = System.currentTimeMillis();
        events = new ArrayList();
    }
    /**
     * mark checkpoint
     * @param detail checkpoint information
     */
    public synchronized void checkPoint (String detail) {
        long now = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer (detail);
        sb.append (" [");
        sb.append (Long.toString (now - parcial));
        sb.append ('/');
        sb.append (Long.toString (now - start));
        sb.append (']');
        events.add (sb.toString());
        parcial = now;
    }
    /**
     * @return total elapsed time since last reset
     */
    public long getElapsed() {
        return System.currentTimeMillis() - start;
    }
    /**
     * @return parcial elapsed time since last reset
     */
    public long getParcial() {
        return System.currentTimeMillis() - parcial;
    }
    public void dump (PrintStream p, String indent) {
        String inner = indent + "  ";
        parcial = start;
        checkPoint ("end");
        Iterator iter = events.iterator();
        p.println (indent + "<profiler>");
        while (iter.hasNext()) 
            p.println (inner + (String) iter.next());

        p.println (indent + "</profiler>");
    }
}


@@ -862,16 +862,48 @@
 ection;%0A
+import java.util.LinkedHashMap;%0A
 import j
@@ -977,16 +977,44 @@
 Revilla%0A
+ * @author David D. Bergert%0A
  * @vers
@@ -1100,26 +1100,29 @@
 al;%0A    
-Collection
+LinkedHashMap
  events;
@@ -1324,25 +1324,29 @@
 s = new 
-ArrayList
+LinkedHashMap
 ();%0A    
@@ -1547,112 +1547,84 @@
     
-StringBuffer sb = new StringBuffer (detail);%0A        sb.append (%22 %5B%22);%0A        sb.append (Long.toString 
+Entry e = new Entry();%0A        e.setEventName(detail);%0A        e.setDuration
 (now
@@ -1634,17 +1634,16 @@
 parcial)
-)
 ;%0A      
@@ -1648,132 +1648,70 @@
     
-sb.append ('/');%0A        sb.append (Long.toString (now - start));%0A        sb.append ('%5D');%0A        events.add (sb.toString()
+e.setTotalDuration(now - start);%0A        events.put (detail, e
 );%0A 
@@ -2193,24 +2193,64 @@
 nt (%22end%22);%0A
+        Collection c = events.values();%0A
         Iter
@@ -2261,22 +2261,17 @@
  iter = 
-events
+c
 .iterato
@@ -2382,22 +2382,22 @@
 nner + (
-String
+(Entry
 ) iter.n
@@ -2402,18 +2402,29 @@
 .next())
+.toString())
 ;
-%0A
 %0A       
@@ -2470,7 +2470,1261 @@
   %7D%0A
-%7D%0A%0A
+    public Entry getEntry(String eventName) %7B%0A         return (Entry)events.get(eventName);         %0A    %7D%0A    public class Entry  %7B     %0A        String  eventName;%0A        long    duration;%0A        long    totalDuration;          %0A        Entry()%7B     %0A           eventName     = %22%22;%0A           duration      = 0L;%0A           totalDuration = 0L;        %0A        %7D  %0A       public void setEventName (String myEvent) %7B%0A            this.eventName = myEvent;%0A        %7D%0A        String getEventName () %7B%0A            return eventName;%0A        %7D    %0A        public void setDuration (long myDuration) %7B%0A            this.duration = myDuration;%0A        %7D%0A        public long getDuration () %7B%0A            return duration;%0A        %7D    %0A        public void setTotalDuration (long myTotalDuration) %7B%0A            this.totalDuration = myTotalDuration;%0A        %7D%0A        public long getTotalDuration () %7B%0A            return totalDuration;%0A        %7D    %0A        public String toString()  %7B%0A            StringBuffer sb = new StringBuffer (eventName);%0A            sb.append (%22 %5B%22);%0A            sb.append (duration);%0A            sb.append ('/');%0A            sb.append (totalDuration);%0A            sb.append ('%5D');%0A            return sb.toString();%0A        %7D            %0A    %7D%0A%7D

Modify the way Profiler stores its events, and expose access to the duration and total duration of each Profiler event.