X

Geertjan's Blog

  • December 27, 2010

Central Lookup

Geertjan Wielenga
Product Manager
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;
}
}

Join the discussion

Comments ( 2 )
  • silhanek Monday, December 27, 2010

    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).


  • Wade Chandler Wednesday, December 29, 2010

    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.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha