View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 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  
18  package io.github.tonywasher.joceanus.themis.solver.proj;
19  
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataMap;
21  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
24  import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
25  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverDef.ThemisSolverPackageDef;
26  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverReference.ThemisSolverRefPackage;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * Class holding references to other packages.
35   */
36  public class ThemisSolverReference
37          implements MetisDataMap<ThemisSolverPackageDef, ThemisSolverRefPackage> {
38  
39      /**
40       * Map of references to other packages.
41       */
42      private final Map<ThemisSolverPackageDef, ThemisSolverRefPackage> theMap;
43  
44      /**
45       * Constructor.
46       */
47      public ThemisSolverReference() {
48          theMap = new HashMap<>();
49      }
50  
51      /**
52       * Obtain the references for a package.
53       *
54       * @param pPackage the package
55       * @return the references
56       */
57      public ThemisSolverRefPackage getReferences(final ThemisSolverPackageDef pPackage) {
58          return theMap.get(pPackage);
59      }
60  
61      /**
62       * Obtain the references for a package.
63       *
64       * @param pPackage the package
65       * @return the reference
66       */
67      public ThemisSolverRefPackage getReferredPackage(final ThemisSolverPackageDef pPackage) {
68          return theMap.get(pPackage);
69      }
70  
71      /**
72       * Add references to map.
73       *
74       * @param pReferences the references
75       */
76      public void addReferences(final ThemisSolverRefPackage pReferences) {
77          final ThemisSolverPackageDef myPackage = pReferences.getPackage();
78          theMap.put(myPackage, pReferences);
79      }
80  
81      /**
82       * Does the map have no references of type?
83       *
84       * @param pRefType the refType
85       * @return true/false
86       */
87      public boolean lackingReferences(final ThemisRefType pRefType) {
88          return getReferences(pRefType).isEmpty();
89      }
90  
91      /**
92       * Obtain references of type.
93       *
94       * @param pRefType the refType
95       * @return the references
96       */
97      public List<ThemisSolverRefPackage> getReferences(final ThemisRefType pRefType) {
98          return theMap.values().stream().filter(p -> p.getReferenceType() == pRefType).toList();
99      }
100 
101     @Override
102     public Map<ThemisSolverPackageDef, ThemisSolverRefPackage> getUnderlyingMap() {
103         return theMap;
104     }
105 
106     @Override
107     public String formatObject(final OceanusDataFormatter pFormatter) {
108         return getClass().getSimpleName();
109     }
110 
111     /**
112      * Class representing links from a class to classes in a particular package.
113      */
114     public static class ThemisSolverRefPackage
115             implements MetisFieldItem {
116         /**
117          * Report fields.
118          */
119         private static final MetisFieldSet<ThemisSolverRefPackage> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisSolverRefPackage.class);
120 
121         /*
122          * Declare Fields.
123          */
124         static {
125             FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_REFTYPE, ThemisSolverRefPackage::getReferenceType);
126             FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_REFERENCES, ThemisSolverRefPackage::getReferences);
127         }
128 
129         /**
130          * The package that is referred to.
131          */
132         private final ThemisSolverPackageDef thePackage;
133 
134         /**
135          * The referenceType.
136          */
137         private final ThemisRefType theRefType;
138 
139         /**
140          * The list of classes that refer to the package.
141          */
142         private final List<ThemisSolverRefClass> theReferences;
143 
144         /**
145          * Constructor.
146          *
147          * @param pPackage the package
148          * @param pRefType the reference type
149          */
150         public ThemisSolverRefPackage(final ThemisSolverPackageDef pPackage,
151                                       final ThemisRefType pRefType) {
152             thePackage = pPackage;
153             theRefType = pRefType;
154             theReferences = new ArrayList<>();
155         }
156 
157         @Override
158         public MetisFieldSet<ThemisSolverRefPackage> getDataFieldSet() {
159             return FIELD_DEFS;
160         }
161 
162         @Override
163         public String formatObject(final OceanusDataFormatter pFormatter) {
164             return toString();
165         }
166 
167         /**
168          * Obtain the class.
169          *
170          * @return the class
171          */
172         public ThemisSolverPackageDef getPackage() {
173             return thePackage;
174         }
175 
176         /**
177          * Obtain the referenceType.
178          *
179          * @return the refType
180          */
181         public ThemisRefType getReferenceType() {
182             return theRefType;
183         }
184 
185         /**
186          * Obtain the references.
187          *
188          * @return the references
189          */
190         public List<ThemisSolverRefClass> getReferences() {
191             return theReferences;
192         }
193 
194         /**
195          * Add class that has references.
196          *
197          * @param pReferences the references
198          */
199         public void addReferences(final ThemisSolverRefClass pReferences) {
200             theReferences.add(pReferences);
201         }
202 
203         @Override
204         public String toString() {
205             return thePackage.toString();
206         }
207     }
208 
209     /**
210      * Class representing links from a class to classes in a particular package.
211      */
212     public static class ThemisSolverRefClass
213             implements MetisFieldItem {
214         /**
215          * Report fields.
216          */
217         private static final MetisFieldSet<ThemisSolverRefClass> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisSolverRefClass.class);
218 
219         /*
220          * Declare Fields.
221          */
222         static {
223             FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_CLASS, ThemisSolverRefClass::getSubject);
224             FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_REFERENCES, ThemisSolverRefClass::getReferences);
225         }
226 
227         /**
228          * The class that holds the references.
229          */
230         private final ThemisSolverClass theClass;
231 
232         /**
233          * The references.
234          */
235         private final List<ThemisSolverClass> theReferences;
236 
237         /**
238          * Constructor.
239          *
240          * @param pClass      the class
241          * @param pReferences the references
242          */
243         public ThemisSolverRefClass(final ThemisSolverClass pClass,
244                                     final List<ThemisSolverClass> pReferences) {
245             theClass = pClass;
246             theReferences = pReferences;
247         }
248 
249         @Override
250         public MetisFieldSet<ThemisSolverRefClass> getDataFieldSet() {
251             return FIELD_DEFS;
252         }
253 
254         @Override
255         public String formatObject(final OceanusDataFormatter pFormatter) {
256             return toString();
257         }
258 
259         /**
260          * Obtain the class.
261          *
262          * @return the class
263          */
264         public ThemisSolverClass getSubject() {
265             return theClass;
266         }
267 
268         /**
269          * Obtain the references.
270          *
271          * @return the references
272          */
273         public List<ThemisSolverClass> getReferences() {
274             return theReferences;
275         }
276 
277         @Override
278         public String toString() {
279             return theClass.getFullName();
280         }
281     }
282 
283     /**
284      * Map types.
285      */
286     public enum ThemisRefType {
287         /**
288          * Sibling.
289          */
290         SIBLING,
291 
292         /**
293          * Child.
294          */
295         CHILD,
296 
297         /**
298          * Parent.
299          */
300         PARENT;
301     }
302 }