comparison artifact-database/src/main/java/org/dive4elements/artifactdatabase/state/FacetActivity.java @ 473:d0ac790a6c89 dive4elements-move

Moved directories to org.dive4elements
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 10:57:18 +0200
parents artifact-database/src/main/java/de/intevation/artifactdatabase/state/FacetActivity.java@f503374f516c
children 415df0fc4fa1
comparison
equal deleted inserted replaced
472:783cc1b6b615 473:d0ac790a6c89
1 package de.intevation.artifactdatabase.state;
2
3 import de.intevation.artifacts.Artifact;
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.apache.log4j.Logger;
11
12
13 /**
14 * System used in practice used by AttributeWriter in flys-artifacts to decide
15 * whether a facet is initially active.
16 * Provides a singleton Registry into which FacetActivities can be registered
17 * under a key (in practice the artifacts name. This Registry is queried for
18 * new Facets in order to find whether they are active or inactive.
19 */
20 public interface FacetActivity
21 {
22 /** Static 'activity' that lets all facets be active. */
23 public static final FacetActivity ACTIVE = new FacetActivity() {
24 @Override
25 public Boolean isInitialActive(
26 Artifact artifact,
27 Facet facet,
28 String output
29 ) {
30 return Boolean.TRUE;
31 }
32 };
33
34 /** Static 'activity' that lets all facets be inactive. */
35 public static final FacetActivity INACTIVE = new FacetActivity() {
36 @Override
37 public Boolean isInitialActive(
38 Artifact artifact,
39 Facet facet,
40 String output
41 ) {
42 return Boolean.FALSE;
43 }
44 };
45
46 Boolean isInitialActive(Artifact artifact, Facet facet, String output);
47
48 /** Singleton registry, that maps artifact names to the activities, which
49 * decide whether or not a facet should be (initially) active. */
50 public static final class Registry {
51
52 /** The logger for this class. */
53 private static Logger logger = Logger.getLogger(Registry.class);
54
55 /** Singleton instance. */
56 private static final Registry INSTANCE = new Registry();
57
58 /** Map of keys (artifact names) to the activities. */
59 private Map<String, List<FacetActivity>> activities;
60
61 /** Private singleton constructor for the Facet-Activity-Registry. */
62 private Registry() {
63 activities = new HashMap<String, List<FacetActivity>>();
64 }
65
66 /** Access Singleton instance. */
67 public static Registry getInstance() {
68 return INSTANCE;
69 }
70
71 /** Queries whether a given facet should be active or not. */
72 public synchronized boolean isInitialActive(
73 String key,
74 Artifact artifact,
75 Facet facet,
76 String output
77 ) {
78 List<FacetActivity> activityList = activities.get(key);
79 if (activityList == null) {
80 logger.debug("FacetActivity.Registry: No activity " +
81 "registered for " + key);
82 return true;
83 }
84 if (activityList.size() != 1) {
85 logger.warn("FacetActivity.Registry: More than one " +
86 "FacetActivity registered for " + key);
87 }
88 for (FacetActivity activity: activityList) {
89 Boolean isActive =
90 activity.isInitialActive(artifact, facet, output);
91 // Nice. Only, in practice they never return NULL.
92 if (isActive != null) {
93 return isActive;
94 }
95 }
96 return true;
97 }
98
99
100 /** Add a FacetActivity under given key (usually artifacts name). */
101 public synchronized void register(String key, FacetActivity activity) {
102 List<FacetActivity> activityList = activities.get(key);
103 if (activityList == null) {
104 activityList = new ArrayList<FacetActivity>(3);
105 activities.put(key, activityList);
106 }
107 activityList.add(activity);
108 }
109 }
110 }
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org