/*******************************************************************************
 * Copyright 2015 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *******************************************************************************/

package org.mousephenotype.cda.utilities;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author mrelac
 * 
 * This class encapsulates the code and data necessary to represent a run status.
 * Use this class to accumulate error and warning messages and optionally to track a success count.
 * 
 */
public class RunStatus {
    private final Set<String>   errorMessages;
    private final Set<String>   warningMessages;
    public        AtomicInteger successCount;

    public RunStatus() {
        errorMessages = ConcurrentHashMap.newKeySet();
        warningMessages = ConcurrentHashMap.newKeySet();
        successCount = new AtomicInteger(0);
    }

    public Set<String> getErrorMessages() {
        return errorMessages;
    }

    public Set<String> getWarningMessages() {
        return warningMessages;
    }
    
    public void add(RunStatus runStatus) {
        errorMessages.addAll(runStatus.errorMessages);
        warningMessages.addAll(runStatus.warningMessages);
        successCount.getAndAdd(runStatus.successCount.get());
    }

    public void addError(String errorMessage) {
        this.errorMessages.add(errorMessage);
    }
    
    public void addError(Set<String> errorMessages) {
        this.errorMessages.addAll(errorMessages);
    }

    public void addWarning(String warningMessage) {
        this.warningMessages.add(warningMessage);
    }
    
    public void addWarning(Set<String> warningMessage) {
        this.warningMessages.addAll(warningMessage);
    }
    
    public boolean hasErrors() {
        return (errorMessages.size() > 0);
    }
    
    public boolean hasWarnings() {
        return (warningMessages.size() > 0);
    }
    
    @Override
    public String toString() {
        return "RunStatus {" + "errors=" + errorMessages.size() + ", warnings=" + warningMessages.size() + ", successCount=" + successCount + "}";
    }

    /**
     * @return  All of the error messages in a String, each message terminated
     * with a newline, suitable for display. Returns an empty string if there
     * are no error messages.
     */
    public String toStringErrorMessages() {
        StringBuilder sb = new StringBuilder();
        for (String s : errorMessages) {
            sb.append(s);
            sb.append("\n");
        }

        return sb.toString();
    }

    /**
     * @return  All of the warning messages in a String, each message terminated
     * with a newline, suitable for display. Returns an empty string if there
     * are no warning messages.
     */
    public String toStringWarningMessages() {
        StringBuilder sb = new StringBuilder();
        for (String s : warningMessages) {
            sb.append(s);
            sb.append("\n");
        }

        return sb.toString();
    }
}
@@ -834,58 +834,15 @@
 til.
-Set;%0Aimport java.util.concurrent.Concurrent
 Hash
-Map
+Set
 ;%0Aim
@@ -860,39 +860,11 @@
 til.
-concurrent.atomic.AtomicInteger
+Set
 ;%0A%0A/
@@ -1220,29 +1220,28 @@
         
-AtomicInteger
+int         
  success
@@ -1301,35 +1301,21 @@
 s = 
-ConcurrentHashMap.newKey
+new Hash
 Set
+%3C%3E
 ();%0A
@@ -1344,35 +1344,21 @@
 s = 
-ConcurrentHashMap.newKey
+new Hash
 Set
+%3C%3E
 ();%0A
@@ -1384,28 +1384,9 @@
 t = 
-new AtomicInteger(0)
+0
 ;%0A  
@@ -1741,19 +1741,12 @@
 ount
-.getAndAdd(
+ += 
 runS
@@ -1767,15 +1767,8 @@
 ount
-.get())
 ;%0A  

Remove AtomicInteger usage from RunStatus as it breaks many tests and is not needed.