001: package com.vividsolutions.jump.workbench.registry;
002:
003: import java.util.*;
004:
005: import com.vividsolutions.jump.util.CollectionMap;
006:
007: /**
008: * While a Registry is similar to a Blackboard in that they are both flexible
009: * repositories of information, there are some subtle differences:
010: * <ul>
011: * <li>The Registry is a bit more structured (values are Lists as opposed to
012: * general Objects).
013: * <li>There is only one Registry, whereas there are Blackboards on several
014: * different levels (the Workbench Blackboard, the Task Blackboard, the Layer
015: * Blackboard, the LayerViewPanel Blackboard), thus representing varying degrees
016: * of scope.
017: * <li>Registry keys are in general "well known" to a greater degree than
018: * Blackboard keys, which plugins tend to create as needed. Thus the Registry
019: * can be thought of as being more static, and the Blackboard more fluid.
020: * <li>Registry entries are intended to be much more static than Blackboard
021: * entries. You might well think about persisting a Registry, but probably never
022: * a Blackboard
023: * <li>In the bigger world, Registries have all kinds of security,
024: * classification and lifecyle features that probably would not appear on a
025: * Blackboard.
026: * </ul>
027: *
028: * @author jaquino
029: * @author dzwiers
030: */
031: public class Registry {
032:
033: private CollectionMap classificationToEntriesMap = new CollectionMap();
034:
035: /**
036: * @param classification
037: * @param entry
038: * @return The current Registry
039: *
040: * @throws ClassCastException When the entry does not match a registered Classification Type
041: * @see Registry#createClassification(Object, Class)
042: */
043: public Registry createEntry(Object classification, Object entry)
044: throws ClassCastException {
045: Class c = (Class) typeMap.get(classification);
046: if (c != null) {
047: // check class type
048: if (!c.isInstance(entry)) {
049: throw new ClassCastException("Cannot Cast '" + entry
050: + "' into " + c.getName()
051: + " for classification '" + classification
052: + "'");
053: }
054: }
055: classificationToEntriesMap.addItem(classification, entry);
056: return this ;
057: }
058:
059: /**
060: * @param classification
061: * @param entries
062: * @return The current Registry
063: *
064: * @throws ClassCastException When the entries do not match a registered Classification Type
065: * @see Registry#createClassification(Object, Class)
066: */
067: public Registry createEntries(Object classification,
068: Collection entries) throws ClassCastException {
069: for (Iterator i = entries.iterator(); i.hasNext();) {
070: createEntry(classification, i.next());
071: }
072: return this ;
073: }
074:
075: public List getEntries(Object classification) {
076: return (List) new ArrayList(classificationToEntriesMap
077: .getItems(classification));
078: }
079:
080: // new api
081: private Map typeMap = new HashMap(); // HashMap<Object,Class> --- use this for templating in jvm1.5
082:
083: /**
084: * Sets up the registry to be type-safe for a particular classification.
085: * Should the user not specify a type mappingthrough this method, no
086: * checks will be performed.
087: *
088: * @param classification
089: * @param type
090: * @return The current Registry
091: *
092: * @throws ClassCastException When the existing entries do not match Classification Type being registered.
093: */
094: public Registry createClassification(Object classification,
095: Class type) throws ClassCastException {
096: if (classificationToEntriesMap.containsKey(classification)) {
097: // need to check here
098: Collection c = classificationToEntriesMap
099: .getItems(classification);
100: if (c != null) {
101: for (Iterator i = c.iterator(); i.hasNext();) {
102: Object entry = i.next();
103: if (!type.isInstance(entry)) {
104: throw new ClassCastException("Cannot Cast '"
105: + entry + "' into " + type.getName()
106: + " for classification '"
107: + classification + "'");
108: }
109: }
110: }
111: }
112: typeMap.put(classification, type);
113: return this;
114: }
115: }
|