changeset 405:e1738650bfca

FacetActivity: use Chain-of-responsibility pattern to figure out if facet should be active. artifacts/trunk@5154 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 29 Jul 2012 11:38:40 +0000
parents 4c78c5d2b367
children 10aa92f2eca0
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java
diffstat 2 files changed, 39 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jul 29 09:48:05 2012 +0000
+++ b/ChangeLog	Sun Jul 29 11:38:40 2012 +0000
@@ -1,3 +1,9 @@
+2012-07-29	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java:
+	  Refactored registry to use a Chain-of-responsibility pattern. This allows
+	  de-centralized facet activity treatment like in the facet producing states.
+
 2012-07-29	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java	Sun Jul 29 09:48:05 2012 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java	Sun Jul 29 11:38:40 2012 +0000
@@ -2,56 +2,78 @@
 
 import de.intevation.artifacts.Artifact;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public interface FacetActivity
 {
     public static final FacetActivity ACTIVE = new FacetActivity() {
         @Override
-        public boolean isInitialActive(
+        public Boolean isInitialActive(
             Artifact artifact,
             Facet    facet,
             String   output
         ) {
-            return true;
+            return Boolean.TRUE;
         }
     };
 
     public static final FacetActivity INACTIVE = new FacetActivity() {
         @Override
-        public boolean isInitialActive(
+        public Boolean isInitialActive(
             Artifact artifact,
             Facet    facet,
             String   output
         ) {
-            return false;
+            return Boolean.FALSE;
         }
     };
 
-    boolean isInitialActive(Artifact artifact, Facet facet, String output);
+    Boolean isInitialActive(Artifact artifact, Facet facet, String output);
 
     public static final class Registry {
 
         private static final Registry INSTANCE = new Registry();
 
-        private Map<String, FacetActivity> activities;
+        private Map<String, List<FacetActivity>> activities;
 
         private Registry() {
-            activities = new HashMap<String, FacetActivity>();
+            activities = new HashMap<String, List<FacetActivity>>();
         }
 
         public static Registry getInstance() {
             return INSTANCE;
         }
 
-        public synchronized FacetActivity getActivity(String key) {
-            FacetActivity activity = activities.get(key);
-            return activity != null ? activity : ACTIVE;
+        public synchronized boolean isInitialActive(
+            String   key,
+            Artifact artifact,
+            Facet    facet,
+            String   output
+        ) {
+            List<FacetActivity> activityList = activities.get(key);
+            if (activityList == null) {
+                return true;
+            }
+            for (FacetActivity activity: activityList) {
+                Boolean isActive =
+                    activity.isInitialActive(artifact, facet, output);
+                if (isActive != null) {
+                    return isActive;
+                }
+            }
+            return true;
         }
 
         public synchronized void register(String key, FacetActivity activity) {
-            activities.put(key, activity);
+            List<FacetActivity> activityList = activities.get(key);
+            if (activityList == null) {
+                activityList = new ArrayList<FacetActivity>(3);
+                activities.put(key, activityList);
+            }
+            activityList.add(activity);
         }
     }
 }

http://dive4elements.wald.intevation.org