Central Lookup

Posting Wade's central lookup here in full, since whenever I need it, I can't find it online:
/\*\*
 \* Class used to house anything one might want to store
 \* in a central lookup which can affect anything within
 \* the application. It can be thought of as a central context
 \* where any application data may be stored and watched.
 \* 
 \* A singleton instance is created using @see getDefault().
 \* This class is as thread safe as Lookup. Lookup appears to be safe.
 \* @author Wade Chandler
 \* @version 1.0
 \*/
public class CentralLookup extends AbstractLookup {

    private InstanceContent content = null;
    private static CentralLookup def = new CentralLookup();

    /\*\*
     \* Creates a CentralLookup instances with a specific content set.
     \* @param content the InstanceContent to use
     \*/
    public CentralLookup(InstanceContent content) {
        super(content);
        this.content = content;
    }

    /\*\*
     \* Creates a new CentralLookup
     \*/
    public CentralLookup() {
        this(new InstanceContent());
    }

    /\*\*
     \* Adds an instance to the Lookup. The instance will be added with the classes
     \* in its hierarchy as keys which may be used to lookup the instance(s).
     \* @param instance The instance to add
     \*/
    public void add(Object instance) {
        content.add(instance);
    }

    /\*\*
     \* Removes the specific instance from the Lookup content.
     \* @param instance The specific instance to remove.
     \*/
    public void remove(Object instance) {
        content.remove(instance);
    }

    /\*\*
     \* Returns the default CentralLookup. This can be used as an application context for
     \* the entire application. If needed CentralLookup may be used directly through the
     \* constructors to allow for more than one if needed. CentralLookup is nothing more
     \* than an InstanceContent instance wrapped in a Lookup with the add and remove methods
     \* added to make updating the data easier.
     \* @return The default CentralLookup which is global in nature.
     \*/
    public static CentralLookup getDefault() {
        return def;
    }

}

Comments:

I think, the CentralLookup(InstanceContent) constructor is useless if this Lookup is able to maintain its content directly.

I suggest factory method for named instances instead of it - getLookp(String name).

Posted by silhanek on December 27, 2010 at 02:28 AM PST #

The constructor taking the InstanceContent allows for one to have some arbitrary InstanceContent they may be using directly in some module and also expose it as a central lookup without replacing that InstanceContent directly. That lets one then use the eventing and lookup interface of the Lookup class along with InstanceContent or share it as different views: central lookup, lookup, or content.

I could see some kind of a factory method regardless. That lets one have a common place to locate contextually bound global instances bound in a HashMap. I might rather it take some hard class instead of a String perhaps, could be a shared application class, to make it compile time checked so one is forced to use a more consistent value in their application.

getInstance(Class<?> theClass)

getInstance(my.shared.package.SomeClass.class)

This way misspellings don't hamper things, and contextually it still has meaning to the application. Bit more overhead as you'd probably want to use a class given that specific purpose, but probably better than the debug costs of misuse some where in the application.

Posted by Wade Chandler on December 28, 2010 at 07:51 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
19
21
22
23
24
25
26
27
28
29
30
   
       
Today