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.exc.ThemisDataException;
21 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
22
23 import java.util.ArrayDeque;
24 import java.util.Deque;
25 import java.util.Objects;
26
27
28
29
30 public class ThemisAnalysisEmbedded
31 implements ThemisAnalysisContainer, ThemisAnalysisAdoptable {
32
33
34
35 private ThemisAnalysisContainer theParent;
36
37
38
39
40 private final ThemisAnalysisLine theHeader;
41
42
43
44
45 private final Deque<ThemisAnalysisElement> theEmbedded;
46
47
48
49
50 private final ThemisAnalysisLine theTrailer;
51
52
53
54
55 private final int theNumLines;
56
57
58
59
60
61
62
63
64
65 ThemisAnalysisEmbedded(final ThemisAnalysisParser pParser,
66 final ThemisAnalysisEmbedType pType,
67 final ThemisAnalysisLine pLine) throws OceanusException {
68
69 theHeader = pLine;
70
71
72 theEmbedded = new ArrayDeque<>();
73 switch (pType) {
74 case LAMBDA:
75 theEmbedded.add(new ThemisAnalysisLambda(pParser, pLine));
76 break;
77 case ANON:
78 theEmbedded.add(new ThemisAnalysisAnonClass(pParser, pLine));
79 break;
80 case ARRAY:
81 theEmbedded.add(new ThemisAnalysisArrayInit(pParser, pLine));
82 break;
83 case NONE:
84 default:
85 break;
86 }
87
88
89 theParent = pParser.getParent();
90
91
92 theNumLines = 2;
93
94
95 theTrailer = (ThemisAnalysisLine) pParser.popNextLine();
96
97
98 if (!theTrailer.endsWithChar(ThemisAnalysisChar.SEMICOLON)) {
99 throw new ThemisDataException("Invalid embedded item");
100 }
101 }
102
103
104
105
106
107
108 ThemisAnalysisLine getHeader() {
109 return theHeader;
110 }
111
112 @Override
113 public Deque<ThemisAnalysisElement> getContents() {
114 return theEmbedded;
115 }
116
117 @Override
118 public ThemisAnalysisContainer getParent() {
119 return theParent;
120 }
121
122 @Override
123 public void setParent(final ThemisAnalysisContainer pParent) {
124 theParent = pParent;
125 theEmbedded.forEach(e -> ((ThemisAnalysisAdoptable) e).setParent(pParent));
126 }
127
128 @Override
129 public ThemisAnalysisDataMap getDataMap() {
130 return ((ThemisAnalysisContainer) Objects.requireNonNull(theEmbedded.peekFirst())).getDataMap();
131 }
132
133 @Override
134 public int getNumLines() {
135 return theNumLines;
136 }
137
138
139
140
141
142
143
144 static ThemisAnalysisEmbedType checkForEmbedded(final ThemisAnalysisLine pLine) {
145 if (ThemisAnalysisLambda.checkLambda(pLine)) {
146 return ThemisAnalysisEmbedType.LAMBDA;
147 }
148 if (ThemisAnalysisAnonClass.checkAnon(pLine)) {
149 return ThemisAnalysisEmbedType.ANON;
150 }
151 if (ThemisAnalysisArrayInit.checkArrayInit(pLine)) {
152 return ThemisAnalysisEmbedType.ARRAY;
153 }
154 if (ThemisAnalysisMethodBody.checkMethodBody(pLine)) {
155 return ThemisAnalysisEmbedType.METHOD;
156 }
157 return ThemisAnalysisEmbedType.NONE;
158 }
159
160
161
162
163 enum ThemisAnalysisEmbedType {
164
165
166
167 LAMBDA,
168
169
170
171
172 ANON,
173
174
175
176
177 ARRAY,
178
179
180
181
182 METHOD,
183
184
185
186
187 NONE;
188 }
189 }