001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2009-2017 CQSE GmbH                                        |
004|                                                                          |
005+-------------------------------------------------------------------------*/
006package org.conqat.engine.index.shared;
007
008import java.io.Serializable;
009import java.util.Collection;
010import java.util.HashMap;
011import java.util.HashSet;
012import java.util.Map;
013import java.util.Map.Entry;
014
015import org.conqat.lib.commons.collections.CollectionUtils;
016import org.conqat.lib.commons.collections.UnmodifiableMap;
017import org.conqat.lib.commons.collections.UnmodifiableSet;
018import org.conqat.lib.commons.uniformpath.UniformPath;
019
020import com.fasterxml.jackson.annotation.JsonCreator;
021import com.fasterxml.jackson.annotation.JsonProperty;
022
023/** Data class for a pre-commit upload. */
024public class PreCommitUploadData implements Serializable {
025
026        /** Default serial version UID */
027        private static final long serialVersionUID = 1L;
028
029        /**
030         * A mapping from uniform paths to the content of the respective file. This
031         * should contain all the changes that need to be analyzed.
032         */
033        @JsonProperty("uniformPathToContentMap")
034        private final HashMap<String, String> uniformPathToContentMap = new HashMap<>();
035
036        /** Set of uniform paths that have been deleted. */
037        @JsonProperty("deletedUniformPaths")
038        private final HashSet<String> deletedUniformPaths = new HashSet<>();
039
040        @JsonCreator
041        public PreCommitUploadData() {
042                // Used for JSON deserialization
043        }
044
045        /**
046         * Constructor.
047         */
048        public PreCommitUploadData(Map<UniformPath, String> uniformPathToContentMap,
049                        Collection<UniformPath> deletedUniformPaths) {
050                for (Entry<UniformPath, String> entry : uniformPathToContentMap.entrySet()) {
051                        UniformPath uniformPath = entry.getKey();
052                        String content = entry.getValue();
053                        this.uniformPathToContentMap.put(uniformPath.toString(), content);
054                }
055                for (UniformPath uniformPath : deletedUniformPaths) {
056                        this.deletedUniformPaths.add(uniformPath.toString());
057                }
058        }
059
060        /** @see #uniformPathToContentMap */
061        public UnmodifiableMap<String, String> getUniformPathToContentMap() {
062                return CollectionUtils.asUnmodifiable(uniformPathToContentMap);
063        }
064
065        /** @see #deletedUniformPaths */
066        public UnmodifiableSet<String> getDeletedUniformPaths() {
067                return CollectionUtils.asUnmodifiable(deletedUniformPaths);
068        }
069
070        /** Validates if the content has been properly set. */
071        public boolean isValid() {
072                return deletedUniformPaths != null && uniformPathToContentMap != null;
073        }
074
075        /** Returns true if no files are deleted and no files are new or changed. */
076        public boolean isEmpty() {
077                return deletedUniformPaths.isEmpty() && uniformPathToContentMap.isEmpty();
078        }
079
080}