1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
21 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisIf.ThemisIteratorChain;
22 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisStatement.ThemisAnalysisStatementHolder;
23
24 import java.util.ArrayDeque;
25 import java.util.Collections;
26 import java.util.Deque;
27 import java.util.Iterator;
28
29
30
31
32 public class ThemisAnalysisElse
33 implements ThemisAnalysisContainer, ThemisAnalysisAdoptable, ThemisAnalysisStatementHolder {
34
35
36
37 private ThemisAnalysisContainer theParent;
38
39
40
41
42 private final ThemisAnalysisStatement theCondition;
43
44
45
46
47 private final Deque<ThemisAnalysisElement> theContents;
48
49
50
51
52 private final ThemisAnalysisElse theElse;
53
54
55
56
57 private final int theNumLines;
58
59
60
61
62 private final ThemisAnalysisDataMap theDataMap;
63
64
65
66
67
68
69
70
71
72 ThemisAnalysisElse(final ThemisAnalysisParser pParser,
73 final ThemisAnalysisContainer pOwner,
74 final ThemisAnalysisLine pLine) throws OceanusException {
75
76 theParent = pOwner;
77 theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
78
79
80 pLine.stripStartSequence(ThemisAnalysisKeyWord.IF.getKeyWord());
81
82
83 final Deque<ThemisAnalysisElement> myHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, pLine);
84 theNumLines = myHeaders.size() + 1;
85 theCondition = new ThemisAnalysisStatement(myHeaders);
86
87
88 final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
89
90
91 theElse = (ThemisAnalysisElse) pParser.processExtra(pOwner, ThemisAnalysisKeyWord.ELSE);
92
93
94 theContents = new ArrayDeque<>();
95 final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
96 myParser.processLines();
97 }
98
99 @Override
100 public Deque<ThemisAnalysisElement> getContents() {
101 return theContents;
102 }
103
104 @Override
105 public Iterator<ThemisAnalysisStatement> statementIterator() {
106 final Iterator<ThemisAnalysisStatement> myLocal = theCondition.nullParameters()
107 ? Collections.emptyIterator()
108 : Collections.singleton(theCondition).iterator();
109 return theElse == null ? myLocal : new ThemisIteratorChain<>(myLocal, theElse.statementIterator());
110 }
111
112 @Override
113 public Iterator<ThemisAnalysisContainer> containerIterator() {
114 return theElse == null
115 ? Collections.emptyIterator()
116 : Collections.singleton((ThemisAnalysisContainer) theElse).iterator();
117 }
118
119 @Override
120 public ThemisAnalysisContainer getParent() {
121 return theParent;
122 }
123
124 @Override
125 public void setParent(final ThemisAnalysisContainer pParent) {
126 theParent = pParent;
127 theDataMap.setParent(pParent.getDataMap());
128 if (theElse != null) {
129 theElse.setParent(pParent);
130 }
131 }
132
133 @Override
134 public ThemisAnalysisDataMap getDataMap() {
135 return theDataMap;
136 }
137
138 @Override
139 public void postProcessExtras() throws OceanusException {
140
141 if (theElse != null) {
142 theElse.postProcessLines();
143 }
144 }
145
146
147
148
149
150
151 public ThemisAnalysisElse getElse() {
152 return theElse;
153 }
154
155 @Override
156 public int getNumLines() {
157 return theNumLines;
158 }
159
160 @Override
161 public String toString() {
162 return theCondition.toString();
163 }
164 }