Author: cziegeler
Date: Tue Oct 18 14:22:21 2016
New Revision: 1765445

URL: http://svn.apache.org/viewvc?rev=1765445&view=rev
Log:
SLING-6056 : achieve 1:1 mapping between observation and resource change 
listener

Modified:
    
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObservationReporter.java
    
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObserverConfiguration.java
    
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java

Modified: 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObservationReporter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObservationReporter.java?rev=1765445&r1=1765444&r2=1765445&view=diff
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObservationReporter.java
 (original)
+++ 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObservationReporter.java
 Tue Oct 18 14:22:21 2016
@@ -130,7 +130,7 @@ public class BasicObservationReporter im
                             types.addAll(info.getResourceChangeTypes());
                             config = new BasicObserverConfiguration(pathSet,
                                 types,
-                                info.isExternal() && found.includeExternal(),
+                                info.isExternal() || found.includeExternal(),
                                 found.getExcludedPaths(),
                                 found.getPropertyNamesHint());
                             observerConfigs.add(config);

Modified: 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObserverConfiguration.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObserverConfiguration.java?rev=1765445&r1=1765444&r2=1765445&view=diff
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObserverConfiguration.java
 (original)
+++ 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/BasicObserverConfiguration.java
 Tue Oct 18 14:22:21 2016
@@ -74,6 +74,7 @@ public class BasicObserverConfiguration
      */
     public void addListener(final ResourceChangeListenerInfo listener) {
         this.listeners.add(listener);
+        Collections.sort(this.listeners);
     }
 
     /**

Modified: 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java?rev=1765445&r1=1765444&r2=1765445&view=diff
==============================================================================
--- 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java
 (original)
+++ 
sling/trunk/bundles/resourceresolver/src/main/java/org/apache/sling/resourceresolver/impl/observation/ResourceChangeListenerInfo.java
 Tue Oct 18 14:22:21 2016
@@ -27,6 +27,7 @@ import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.TreeSet;
 
 import org.apache.sling.api.resource.ResourceUtil;
 import 
org.apache.sling.api.resource.observation.ExternalResourceChangeListener;
@@ -40,7 +41,7 @@ import org.osgi.framework.ServiceReferen
 /**
  * Information about a resource change listener.
  */
-public class ResourceChangeListenerInfo {
+public class ResourceChangeListenerInfo implements 
Comparable<ResourceChangeListenerInfo> {
 
     private static final Set<ChangeType> DEFAULT_CHANGE_RESOURCE_TYPES = 
EnumSet.of(ChangeType.ADDED, ChangeType.REMOVED, ChangeType.CHANGED);
 
@@ -197,6 +198,63 @@ public class ResourceChangeListenerInfo
         this.external = listener instanceof ExternalResourceChangeListener;
     }
 
+    private int compareSet(final Set<String> t, final Set<String> o) {
+        if ( t == null && o == null ) {
+            return 0;
+        }
+        if ( t == null ) {
+            return -1;
+        }
+        if ( o == null ) {
+            return 1;
+        }
+        final Set<String> tPaths = new TreeSet<>(t);
+        final Set<String> oPaths = new TreeSet<>(o);
+
+        int result = tPaths.size() - oPaths.size();
+        if ( result == 0 ) {
+            final Iterator<String> tPathsIter = tPaths.iterator();
+            final Iterator<String> oPathsIter = oPaths.iterator();
+            while ( result == 0 && tPathsIter.hasNext() ) {
+                result = tPathsIter.next().compareTo(oPathsIter.next());
+            }
+        }
+        return result;
+    }
+
+    private int compareChangeTypes(final Set<ChangeType> t, final 
Set<ChangeType> o) {
+        int result = t.size() - o.size();
+        if ( result == 0 ) {
+            final Iterator<ChangeType> tIter = t.iterator();
+            final Iterator<ChangeType> oIter = o.iterator();
+            while ( result == 0 && tIter.hasNext() ) {
+                result = tIter.next().compareTo(oIter.next());
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public int compareTo(final ResourceChangeListenerInfo o) {
+        // paths first
+        int result = compareSet(this.paths.toStringSet(), 
o.paths.toStringSet());
+        if ( result == 0 ) {
+            // hints
+            result = compareSet(this.propertyNamesHint, o.propertyNamesHint);
+            if ( result == 0 ) {
+                // external next
+                result = Boolean.valueOf(this.external).compareTo(o.external);
+                if ( result == 0 ) {
+                    result = compareChangeTypes(this.resourceChangeTypes, 
o.resourceChangeTypes);
+                    if ( result == 0 ) {
+                        result = compareChangeTypes(this.providerChangeTypes, 
o.providerChangeTypes);
+                    }
+                }
+            }
+        }
+        return result;
+    }
+
     @Override
     public String toString() {
         return "ResourceChangeListenerInfo [paths=" + paths + ", 
resourceChangeTypes=" + resourceChangeTypes


Reply via email to