2424import java .util .Collection ;
2525import java .util .HashMap ;
2626import java .util .Map ;
27+ import java .util .UUID ;
2728import java .util .function .BiConsumer ;
2829import java .util .function .Function ;
2930
3536import org .omg .sysml .util .traversal .Traversal ;
3637import org .omg .sysml .util .traversal .facade .impl .ElementIdProcessingFacade ;
3738
39+ /**
40+ * Class to track UUIDs while fetching from the repository. The tracker can be
41+ * pre-populated by locally existing UUID. The standard library UUIDs are tracked separately
42+ * and they shadow user UUIDs (non-standard library UUIDs).
43+ */
3844public class EObjectUUIDTracker {
39- private Map <Object , EObject > uuidToLibraryElement = new HashMap <>();
40- private Map <Object , EObject > uuidToUserElement = new HashMap <>();
45+ private Map <UUID , EObject > uuidToLibraryElement = new HashMap <>();
46+ private Map <UUID , EObject > uuidToUserElement = new HashMap <>();
4147
48+ /**
49+ * Collects UUIDs from the given library resources
50+ *
51+ * @param libraryResources resources containing standard library models
52+ */
4253 public void trackLibraryUUIDs (Collection <Resource > libraryResources ) {
4354 trackUUIDSFromResources (libraryResources , uuidToLibraryElement );
4455 }
4556
57+ /**
58+ * Collects UUIDs from non-standard library models.
59+ *
60+ * @param userResources resources containing user models
61+ */
4662 public void trackUserUUIDs (Collection <Resource > userResources ) {
4763 trackUUIDSFromResources (userResources , uuidToUserElement );
4864 }
4965
50- private void trackUUIDSFromResources (Collection <Resource > resources , Map <Object , EObject > uuidToElementMap ) {
66+ private void trackUUIDSFromResources (Collection <Resource > resources , Map <UUID , EObject > uuidToElementMap ) {
5167 //avoid concurrent modification exception in the traversal by transforming the library
5268 for (Resource resource : resources ) {
5369 if (resource instanceof XtextResource ) {
@@ -65,30 +81,61 @@ private void trackUUIDSFromResources(Collection<Resource> resources, Map<Object
6581 });
6682 }
6783
84+ /**
85+ * Checks if the tracker contains an index for the library
86+ *
87+ * @return true if the library has been tracked
88+ */
6889 public boolean isLibraryTracked () {
6990 return !uuidToLibraryElement .isEmpty ();
7091 }
7192
72- public void trackUserElement (Object uuid , EObject element ) {
93+ /**
94+ * Used to track a user element by its UUID
95+ *
96+ * @param uuid UUID to track
97+ * @param element element identified by the UUID
98+ */
99+ public void trackUserElement (UUID uuid , EObject element ) {
73100 uuidToUserElement .put (uuid , element );
74101 }
75102
76- public EObject createIfMissingAndTrack (Object uuid , Function <? super Object , ? extends EObject > compute ) {
103+ /**
104+ * Returns the element if it has been tracked by its UUID
105+ * otherwise creates the element using the provided compute and tracks it.
106+ *
107+ * @param uuid UUID of the element
108+ * @param compute Function to compute the Element in case it's not tracked
109+ */
110+ public EObject createIfMissingAndTrack (UUID uuid , Function <? super UUID , ? extends EObject > compute ) {
77111 return uuidToUserElement .computeIfAbsent (uuid , compute );
78112 }
79113
80- public EObject get (Object uuid ) {
114+ /**
115+ * Returns the element identified by a UUID. Library UUIDs are checked first.
116+ *
117+ * @return element identified by the provided UUID or null if UUID is not tracked
118+ */
119+ public EObject get (UUID uuid ) {
81120 if (uuidToLibraryElement .containsKey (uuid )) {
82121 return uuidToLibraryElement .get (uuid );
83122 } else {
84123 return uuidToUserElement .get (uuid );
85124 }
86125 }
87126
88- public void forEachTrackedUserElement (BiConsumer <? super Object , ? super EObject > consumer ) {
127+ /**
128+ * Iterates user elements passing them to the provided consumer
129+ *
130+ * @param consumer logic to execute on each element
131+ */
132+ public void forEachTrackedUserElement (BiConsumer <? super UUID , ? super EObject > consumer ) {
89133 uuidToUserElement .forEach (consumer );
90134 }
91135
136+ /**
137+ * Removes all user element from the tracker
138+ */
92139 public void clearTrackedUserElements () {
93140 uuidToUserElement .clear ();
94141 }
0 commit comments