incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1455511 - in /sling/trunk/bundles/api/src: main/java/org/apache/sling/api/resource/ResourceMetadata.java test/java/org/apache/sling/api/resource/ResourceMetadataTest.java
Date Tue, 12 Mar 2013 12:45:55 GMT
Author: bdelacretaz
Date: Tue Mar 12 12:45:55 2013
New Revision: 1455511

URL: http://svn.apache.org/r1455511
Log:
SLING-2785 - avoid recursion in get methods

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceMetadataTest.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java?rev=1455511&r1=1455510&r2=1455511&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
(original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceMetadata.java
Tue Mar 12 12:45:55 2013
@@ -306,7 +306,7 @@ public class ResourceMetadata extends Ha
      */
     private void checkReadOnly() {
         if ( this.isReadOnly ) {
-            throw new UnsupportedOperationException();
+            throw new UnsupportedOperationException(getClass().getSimpleName() + " is locked");
         }
     }
 
@@ -334,36 +334,37 @@ public class ResourceMetadata extends Ha
         return super.remove(key);
     }
 
-    private Map<String, Object> unmodifiableMap;
-
-    private Map<String, Object> getUnmodifiableMap() {
-        if ( this.unmodifiableMap == null ) {
-            this.unmodifiableMap = Collections.unmodifiableMap(this);
+    private Set<Map.Entry<String, Object>> lockedEntrySet;
+    private Set<String> lockedKeySet;
+    private Collection<Object> lockedValues;
+
+    private void getLockedData() {
+        if(isReadOnly && lockedEntrySet == null) {
+            synchronized (this) {
+                if(isReadOnly && lockedEntrySet == null) {
+                    lockedEntrySet = Collections.unmodifiableSet(super.entrySet());
+                    lockedKeySet = Collections.unmodifiableSet(super.keySet());
+                    lockedValues = Collections.unmodifiableCollection(super.values());
+                }
+            }
         }
-        return this.unmodifiableMap;
     }
-
+    
     @Override
-    public Set<java.util.Map.Entry<String, Object>> entrySet() {
-        if ( this.isReadOnly ) {
-            return this.getUnmodifiableMap().entrySet();
-        }
-        return super.entrySet();
+    public Set<Map.Entry<String, Object>> entrySet() {
+        getLockedData();
+        return lockedEntrySet != null ? lockedEntrySet : super.entrySet();
     }
 
     @Override
     public Set<String> keySet() {
-        if ( this.isReadOnly ) {
-            return this.getUnmodifiableMap().keySet();
-        }
-        return super.keySet();
+        getLockedData();
+        return lockedKeySet != null ? lockedKeySet : super.keySet();
     }
 
     @Override
     public Collection<Object> values() {
-        if ( this.isReadOnly ) {
-            return this.getUnmodifiableMap().values();
-        }
-        return super.values();
+        getLockedData();
+        return lockedValues != null ? lockedValues : super.values();
     }
 }

Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceMetadataTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceMetadataTest.java?rev=1455511&r1=1455510&r2=1455511&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceMetadataTest.java
(original)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceMetadataTest.java
Tue Mar 12 12:45:55 2013
@@ -81,21 +81,21 @@ public class ResourceMetadataTest {
         }
     }
     
-    @Test(expected=StackOverflowError.class)
+    @Test
     public void testLockedEntrySet() {
         final ResourceMetadata m = new ResourceMetadata();
         m.lock();
         m.entrySet().toString();
     }
     
-    @Test(expected=StackOverflowError.class)
+    @Test
     public void testLockedKeySet() {
         final ResourceMetadata m = new ResourceMetadata();
         m.lock();
         m.keySet().toString();
     }
     
-    @Test(expected=StackOverflowError.class)
+    @Test
     public void testLockedValues() {
         final ResourceMetadata m = new ResourceMetadata();
         m.lock();



Mime
View raw message