package com.suse.saltstack.netapi.results;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import com.suse.saltstack.netapi.parser.JsonParser;

/**
 *
 * Represents the SaltStack's result information structure.
 *
 */

public class ResultInfo {

    @SerializedName("Function")
    private String function;

    @SerializedName("StartTime")
    @JsonAdapter(JsonParser.JobStartTimeJsonAdapter.class)
    private Date startTime;

    @SerializedName("Arguments")
    private List<Object> arguments;

    @SerializedName("Minions")
    private final HashSet<String> minions = new HashSet<>();

    @SerializedName("User")
    private String user;

    @SerializedName("Target")
    private String target;

    @SerializedName("Result")
    private HashMap<String, Result<Object>> rawResults;
    private transient final HashMap<String, Object> resultsCache = new HashMap<>();

    /**
     * Returns a list of arguments supplied function.
     *
     * @return list of Objects
     */
    public List<Object> getArguments() {
        return arguments;
    }

    /**
     * Returns function name.
     *
     * @return function name
     */
    public String getFunction() {
        return function;
    }

    /**
     * Returns set of minions this job was submitted to.
     *
     * @return minion set
     */
    public Set<String> getMinions() {
        return minions;
    }

    /**
     * Returns start time of the job.
     *
     * @return job start date
     */
    public Date getStartTime() {
        return startTime;
    }

    /**
     * Returns target of job submission.
     *
     * @return job submission target
     */
    public String getTarget() {
        return target;
    }

    /**
     * Returns user associated with this job.
     *
     * @return job's user
     */
    public String getUser() {
        return user;
    }

    /**
     * Returns job's return value that is associated with supplied minion
     * as an {@link Optional}. If a minion has not returned a response, an empty
     * value is returned.
     *
     * @param minion - name of a minion
     *
     * @return {@link Optional} associated with the result from a given minion.
     */
    public Optional<Object> getResult(String minion) {
        Result<Object> result;
        if (rawResults == null || (result = rawResults.get(minion)) == null) {
            return Optional.<Object>empty();
        }

        return Optional.<Object>ofNullable(result.getResult());
    }

    /**
     * Returns result map of available {@link Object} associated with each minion.
     * Minions that have yet to return a value are not included in this mapping.
     *
     * @return Map&lt;String, Object&gt; of available job return values.
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> getResults() {
        if (rawResults == null || resultsCache.size() == rawResults.size()) {
            return resultsCache;
        }

        resultsCache.clear();
        resultsCache.putAll(rawResults);
        resultsCache.replaceAll(
                (String key, Object result) -> ((Result<Object>) result).getResult());

        return resultsCache;
    }

    /**
     * Returns a set of minions that have yet to return a result.
     *
     * @return set of minions that have not returned a result.
     */
    public Set<String> getPendingMinions() {
        HashSet<String> pend = new HashSet<>(minions);

        pend.removeAll(rawResults.keySet());
        return pend;
    }
}

@@ -363,19 +363,16 @@
 r;%0A%0A/**%0A
- *%0A
  * Repre
@@ -429,13 +429,9 @@
 .%0A *
-%0A */%0A
+/
 %0Apub
@@ -2297,23 +2297,16 @@
  minion%0A
-     *%0A
      * @
@@ -3331,17 +3331,16 @@
 ult());%0A
-%0A
         
@@ -3619,17 +3619,16 @@
 nions);%0A
-%0A
         

Remove some empty lines