001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright (c) 2005-2018 The ConQAT Project                               |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016|                                                                          |
017+-------------------------------------------------------------------------*/
018package org.conqat.engine.sourcecode.coverage;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024
025import org.conqat.lib.commons.collections.CollectionUtils;
026import org.conqat.lib.commons.collections.UnmodifiableList;
027import org.conqat.lib.commons.js_export.ExportAsType;
028import org.conqat.lib.commons.js_export.ExportToJavaScript;
029
030import com.fasterxml.jackson.annotation.JsonCreator;
031import com.fasterxml.jackson.annotation.JsonProperty;
032
033/**
034 * Holds coverage information for a file represented as "probes". See
035 * {@link CoverageProbeBase} for a description of probes.
036 * 
037 * We use this object both to represent covered and coverable information. The
038 * flag {@link #uploaded} differs between the two cases.
039 */
040@ExportToJavaScript
041public class ProbeCoverageInfo implements Serializable {
042
043        private static final long serialVersionUID = 1L;
044
045        /** The name of the JSON property name for {@link #probes}. */
046        private static final String PROBES_PROPERTY = "probes";
047
048        /** The name of the JSON property name for {@link #uploaded}. */
049        private static final String UPLOADED_PROPERTY = "uploaded";
050
051        @JsonProperty(PROBES_PROPERTY)
052        @ExportAsType("!Array<!ts.data.DecisionProbe|!ts.data.SimpleStatementCoverageProbe>")
053        private final ArrayList<CoverageProbeBase> probes;
054
055        /**
056         * Signals whether this is coverage info uploaded externally. See also class
057         * comment
058         */
059        @JsonProperty(UPLOADED_PROPERTY)
060        private final boolean uploaded;
061
062        public ProbeCoverageInfo(boolean uploaded) {
063                this.probes = new ArrayList<>();
064                this.uploaded = uploaded;
065        }
066
067        @JsonCreator
068        public ProbeCoverageInfo(@JsonProperty(UPLOADED_PROPERTY) boolean uploaded,
069                        @JsonProperty(PROBES_PROPERTY) Collection<CoverageProbeBase> probes) {
070                this(uploaded);
071                this.probes.addAll(probes);
072        }
073
074        /** Constructor. */
075        public ProbeCoverageInfo(ProbeCoverageInfo coverageProbeInfo) {
076                this.probes = new ArrayList<>(coverageProbeInfo.probes);
077                this.uploaded = coverageProbeInfo.uploaded;
078        }
079
080        /** Adds the given probe */
081        public void addProbe(CoverageProbeBase probe) {
082                probes.add(probe);
083        }
084
085        public UnmodifiableList<CoverageProbeBase> getProbes() {
086                return CollectionUtils.asUnmodifiable(probes);
087        }
088
089        /** Overwrites the probes of this coverage info. */
090        public void setProbes(List<CoverageProbeBase> probes) {
091                this.probes.clear();
092                this.probes.addAll(probes);
093
094        }
095
096        /** @see #uploaded */
097        public boolean isUploaded() {
098                return uploaded;
099        }
100
101        /** Returns the number of coverable entities of this probe coverage */
102        public int getCoverableCount() {
103                int coverableCount = 0;
104                for (CoverageProbeBase probe : probes) {
105                        coverableCount += probe.getCoverableCount();
106                }
107                return coverableCount;
108        }
109
110        /** Returns the number of covered entities of this probe coverage */
111        public int getCoveredCount() {
112                int coveredCount = 0;
113                for (CoverageProbeBase probe : probes) {
114                        coveredCount += probe.getCoveredCount();
115                }
116                return coveredCount;
117        }
118
119}