diff artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java @ 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 15179c77aa1d
line wrap: on
line diff
--- 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