1 package net.sourceforge.pmd.lang.dfa.pathfinder;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import net.sourceforge.pmd.lang.dfa.DataFlowNode;
8 import net.sourceforge.pmd.lang.dfa.NodeType;
9
10 public class CurrentPath {
11
12 private final List<DataFlowNode> list;
13
14 public CurrentPath() {
15 list = new ArrayList<DataFlowNode>();
16 }
17
18 public int getLength() {
19 return list.size();
20 }
21
22 public Iterator<DataFlowNode> iterator() {
23 return list.iterator();
24 }
25
26 public DataFlowNode getLast() {
27 return list.get(list.size() - 1);
28 }
29
30 public void removeLast() {
31 list.remove(list.size() - 1);
32 }
33
34 public boolean isEmpty() {
35 return list.isEmpty();
36 }
37
38 public void addLast(DataFlowNode n) {
39 list.add(n);
40
41 }
42
43 public boolean isDoBranchNode() {
44 return this.getLast().isType(NodeType.DO_EXPR);
45 }
46
47 public boolean isFirstDoStatement() {
48 return isFirstDoStatement(this.getLast());
49 }
50
51 public DataFlowNode getDoBranchNodeFromFirstDoStatement() {
52
53 if (!isFirstDoStatement()) {
54 return null;
55 }
56 DataFlowNode inode = getLast();
57 for (DataFlowNode parent: inode.getParents()) {
58 if (parent.isType(NodeType.DO_EXPR)) {
59 return parent;
60 }
61 }
62 return null;
63 }
64
65 public boolean isEndNode() {
66 return this.getLast().getChildren().size() == 0;
67
68 }
69
70 public boolean isBranch() {
71 return this.getLast().getChildren().size() > 1;
72 }
73
74 private boolean isFirstDoStatement(DataFlowNode inode) {
75 int index = inode.getIndex() - 1;
76 if (index < 0) {
77 return false;
78 }
79 return inode.getFlow().get(index).isType(NodeType.DO_BEFORE_FIRST_STATEMENT);
80 }
81 }
82