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.Map;
24  import java.util.Map.Entry;
25  
26  /**
27   * Method parameters.
28   */
29  public class ThemisAnalysisParameters {
30      /**
31       * The map of parameters.
32       */
33      private final Map<String, ThemisAnalysisReference> theParameters;
34  
35      /**
36       * The thrown exception.
37       */
38      private final ThemisAnalysisReference theThrown;
39  
40      /**
41       * Constructor.
42       *
43       * @param pParser  the parser
44       * @param pHeaders the headers
45       * @throws OceanusException on error
46       */
47      ThemisAnalysisParameters(final ThemisAnalysisParser pParser,
48                               final Deque<ThemisAnalysisElement> pHeaders) throws OceanusException {
49          /* Convert headers to single line */
50          final ThemisAnalysisLine myHeader = new ThemisAnalysisLine(pHeaders);
51  
52          /* Find the end of the generic sequence */
53          final int myEnd = myHeader.findEndOfNestedSequence(0, 0, ThemisAnalysisChar.PARENTHESIS_CLOSE, ThemisAnalysisChar.PARENTHESIS_OPEN);
54          if (myEnd < 0) {
55              throw new ThemisDataException("End character not found");
56          }
57          final ThemisAnalysisLine myParms = myHeader.stripUpToPosition(myEnd);
58          myParms.stripStartChar(ThemisAnalysisChar.PARENTHESIS_OPEN);
59          myParms.stripEndChar(ThemisAnalysisChar.PARENTHESIS_CLOSE);
60  
61          /* Parse the parameters */
62          theParameters = pParser.parseParameters(myParms);
63  
64          /* Handle throws clause */
65          final String myToken = myHeader.peekNextToken();
66          if (myToken.equals(ThemisAnalysisKeyWord.THROWS.getKeyWord())) {
67              myHeader.stripNextToken();
68              theThrown = ThemisAnalysisParser.parseDataType(pParser.getDataMap(), myHeader);
69          } else {
70              theThrown = null;
71          }
72      }
73  
74      @Override
75      public String toString() {
76          /* Start parameters */
77          final StringBuilder myBuilder = new StringBuilder();
78          myBuilder.append(ThemisAnalysisChar.PARENTHESIS_OPEN);
79  
80          /* Build parameters */
81          boolean bFirst = true;
82          for (Entry<String, ThemisAnalysisReference> myEntry : theParameters.entrySet()) {
83              /* Handle separators */
84              if (!bFirst) {
85                  myBuilder.append(ThemisAnalysisChar.COMMA);
86                  myBuilder.append(ThemisAnalysisChar.BLANK);
87              } else {
88                  bFirst = false;
89              }
90  
91              /* Add parameter */
92              myBuilder.append(myEntry.getValue());
93              myBuilder.append(ThemisAnalysisChar.BLANK);
94              myBuilder.append(myEntry.getKey());
95          }
96  
97          /* End parameters */
98          myBuilder.append(ThemisAnalysisChar.PARENTHESIS_CLOSE);
99  
100         /* If we have a thrown exception */
101         if (theThrown != null) {
102             /* Format it */
103             myBuilder.append(ThemisAnalysisChar.BLANK);
104             myBuilder.append(ThemisAnalysisKeyWord.THROWS.getKeyWord());
105             myBuilder.append(ThemisAnalysisChar.BLANK);
106             myBuilder.append(theThrown.toString());
107         }
108 
109         /* return the string */
110         return myBuilder.toString();
111     }
112 }