Index: trunk/java-utils/src/main/java/de/ugoe/cs/util/StopWatch.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/StopWatch.java	(revision 1119)
+++ trunk/java-utils/src/main/java/de/ugoe/cs/util/StopWatch.java	(revision 1128)
@@ -13,5 +13,5 @@
 //   limitations under the License.
 
-package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
+package de.ugoe.cs.util;
 
 import java.io.PrintStream;
@@ -24,5 +24,10 @@
 /**
  * <p>
- * TODO comment
+ * This is a simple implementation for a stop watch that can be used for performance measures.
+ * A stop watch can be used to measure several splits. Each split is started and stopped using the
+ * same id provided to the {@link #start(String)} and {@link #stop(String)} methods. The measured
+ * durations can be retrieved afterwards using {@link #getDuration(String)}.
+ * {@link #dumpStatistics(PrintStream)} is a convenience method useful to effectively dump all
+ * information for the different splits.
  * </p>
  * 
@@ -32,85 +37,90 @@
     
     /**
-     * 
-     */
-    private Map<String, List<Long>> mWatches = new HashMap<String, List<Long>>();
-
-    /**
+     * the splits hold internally
+     */
+    private Map<String, Split> mSplits = new HashMap<String, Split>();
+
+    /**
+     * starts a split with the given id. If the split with the id is already started, an
+     * {@link IllegalStateException} is thrown.
      *
+     * @param id the id of the split to be started
+     * 
+     * @throws IllegalStateException if the split is already started
+     */
+    public void start(String id) throws IllegalStateException {
+        Split split = mSplits.get(id);
+        
+        if (split == null) {
+            split = new Split(id);
+            mSplits.put(id, split);
+        }
+        
+        split.start();
+    }
+    
+    /**
+     * stops a split with the given id. If the split with the id is already stopped, an
+     * {@link IllegalStateException} is thrown. If no split with the given id exists, an
+     * {@link IllegalArgumentException} is thrown.
      *
-     * @param id
-     */
-    public void start(String id) {
-        List<Long> timeStamps = mWatches.get(id);
-        
-        if (timeStamps == null) {
-            timeStamps = new LinkedList<Long>();
-            mWatches.put(id, timeStamps);
-        }
-        
-        if (timeStamps.size() % 2 == 0) {
-            timeStamps.add(System.currentTimeMillis());
-        }
-        else {
-            throw new IllegalStateException("watch with id " + id + " already running");
-        }
-    }
-    
-    /**
+     * @param id the id of the split to be stopped
+     * 
+     * @throws IllegalStateException    if the split is not running
+     * @throws IllegalArgumentException if the split with the given id does not exist
+     */
+    public void stop(String id) throws IllegalStateException, IllegalArgumentException {
+        Split split = mSplits.get(id);
+       
+        if (split == null) {
+            throw new IllegalArgumentException("split with id " + id + " does not exist");
+        }
+       
+        split.stop();
+    }
+    
+    /**
+     * returns the duration of a split with the given id. If the split with the id is currently
+     * running, it is stopped. If no split with the given id exists, an
+     * {@link IllegalArgumentException} is thrown.
      *
-     *
-     * @param id
-     */
-    public void stop(String id) {
-        List<Long> timeStamps = mWatches.get(id);
+     * @param id the id of the split for which the duration shall be returned
+     * 
+     * @return the duration measured for the split
+     * 
+     * @throws IllegalArgumentException if the split with the given id does not exist
+     */
+    public long getDuration(String id) throws IllegalArgumentException {
+        Split split = mSplits.get(id);
+        
+        if (split == null) {
+            throw new IllegalArgumentException("split with id " + id + " does not exist");
+        }
        
-        if (timeStamps == null) {
-            throw new IllegalArgumentException("watch with id " + id + " does not exist");
-        }
-       
-        if (timeStamps.size() % 2 == 0) {
-            throw new IllegalStateException("watch with id " + id + " already stopped");
-        }
-        else {
-            timeStamps.add(System.currentTimeMillis());
-        }
-    }
-    
-    /**
-     *
-     *
-     * @param id
-     */
-    public long getDuration(String id) {
-        List<Long> timeStamps = mWatches.get(id);
-       
-        if (timeStamps == null) {
-            throw new IllegalArgumentException("watch with id " + id + " does not exist");
-        }
-       
-        if (timeStamps.size() % 2 != 0) {
-            stop(id);
-        }
-        
-        long result = 0;
-        for (long timeStamp : timeStamps) {
-            if (result >= 0) {
-                result -= timeStamp;
-            }
-            else {
-                result += timeStamp;
-            }
-        }
-        
-        return result;
-    }
-
-    /**
-     *
-     * @param out
+        if (split.isRunning()) {
+            split.stop();
+        }
+        
+        return split.getDuration();
+    }
+
+    /**
+     * resets the stop watch and clears all splits 
+     */
+    public void reset() {
+        mSplits.clear();
+    }
+    
+    /**
+     * dumps the statistics about the splits. Splits still running are stopped. The method checks
+     * if the longest split also covers the other splits. If so, it considers this split as a
+     * kind of overall duration and dumps the proportion of all other splits to this split in
+     * percentage.
+     * 
+     * @param out the stream to dump the statistics to
      */
     public void dumpStatistics(PrintStream out) {
-        if (mWatches.size() <= 0) {
-            throw new IllegalStateException("no watches registered that could be dumped");
+        if (mSplits.size() <= 0) {
+            throw new IllegalStateException("no splits registered that could be dumped");
         }
         
@@ -118,5 +128,5 @@
         
         // get durations
-        for (String id : mWatches.keySet()) {
+        for (String id : mSplits.keySet()) {
             durations.put(id, getDuration(id));
         }
@@ -145,13 +155,10 @@
         // get longest duration and check whether it spans all other entries
         String id = sortedIds.get(sortedIds.size() - 1);
-        List<Long> timeStamps = mWatches.get(id);
-        long firstTimeStamp = timeStamps.get(0);
-        long lastTimeStamp = timeStamps.get(timeStamps.size() - 1);
-        long longestDuration = durations.get(id);
+        Split longestWatch = mSplits.get(id);
         boolean longestDurationCoversOthers = true;
         
-        for (Map.Entry<String, List<Long>> watch : mWatches.entrySet()) {
-            if ((watch.getValue().get(0) < firstTimeStamp) ||
-                (watch.getValue().get(watch.getValue().size() - 1) > lastTimeStamp))
+        for (Map.Entry<String, Split> watch : mSplits.entrySet()) {
+            if ((watch.getValue().getFirstStart() < longestWatch.getFirstStart()) ||
+                (watch.getValue().getLastStop() > longestWatch.getLastStop()))
             {
                 longestDurationCoversOthers = false;
@@ -172,20 +179,137 @@
             }
             
-            out.print(':');
-            out.print(' ');
+            out.print(": ");
             
             out.print(durations.get(sortedId));
             out.print(" ms");
             
+            out.print(" (");
+            out.print(mSplits.get(sortedId).getNoOfStarts());
+            out.print(" starts");
+            
+            //out.print(", ");
+            //out.print(1000 * durations.get(sortedId) / mSplits.get(sortedId).getNoOfStarts());
+            //out.print(" ms per 1000 starts");
+
             if (longestDurationCoversOthers) {
-                out.print(" (");
+                out.print(", ");
                 out.print(DecimalFormat.getPercentInstance().format
-                              ((double) durations.get(sortedId) / longestDuration));
-                out.print(" of overall duration)");
-            }
-            out.println();
+                              ((double) durations.get(sortedId) / longestWatch.getDuration()));
+                out.print(" of overall duration");
+            }
+            
+            out.println(')');
         }
         
         out.println();
     }
+    
+    /**
+     * internally used to store splits
+     */
+    private static class Split {
+        
+        /**
+         * the id of the split
+         */
+        private String id;
+        
+        /**
+         * the system time of the first start of the split
+         */
+        private long firstStart = -1;
+        
+        /**
+         * the system time of the last start of the split
+         */
+        private long lastStart = -1;
+        
+        /**
+         * the system time of the last stop of the split
+         */
+        private long lastStop = -1;
+        
+        /**
+         * the duration so far for the split (excluding the time since the last start)
+         */
+        private long duration = 0;
+        
+        /**
+         * the number of starts or the splits
+         */
+        private long noOfStarts = 0;
+        
+        /**
+         * initializes the split with its id
+         */
+        private Split(String id) {
+            this.id = id;
+        }
+        
+        /**
+         * starts the split if it is not already started
+         */
+        private void start() throws IllegalStateException {
+            if (lastStart > -1) {
+                throw new IllegalStateException("split with id " + id + " already running");
+            }
+            
+            lastStart = System.currentTimeMillis();
+            
+            if (firstStart < 0) {
+                firstStart = lastStart;
+            }
+            
+            noOfStarts++;
+        }
+        
+        /**
+         * checks if the split is currently running
+         */
+        private boolean isRunning() {
+            return (lastStart > -1);
+        }
+        
+        /**
+         * stops the split if it is not already stopped
+         */
+        private void stop() throws IllegalStateException {
+            if (lastStart < 0) {
+                throw new IllegalStateException("split with id " + id + " not running");
+            }
+            
+            lastStop = System.currentTimeMillis();
+            duration += lastStop - lastStart;
+            lastStart = -1;
+        }
+        
+        /**
+         * returns the fist start of the split
+         */
+        private long getFirstStart() {
+            return firstStart;
+        }
+
+        /**
+         * returns the last stop of the split
+         */
+        private long getLastStop() {
+            return lastStop;
+        }
+
+        /**
+         * returns the duration of the split measured so far excluding the time since the last 
+         * start if the split is currently started
+         */
+        private long getDuration() {
+            return duration;
+        }
+
+        /**
+         * returns the number of starts for the split
+         */
+        private long getNoOfStarts() {
+            return noOfStarts;
+        }
+    }
 }
