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.lethe.analysis.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
21  
22  import java.util.ArrayDeque;
23  import java.util.Deque;
24  
25  /**
26   * Block construct.
27   */
28  public class ThemisAnalysisBlock
29          implements ThemisAnalysisContainer, ThemisAnalysisAdoptable {
30      /**
31       * The Header.
32       */
33      private final ThemisAnalysisLine theHeader;
34  
35      /**
36       * The properties.
37       */
38      private final ThemisAnalysisProperties theProperties;
39  
40      /**
41       * The contents.
42       */
43      private final Deque<ThemisAnalysisElement> theContents;
44  
45      /**
46       * The number of lines.
47       */
48      private final int theNumLines;
49  
50      /**
51       * The parent.
52       */
53      private ThemisAnalysisContainer theParent;
54  
55      /**
56       * The dataMap.
57       */
58      private final ThemisAnalysisDataMap theDataMap;
59  
60      /**
61       * Constructor.
62       *
63       * @param pParser the parser
64       * @param pLine   the initial class line
65       * @throws OceanusException on error
66       */
67      ThemisAnalysisBlock(final ThemisAnalysisParser pParser,
68                          final ThemisAnalysisLine pLine) throws OceanusException {
69          /* Store parameters */
70          theHeader = pLine;
71          theNumLines = 2;
72  
73          /* Store parameters */
74          theProperties = pLine.getProperties();
75          theParent = pParser.getParent();
76          theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
77  
78          /* Create the arrays */
79          final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
80  
81          /* Create a parser */
82          theContents = new ArrayDeque<>();
83          final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
84          myParser.processLines();
85      }
86  
87      @Override
88      public Deque<ThemisAnalysisElement> getContents() {
89          return theContents;
90      }
91  
92      @Override
93      public ThemisAnalysisContainer getParent() {
94          return theParent;
95      }
96  
97      @Override
98      public void setParent(final ThemisAnalysisContainer pParent) {
99          theParent = pParent;
100         theDataMap.setParent(pParent.getDataMap());
101     }
102 
103     @Override
104     public ThemisAnalysisDataMap getDataMap() {
105         return theDataMap;
106     }
107 
108     @Override
109     public int getNumLines() {
110         return theNumLines;
111     }
112 
113     /**
114      * Is the block synchronized.
115      *
116      * @return true/false
117      */
118     public boolean isSynchronized() {
119         return theProperties.hasModifier(ThemisAnalysisModifier.SYNCHRONIZED);
120     }
121 
122     @Override
123     public String toString() {
124         /* Create builder */
125         final StringBuilder myBuilder = new StringBuilder();
126 
127         /* Handle flags */
128         if (theHeader.getProperties().hasModifier(ThemisAnalysisModifier.STATIC)) {
129             myBuilder.append(ThemisAnalysisModifier.STATIC);
130             myBuilder.append(ThemisAnalysisChar.BLANK);
131         }
132         if (theHeader.getProperties().hasModifier(ThemisAnalysisModifier.SYNCHRONIZED)) {
133             myBuilder.append(ThemisAnalysisModifier.SYNCHRONIZED);
134             myBuilder.append(ThemisAnalysisChar.BLANK);
135         }
136 
137         /* Add header */
138         myBuilder.append(theHeader);
139         return myBuilder.toString();
140     }
141 
142     /**
143      * Check for block sequence.
144      *
145      * @param pLine the line to check
146      * @return true/false
147      */
148     static boolean checkBlock(final ThemisAnalysisLine pLine) {
149         /* Only interested in possible blocks */
150         if (!pLine.endsWithChar(ThemisAnalysisChar.BRACE_OPEN)) {
151             return false;
152         }
153 
154         /* Check for synchronised block */
155         if (pLine.getProperties().hasModifier(ThemisAnalysisModifier.SYNCHRONIZED)
156                 && pLine.startsWithChar(ThemisAnalysisChar.PARENTHESIS_OPEN)) {
157             return true;
158         }
159 
160         /* Check for standard block */
161         return pLine.getLength() == 1;
162     }
163 }