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  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * Annotation.
26   */
27  public class ThemisAnalysisAnnotation
28          implements ThemisAnalysisProcessed {
29      /**
30       * The annotationLines.
31       */
32      private final List<ThemisAnalysisAnnotationRef> theAnnotations;
33  
34      /**
35       * Constructor.
36       *
37       * @param pParser the parser
38       * @param pLine   the initial annotation line
39       * @throws OceanusException on error
40       */
41      ThemisAnalysisAnnotation(final ThemisAnalysisParser pParser,
42                               final ThemisAnalysisLine pLine) throws OceanusException {
43          /* Create the list of annotation lines */
44          theAnnotations = new ArrayList<>();
45          theAnnotations.add(new ThemisAnalysisAnnotationRef(pParser, pLine));
46  
47          /* While there are further lines */
48          while (pParser.hasLines()) {
49              /* Access next line */
50              final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.peekNextLine();
51  
52              /* It it is also an annotation */
53              if (isAnnotation(myLine)) {
54                  /* Add the annotation line and remove from input */
55                  theAnnotations.add(new ThemisAnalysisAnnotationRef(pParser, myLine));
56                  pParser.popNextLine();
57  
58                  /* else break loop */
59              } else {
60                  break;
61              }
62          }
63      }
64  
65      @Override
66      public int getNumLines() {
67          return theAnnotations.size();
68      }
69  
70      /**
71       * Is the line and annotation?
72       *
73       * @param pLine the line
74       * @return true/false
75       */
76      static boolean isAnnotation(final ThemisAnalysisLine pLine) {
77          return pLine.startsWithChar(ThemisAnalysisChar.ANNOTATION)
78                  && !pLine.peekNextToken().equals(ThemisAnalysisKeyWord.ANNOTATION.getKeyWord());
79      }
80  
81      @Override
82      public String toString() {
83          /* Start parameters */
84          final StringBuilder myBuilder = new StringBuilder();
85  
86          /* Build parameters */
87          boolean bFirst = true;
88          for (ThemisAnalysisAnnotationRef myRef : theAnnotations) {
89              /* Handle separators */
90              if (!bFirst) {
91                  myBuilder.append(ThemisAnalysisChar.LF);
92              } else {
93                  bFirst = false;
94              }
95  
96              /* Add parameter */
97              myBuilder.append(myRef);
98          }
99  
100         return myBuilder.toString();
101     }
102 
103     /**
104      * AnnotationReference.
105      */
106     static class ThemisAnalysisAnnotationRef {
107         /**
108          * The annotationClass.
109          */
110         private final ThemisAnalysisReference theAnnotation;
111 
112         /**
113          * Constructor.
114          *
115          * @param pParser the parser
116          * @param pLine   the initial annotation line
117          * @throws OceanusException on error
118          */
119         ThemisAnalysisAnnotationRef(final ThemisAnalysisParser pParser,
120                                     final ThemisAnalysisLine pLine) throws OceanusException {
121             pLine.stripStartChar(ThemisAnalysisChar.ANNOTATION);
122             theAnnotation = ThemisAnalysisParser.parseDataType(pParser.getDataMap(), pLine);
123         }
124 
125         @Override
126         public String toString() {
127             return theAnnotation.toString();
128         }
129     }
130 }