-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make work when the references are to a supertype of type hierarchy
- Loading branch information
Showing
4 changed files
with
163 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,36 +8,55 @@ | |
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* A context for storing ephemeral information about a model. | ||
* @author Paul Harrison ([email protected]) | ||
* //TODO ensure thread safety. | ||
*/ | ||
public class ModelContext { | ||
|
||
//force instances to be instantiated via create() | ||
private ModelContext() { | ||
|
||
}; | ||
|
||
private static ModelContext instance; | ||
|
||
public ModelContext(Map<Class, ReferenceCache> mm) { | ||
private ModelContext(Map<Class, ReferenceCache> mm) { | ||
this.mm = mm; | ||
} | ||
|
||
private Map<Class, ReferenceCache> mm; | ||
|
||
public static ModelContext current() { | ||
public static synchronized ModelContext current() { | ||
if (instance != null) | ||
return instance; | ||
else | ||
throw new IllegalStateException("the context has not been set"); | ||
} | ||
|
||
/** | ||
* create a new model context with the associated reference cache. | ||
* @param m the reference cache. | ||
*/ | ||
public static void create(Map<Class, ReferenceCache> m) { | ||
instance = new ModelContext(m); | ||
} | ||
|
||
/** | ||
* Return the cache for a particular reference type. | ||
* @param <T> the type of the reference. | ||
* @param clazz the type of the reference. | ||
* @return the cache. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public <T> ReferenceCache<T> cache(Class<T> clazz) { | ||
return mm.get(clazz); | ||
} | ||
|
||
/** | ||
* @return | ||
* List the reference types contained in the context. | ||
* @return the set of types. | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
public Set<Class> containedRefs() { | ||
|
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 |
---|---|---|
|
@@ -8,20 +8,32 @@ | |
import java.util.Map; | ||
|
||
|
||
/** | ||
* A reference cache implementation to help with processing of contained references. | ||
* The type parameter is the type of the references contained within the cache instance. | ||
* @author Paul Harrison ([email protected]) | ||
* @since 15 Feb 2024 | ||
*/ | ||
public class ReferenceCache <T> { | ||
|
||
private Map<T, T> valmap = new HashMap<>(); | ||
|
||
public void setValues(List<T> inital, List<T> cloned) | ||
/** | ||
* Store values in the cache. | ||
* @param inital - the original reference. | ||
* @param cloned - the cloned value of the same reference. | ||
*/ | ||
public void setValues(List<T> inital, List<T> cloned) | ||
{ | ||
for (int i = 0; i < inital.size(); i++) { | ||
valmap.put(inital.get(i), cloned.get(i)) ; | ||
} | ||
} | ||
|
||
/** | ||
* @param refcont | ||
* @return | ||
* Get the new instance of the reference. | ||
* @param initial the original value of the reference. | ||
* @return the new instance of the reference. | ||
*/ | ||
public T get(T initial) { | ||
return valmap.get(initial); | ||
|
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
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