@@ -28,6 +28,8 @@ public class BasicWorkspace implements Workspace {
2828 private final WorkspaceResource primary ;
2929 private final List <WorkspaceResource > supporting = new ArrayList <>();
3030 private final List <WorkspaceResource > internal ;
31+ private List <WorkspaceResource > cachedAllResourcesNoInternal ;
32+ private List <WorkspaceResource > cachedAllResources ;
3133
3234 /**
3335 * @param primary
@@ -92,6 +94,7 @@ public List<WorkspaceResource> getInternalSupportingResources() {
9294
9395 @ Override
9496 public void addSupportingResource (@ Nonnull WorkspaceResource resource ) {
97+ cachedAllResources = null ;
9598 supporting .add (resource );
9699 Unchecked .checkedForEach (modificationListeners , listener -> listener .onAddLibrary (this , resource ),
97100 (listener , t ) -> logger .error ("Exception thrown when adding supporting resource" , t ));
@@ -101,6 +104,7 @@ public void addSupportingResource(@Nonnull WorkspaceResource resource) {
101104 public boolean removeSupportingResource (@ Nonnull WorkspaceResource resource ) {
102105 boolean remove = supporting .remove (resource );
103106 if (remove ) {
107+ cachedAllResources = null ;
104108 Unchecked .checkedForEach (modificationListeners , listener -> listener .onRemoveLibrary (this , resource ),
105109 (listener , t ) -> logger .error ("Exception thrown when removing supporting resource" , t ));
106110 }
@@ -123,6 +127,24 @@ public void removeWorkspaceModificationListener(@Nonnull WorkspaceModificationLi
123127 modificationListeners .remove (listener );
124128 }
125129
130+ @ Nonnull
131+ @ Override
132+ public List <WorkspaceResource > getAllResources (boolean includeInternal ) {
133+ // Cache the list of all resources since it is commonly used and the underlying stream
134+ // collection in the base implementation is expensive to compute.
135+ if (includeInternal ) {
136+ List <WorkspaceResource > cached = this .cachedAllResources ;
137+ if (cached != null )
138+ return cached ;
139+ return this .cachedAllResources = Workspace .super .getAllResources (false );
140+ } else {
141+ List <WorkspaceResource > cached = this .cachedAllResourcesNoInternal ;
142+ if (cached != null )
143+ return cached ;
144+ return this .cachedAllResourcesNoInternal = Workspace .super .getAllResources (false );
145+ }
146+ }
147+
126148 /**
127149 * Called by {@link WorkspaceManager} when the workspace is closed.
128150 */
0 commit comments