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