package com.tikal.jenkins.plugins.multijob.counters;

import java.util.HashMap;
import java.util.Map;


/**
 * Helper methods for counters.
 */
public final class CounterHelper {
    private CounterHelper() {}

    /**
     * Put the counters in phase variables,
     * add the counters to the multijob variables,
     * and merge all in previous variables.
     */
    public static Map<String, String> putPhaseAddMultijobAndMergeTheRest(
        final String phaseName,
        final Map<String, String> incomingVars,
        final Map<String, String> previousEnvVars) {
        final String safePhaseName = phaseName.replaceAll("[^A-Za-z0-9]", "_").toUpperCase();
        final Map<String, String> mixtured = new HashMap<String, String>(previousEnvVars);

        // It is necessary iterate through the incomingVars map, because
        // it may contain counters and non counter variables.
        for(Map.Entry<String, String> entry: incomingVars.entrySet()) {
            String key = entry.getKey();
            final CounterKey counterKey = CounterKey.safetyValueOf(key);
            String incomingValue = entry.getValue();
            if (counterKey == null) {
                mixtured.put(key, incomingValue);
            } else {
                key = counterKey.getPhaseKey();
                final String previousValue = zeroIfNull(previousEnvVars.get(key));
                incomingValue = zeroIfNull(incomingValue);
                // Updating PHASE_????? and PHASENAME_?????
                mixtured.put(key, incomingValue);
                mixtured.put(safePhaseName + "_" + counterKey.name(), incomingValue);

                // Updating MULTIJOB_?????_KEY
                key = counterKey.getMultiJobKey();
                mixtured.put(
                    key,
                    String.valueOf(Integer.parseInt(incomingValue) + Integer.parseInt(previousValue))
                );
            }
        }
        if (mixtured.containsKey(CounterManager.PHASE_RESULT)) {
            mixtured.put(safePhaseName + "_RESULT", mixtured.get(CounterManager.PHASE_RESULT));
        }

        return mixtured;
    }

    private static String zeroIfNull(String value) {
        if ( value == null || value.trim().length() == 0 ) {
            return "0";
        }
        return value;
    }
}
@@ -96,16 +96,51 @@
 l.Map;%0A%0A
+import hudson.model.BuildListener;%0A
 %0A/**%0A * 
@@ -412,16 +412,29 @@
  static 
+synchronized 
 Map%3CStri
@@ -481,16 +481,48 @@
 heRest(%0A
+        BuildListener listener,%0A
         
@@ -1371,38 +1371,32 @@
                 
-final 
 String previousV
@@ -1491,32 +1491,231 @@
 incomingValue);%0A
+//listener.getLogger().println(%22putPhase %22 + phaseName + %22: %22 + counterKey.name() + %22/%22 + counterKey.getPhaseKey() + %22/%22 + counterKey.getMultiJobKey() + %22 = %22 + previousValue + %22/%22 + incomingValue);%0A
                 
@@ -1989,24 +1989,94 @@
 tiJobKey();%0A
+                previousValue = zeroIfNull(previousEnvVars.get(key));%0A
             

Fix the MULTIJOB accumulation logic. Previously doesn't add to the previous value.