View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.coverage.lines;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   import edu.umd.cs.findbugs.annotations.Nullable;
9   
10  import java.io.Serializable;
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  import mockit.coverage.CallPoint;
15  import mockit.coverage.Configuration;
16  
17  import org.checkerframework.checker.index.qual.NonNegative;
18  
19  public class LineSegmentData implements Serializable {
20      private static final long serialVersionUID = -6233980722802474992L;
21      private static final int MAX_CALL_POINTS = Integer.parseInt(Configuration.getProperty("maxCallPoints", "10"));
22  
23      // Constant data:
24      private boolean unreachable;
25      protected boolean empty;
26  
27      // Runtime data:
28      @NonNegative
29      int executionCount;
30      @Nullable
31      private List<CallPoint> callPoints;
32  
33      public final void markAsUnreachable() {
34          unreachable = true;
35      }
36  
37      final void markAsReachable() {
38          unreachable = false;
39      }
40  
41      public boolean isEmpty() {
42          return empty;
43      }
44  
45      final void markAsEmpty() {
46          empty = true;
47      }
48  
49      final boolean acceptsAdditionalCallPoints() {
50          return callPoints == null || callPoints.size() < MAX_CALL_POINTS;
51      }
52  
53      @NonNegative
54      final int registerExecution(@Nullable CallPoint callPoint) {
55          int previousExecutionCount = executionCount++;
56  
57          if (callPoint != null) {
58              addCallPoint(callPoint);
59          }
60  
61          return previousExecutionCount;
62      }
63  
64      private void addCallPoint(@NonNull CallPoint callPoint) {
65          if (callPoints == null) {
66              callPoints = new ArrayList<>(MAX_CALL_POINTS);
67          }
68  
69          for (int i = callPoints.size() - 1; i >= 0; i--) {
70              CallPoint previousCallPoint = callPoints.get(i);
71  
72              if (callPoint.isSameLineInTestCode(previousCallPoint)) {
73                  previousCallPoint.incrementRepetitionCount();
74                  return;
75              }
76          }
77  
78          callPoints.add(callPoint);
79      }
80  
81      public final boolean containsCallPoints() {
82          return callPoints != null;
83      }
84  
85      @Nullable
86      public final List<CallPoint> getCallPoints() {
87          return callPoints;
88      }
89  
90      @NonNegative
91      public final int getExecutionCount() {
92          return executionCount;
93      }
94  
95      final void setExecutionCount(@NonNegative int executionCount) {
96          this.executionCount = executionCount;
97      }
98  
99      public final boolean isCovered() {
100         return unreachable || !empty && executionCount > 0;
101     }
102 
103     final void addExecutionCountAndCallPointsFromPreviousTestRun(@NonNull LineSegmentData previousData) {
104         executionCount += previousData.executionCount;
105 
106         if (previousData.callPoints != null) {
107             if (callPoints != null) {
108                 callPoints.addAll(0, previousData.callPoints);
109             } else {
110                 callPoints = previousData.callPoints;
111             }
112         }
113     }
114 }