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