Author: sseifert Date: Mon Sep 5 20:06:48 2016 New Revision: 1759351 URL: http://svn.apache.org/viewvc?rev=1759351&view=rev Log: SLING-6036 ResourceBuilder: Reusing ResourceBuilder instances
Modified: sling/trunk/bundles/extensions/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java sling/trunk/bundles/extensions/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java Modified: sling/trunk/bundles/extensions/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java?rev=1759351&r1=1759350&r2=1759351&view=diff ============================================================================== --- sling/trunk/bundles/extensions/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java (original) +++ sling/trunk/bundles/extensions/resourcebuilder/src/main/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImpl.java Mon Sep 5 20:06:48 2016 @@ -59,8 +59,18 @@ public class ResourceBuilderImpl impleme } originalParent = parent; resourceResolver = originalParent.getResourceResolver(); - withIntermediatePrimaryType(null); - atParent(); + intermediatePrimaryType = DEFAULT_PRIMARY_TYPE; + currentParent = parent; + hierarchyMode = true; + } + + private ResourceBuilderImpl cloneResourceBuilder(Resource newCurrentParent, + String newIntermediatePrimaryType, boolean newHierarchyMode) { + ResourceBuilderImpl clone = new ResourceBuilderImpl(originalParent, mimeTypeService); + clone.currentParent = newCurrentParent; + clone.intermediatePrimaryType = newIntermediatePrimaryType; + clone.hierarchyMode = newHierarchyMode; + return clone; } @Override @@ -80,9 +90,7 @@ public class ResourceBuilderImpl impleme @Override public ResourceBuilder atParent() { - currentParent = originalParent; - hierarchyMode(); - return this; + return cloneResourceBuilder(originalParent, this.intermediatePrimaryType, true); } private boolean isAbsolutePath(String path) { @@ -145,10 +153,10 @@ public class ResourceBuilderImpl impleme "PersistenceException while creating Resource " + fullPath, pex); } - if(r == null) { + if (r == null) { throw new RuntimeException("Failed to get or create resource " + fullPath); } else if(hierarchyMode) { - currentParent = r; + return cloneResourceBuilder(r, this.intermediatePrimaryType, this.hierarchyMode); } return this; } @@ -231,9 +239,8 @@ public class ResourceBuilderImpl impleme throw new RuntimeException("Unable to get or created file resource " + relativePath + " under " + currentParent.getPath()); } if(hierarchyMode) { - currentParent = file; + return cloneResourceBuilder(file, this.intermediatePrimaryType, this.hierarchyMode); } - return this; } @@ -244,20 +251,18 @@ public class ResourceBuilderImpl impleme @Override public ResourceBuilder withIntermediatePrimaryType(String primaryType) { - intermediatePrimaryType = primaryType == null ? DEFAULT_PRIMARY_TYPE : primaryType; - return this; + String intermediatePrimaryType = primaryType == null ? DEFAULT_PRIMARY_TYPE : primaryType; + return cloneResourceBuilder(currentParent, intermediatePrimaryType, hierarchyMode); } @Override public ResourceBuilder siblingsMode() { - hierarchyMode = false; - return this; + return cloneResourceBuilder(currentParent, intermediatePrimaryType, false); } @Override public ResourceBuilder hierarchyMode() { - hierarchyMode = true; - return this; + return cloneResourceBuilder(currentParent, intermediatePrimaryType, true); } @Override Modified: sling/trunk/bundles/extensions/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java?rev=1759351&r1=1759350&r2=1759351&view=diff ============================================================================== --- sling/trunk/bundles/extensions/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java (original) +++ sling/trunk/bundles/extensions/resourcebuilder/src/test/java/org/apache/sling/resourcebuilder/impl/ResourceBuilderImplTest.java Mon Sep 5 20:06:48 2016 @@ -30,6 +30,7 @@ import org.apache.sling.api.resource.Res import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceUtil; import org.apache.sling.commons.mime.MimeTypeService; +import org.apache.sling.resourcebuilder.api.ResourceBuilder; import org.apache.sling.resourcebuilder.test.ResourceAssertions; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.junit.SlingContext; @@ -345,5 +346,24 @@ public class ResourceBuilderImplTest { A.assertResource("/g/h/i/j/l/m"); A.assertResource("/o/p/q"); } - + + @Test + public void reuseInstance() throws Exception { + ResourceBuilder content = new ResourceBuilderService() + .forResolver(resourceResolver) + .resource("/content"); + content.resource("a"); + content.resource("b/c"); + content.resource("/test") + .siblingsMode() + .resource("1") + .resource("2"); + + A.assertResource("/content/a"); + A.assertResource("/content/b/c"); + A.assertResource("/test"); + A.assertResource("/test/1"); + A.assertResource("/test/2"); + } + }