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.ThemisAnalysisGeneric.ThemisAnalysisGenericBase;
21  import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisGeneric.ThemisAnalysisGenericVarList;
22  
23  import java.util.EnumMap;
24  import java.util.Map;
25  
26  /**
27   * Properties for an element.
28   */
29  public final class ThemisAnalysisProperties {
30      /**
31       * Null properties.
32       */
33      static final ThemisAnalysisProperties NULL = new ThemisAnalysisProperties(true);
34  
35      /**
36       * Is this the null entry?
37       */
38      private final boolean isNull;
39  
40      /**
41       * The map of Modifiers.
42       */
43      private final Map<ThemisAnalysisModifier, Boolean> theModifiers;
44  
45      /**
46       * The generic variable list.
47       */
48      private ThemisAnalysisGeneric theGenericVars;
49  
50      /**
51       * Constructor.
52       */
53      private ThemisAnalysisProperties() {
54          this(false);
55      }
56  
57      /**
58       * Constructor.
59       *
60       * @param pNull is this the null element?
61       */
62      private ThemisAnalysisProperties(final boolean pNull) {
63          isNull = pNull;
64          theModifiers = isNull
65                  ? null
66                  : new EnumMap<>(ThemisAnalysisModifier.class);
67      }
68  
69      /**
70       * Is the modifier present?
71       *
72       * @param pModifier the modifier
73       * @return true/false
74       */
75      boolean hasModifier(final ThemisAnalysisModifier pModifier) {
76          return !isNull && theModifiers.containsKey(pModifier);
77      }
78  
79      /**
80       * Are generic variables present?
81       *
82       * @return true/false
83       */
84      boolean hasGeneric() {
85          return theGenericVars != null;
86      }
87  
88      /**
89       * Set modifier.
90       *
91       * @param pModifier the modifier
92       * @return the updated properties
93       */
94      ThemisAnalysisProperties setModifier(final ThemisAnalysisModifier pModifier) {
95          final ThemisAnalysisProperties myProps = isNull
96                  ? new ThemisAnalysisProperties()
97                  : this;
98          myProps.theModifiers.put(pModifier, Boolean.TRUE);
99          return myProps;
100     }
101 
102     /**
103      * Set the generic variable list.
104      *
105      * @param pGeneric the generic variables
106      * @return the updated properties
107      */
108     ThemisAnalysisProperties setGenericVariables(final ThemisAnalysisGeneric pGeneric) {
109         final ThemisAnalysisProperties myProps = isNull
110                 ? new ThemisAnalysisProperties()
111                 : this;
112         myProps.theGenericVars = pGeneric;
113         return myProps;
114     }
115 
116     /**
117      * Resolve the generic variables.
118      *
119      * @param pParser the parser
120      * @throws OceanusException on error
121      */
122     void resolveGeneric(final ThemisAnalysisParser pParser) throws OceanusException {
123         /* Resolve any generic base instance */
124         if (theGenericVars instanceof ThemisAnalysisGenericBase myBase) {
125             /* Resolve the variables */
126             final ThemisAnalysisGeneric myVars = new ThemisAnalysisGenericVarList(pParser, myBase);
127 
128             /* Only record the parsed variables if the parser is nonTemporary */
129             if (!pParser.isTemporary()) {
130                 theGenericVars = myVars;
131             }
132         }
133     }
134 }