-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Guy Davenport
committed
Oct 14, 2024
1 parent
15e3e39
commit d6c2aee
Showing
11 changed files
with
494 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
java/core/src/main/java/org/brapi/schematools/core/utils/BrAPITClassCacheUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.brapi.schematools.core.utils; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.brapi.schematools.core.model.*; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Utility class for creating a cache of {@link BrAPIClass}es. Takes a list of | ||
* classes and caches those in the list if they pass the provided {@link #cachePredicate}. | ||
* Additional classes are added to the cached depending on the subclass of {@link BrAPIClass} | ||
* For {@link BrAPIObjectType} utility checks the properties and | ||
* tries to cache any that are the return type of these properties {@link BrAPIClass}es. | ||
* If an {@link BrAPIArrayType} is encountered then the {@link BrAPIArrayType#getItems()} is | ||
* checked recursively to be included in the cache. | ||
* For {@link BrAPIOneOfType} it is added to the cache and any of {@link BrAPIOneOfType#getPossibleTypes()} | ||
* are checked recursively to be included in the cache. | ||
* {@link BrAPIAllOfType} are ignored. | ||
*/ | ||
@AllArgsConstructor | ||
public class BrAPITClassCacheUtil { | ||
|
||
private Predicate<BrAPIClass> cachePredicate ; | ||
|
||
public Map<String, BrAPIClass> createMap(List<BrAPIClass> brAPIClasses) { | ||
return new Cache(brAPIClasses).brAPIClassMap ; | ||
} | ||
|
||
private class Cache { | ||
|
||
private final Map<String, BrAPIClass> brAPIClassMap ; | ||
|
||
public Cache(List<BrAPIClass> brAPIClasses) { | ||
|
||
brAPIClassMap = new TreeMap<>(); | ||
|
||
for (BrAPIClass brAPIClass : brAPIClasses) { | ||
cacheClass(brAPIClass); | ||
} | ||
} | ||
|
||
private void cacheClass(BrAPIType brAPIType) { | ||
if (brAPIType instanceof BrAPIClass brAPIClass && cachePredicate.test(brAPIClass) && !brAPIClassMap.containsKey(brAPIClass.getName())) { | ||
if (brAPIClass instanceof BrAPIObjectType brAPIObjectType) { | ||
brAPIClassMap.put(brAPIClass.getName(), brAPIObjectType); | ||
|
||
brAPIObjectType.getProperties().forEach(property -> cacheClass(property.getType())); | ||
} else if (brAPIType instanceof BrAPIOneOfType brAPIOneOfType) { | ||
brAPIClassMap.put(brAPIClass.getName(), brAPIOneOfType); | ||
|
||
brAPIOneOfType.getPossibleTypes().forEach(this::cacheClass); | ||
} else if (brAPIClass instanceof BrAPIEnumType brAPIEnumType) { | ||
brAPIClassMap.put(brAPIClass.getName(), brAPIEnumType); | ||
} | ||
} else if (brAPIType instanceof BrAPIArrayType brAPIArrayType) { | ||
cacheClass(brAPIArrayType.getItems()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.