View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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  
22  import java.util.Deque;
23  import java.util.Iterator;
24  
25  /**
26   * Statements.
27   */
28  public class ThemisAnalysisStatement
29          implements ThemisAnalysisProcessed {
30      /**
31       * StatementHolder interface.
32       */
33      public interface ThemisAnalysisStatementHolder {
34          /**
35           * Obtain statement iterator.
36           *
37           * @return the iterator
38           */
39          Iterator<ThemisAnalysisStatement> statementIterator();
40      }
41  
42      /**
43       * The control.
44       */
45      private final ThemisAnalysisKeyWord theControl;
46  
47      /**
48       * The lines.
49       */
50      private final ThemisAnalysisStack theLines;
51  
52      /**
53       * Constructor.
54       *
55       * @param pParser the parser
56       * @param pLine   the line
57       * @throws OceanusException on error
58       */
59      ThemisAnalysisStatement(final ThemisAnalysisParser pParser,
60                              final ThemisAnalysisLine pLine) throws OceanusException {
61          this(pParser, null, pLine);
62      }
63  
64      /**
65       * Constructor.
66       *
67       * @param pParser  the parser
68       * @param pControl the control
69       * @param pLine    the line
70       * @throws OceanusException on error
71       */
72      ThemisAnalysisStatement(final ThemisAnalysisParser pParser,
73                              final ThemisAnalysisKeyWord pControl,
74                              final ThemisAnalysisLine pLine) throws OceanusException {
75          this(pControl, ThemisAnalysisBuilder.parseTrailers(pParser, pLine));
76          checkSeparator();
77      }
78  
79      /**
80       * Constructor.
81       *
82       * @param pEmbedded the embedded block
83       * @throws OceanusException on error
84       */
85      ThemisAnalysisStatement(final ThemisAnalysisEmbedded pEmbedded) throws OceanusException {
86          this(null, pEmbedded);
87      }
88  
89      /**
90       * Constructor.
91       *
92       * @param pControl  the control keyword
93       * @param pEmbedded the embedded block
94       * @throws OceanusException on error
95       */
96      ThemisAnalysisStatement(final ThemisAnalysisKeyWord pControl,
97                              final ThemisAnalysisEmbedded pEmbedded) throws OceanusException {
98          this(pControl, new ThemisAnalysisStack(pEmbedded));
99          pEmbedded.postProcessLines();
100     }
101 
102     /**
103      * Constructor.
104      *
105      * @param pParams the parameters
106      */
107     ThemisAnalysisStatement(final Deque<ThemisAnalysisElement> pParams) {
108         this(null, pParams);
109     }
110 
111     /**
112      * Constructor.
113      *
114      * @param pStack the stack
115      */
116     ThemisAnalysisStatement(final ThemisAnalysisStack pStack) {
117         this(null, pStack);
118     }
119 
120     /**
121      * Constructor.
122      *
123      * @param pControl the control
124      * @param pParams  the parameters
125      */
126     ThemisAnalysisStatement(final ThemisAnalysisKeyWord pControl,
127                             final Deque<ThemisAnalysisElement> pParams) {
128         theControl = pControl;
129         theLines = new ThemisAnalysisStack(pParams);
130     }
131 
132     /**
133      * Constructor.
134      *
135      * @param pControl the control keyword
136      * @param pStack   the stack
137      */
138     ThemisAnalysisStatement(final ThemisAnalysisKeyWord pControl,
139                             final ThemisAnalysisStack pStack) {
140         theControl = pControl;
141         theLines = pStack;
142     }
143 
144     /**
145      * Test for separator.
146      *
147      * @throws OceanusException on error
148      */
149     void checkSeparator() throws OceanusException {
150         final ThemisAnalysisScanner myScanner = new ThemisAnalysisScanner(theLines);
151         final boolean isSep = myScanner.checkForSeparator(ThemisAnalysisChar.COMMA);
152         if (isSep) {
153             throw new ThemisDataException("Multi-statement");
154         }
155     }
156 
157     @Override
158     public int getNumLines() {
159         return theLines.size();
160     }
161 
162     /**
163      * Are there null parameters?
164      *
165      * @return true/false
166      */
167     public boolean nullParameters() {
168         return theLines.isEmpty();
169     }
170 
171     /**
172      * Obtain the embedded statement (if any).
173      *
174      * @return the embedded statement (or null)
175      */
176     public ThemisAnalysisElement getEmbedded() {
177         final ThemisAnalysisElement myLast = theLines.peekLastLine();
178         return myLast instanceof ThemisAnalysisEmbedded myEmbedded
179                 ? myEmbedded.getContents().peekFirst()
180                 : null;
181     }
182 
183     @Override
184     public String toString() {
185         final String myParms = theLines.toString();
186         return theControl == null ? myParms : theControl + " " + myParms;
187     }
188 }