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.solver.proj;
18  
19  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
22  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance;
23  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisClassInstance;
24  import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisMethodInstance;
25  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverDef.ThemisSolverClassDef;
26  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverDef.ThemisSolverFileDef;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * Solver Class.
33   */
34  public class ThemisSolverClass
35          implements ThemisSolverClassDef {
36      /**
37       * Report fields.
38       */
39      private static final MetisFieldSet<ThemisSolverClass> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisSolverClass.class);
40  
41      /*
42       * Declare Fields.
43       */
44      static {
45          FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_METHODS, ThemisSolverClass::getMethods);
46          FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_UNDERLYING, ThemisSolverClass::getUnderlyingClass);
47      }
48  
49      /**
50       * The fully qualified name of the class.
51       */
52      private final String theFullName;
53  
54      /**
55       * The owning file.
56       */
57      private final ThemisSolverFileDef theFile;
58  
59      /**
60       * The contained class.
61       */
62      private final ThemisClassInstance theClass;
63  
64      /**
65       * The methods.
66       */
67      private final List<ThemisSolverMethod> theMethods;
68  
69      /**
70       * The ancestors.
71       */
72      private final List<String> theAncestors;
73  
74      /**
75       * Is this a standard class?
76       */
77      private final boolean isStandard;
78  
79      /**
80       * Constructor.
81       *
82       * @param pFile  the owning file
83       * @param pClass the parsed class
84       */
85      ThemisSolverClass(final ThemisSolverFileDef pFile,
86                        final ThemisClassInstance pClass) {
87          /* Store the parameters */
88          theFile = pFile;
89          theClass = pClass;
90          theAncestors = new ArrayList<>();
91  
92          /* Access the full name */
93          theFullName = theClass.getFullName();
94          isStandard = pFile.getOwningPackage().isStandard();
95  
96          /* Populate the methodList */
97          theMethods = new ArrayList<>();
98          final ThemisInstance myNode = (ThemisInstance) theClass;
99          for (ThemisInstance myMethod : myNode.discoverChildren(ThemisMethodInstance.class::isInstance)) {
100             final ThemisSolverMethod mySolverMethod = new ThemisSolverMethod(this, (ThemisMethodInstance) myMethod);
101             theMethods.add(mySolverMethod);
102         }
103     }
104 
105     @Override
106     public MetisFieldSet<ThemisSolverClass> getDataFieldSet() {
107         return FIELD_DEFS;
108     }
109 
110     @Override
111     public String formatObject(final OceanusDataFormatter pFormatter) {
112         return toString();
113     }
114 
115     /**
116      * Obtain the name of the class.
117      *
118      * @return the name
119      */
120     public String getName() {
121         return theClass.getName();
122     }
123 
124     @Override
125     public String getFullName() {
126         return theFullName;
127     }
128 
129     @Override
130     public ThemisSolverFileDef getOwningFile() {
131         return theFile;
132     }
133 
134     @Override
135     public ThemisClassInstance getUnderlyingClass() {
136         return theClass;
137     }
138 
139     /**
140      * Is this a standard class?
141      *
142      * @return true/false
143      */
144     public boolean isStandard() {
145         return isStandard;
146     }
147 
148     /**
149      * Is this a top-level class?
150      *
151      * @return true/false
152      */
153     public boolean isTopLevel() {
154         return theClass.isTopLevel();
155     }
156 
157     /**
158      * Obtain the method list.
159      *
160      * @return the method list
161      */
162     public List<ThemisSolverMethod> getMethods() {
163         return theMethods;
164     }
165 
166     /**
167      * Obtain the package.
168      *
169      * @return the packageName
170      */
171     public String getPackageName() {
172         return theFile.getOwningPackage().toString();
173     }
174 
175     @Override
176     public boolean equals(final Object pThat) {
177         /* Handle the trivial cases */
178         if (this == pThat) {
179             return true;
180         }
181         if (pThat == null) {
182             return false;
183         }
184 
185         /* Make sure that the object is a Class */
186         if (!(pThat instanceof ThemisSolverClass myThat)) {
187             return false;
188         }
189 
190         /* Check full name */
191         return theFullName.equals(myThat.getFullName());
192     }
193 
194     @Override
195     public int hashCode() {
196         return theFullName.hashCode();
197     }
198 
199     @Override
200     public String toString() {
201         return theFullName;
202     }
203 
204     /**
205      * Obtain the list of ancestors.
206      *
207      * @return the list
208      */
209     public List<String> getAncestors() {
210         return theAncestors;
211     }
212 
213     /**
214      * Add ancestor.
215      *
216      * @param pAncestor the ancestor
217      */
218     public void addAncestor(final String pAncestor) {
219         theAncestors.add(pAncestor);
220     }
221 }