Class DefaultObjectNamer<TO extends DefaultNamedObject<TO>>

java.lang.Object
org.bzdev.obnaming.DefaultObjectNamer<TO>
All Implemented Interfaces:
ObjectNamerOps<TO>

public class DefaultObjectNamer<TO extends DefaultNamedObject<TO>> extends Object implements ObjectNamerOps<TO>
Default implementation of an object namer. The default implementation is a bare-bones implementation suitable for simple applications. The type parameter TO provides the type of a subclass of DefaultNamedObject that is the common superclass of all named objects recognized by this object namer. Unlike object namers generated by the annotation ObjectNamer, this object namer implicitly extends java.lang.Object and has no other superclass.

The default implementation avoids the need to use an annotation processor to generate code.

As an example, to create an object namer, a named object, and a factory for the named object, one would define the following classes:


  abstract public class OurNamedObject
        extends DefaultNamedObject<OurNamedObject>
  {
       ...
       protected OurNamedObject(OurObjectNamer namer,
                                String name,
                                boolean intern)
       {
          super(namer, name, intern);
       }
  }

  public class OurObjectNamer
       extends DefaultObjectNamer<OurNamedObject>
  {
       ...
       public OurObjectNamer() {
           super(OurNamedObject.class);
       }
  }

  abstract public class OurObjectFactory<OBJ extends OurNamedObject>
       extends DefaultNOFactory<OurObjectNamer,OurNamedObject,OBJ>
  {
       ...
       protected OurObjectFactory(OurObjectNamer namer) {
          super(namer);
       }
  }
 
Additional code (indicated by an ellipsis) is, of course, necessary to do anything useful.
  • Constructor Details

    • DefaultObjectNamer

      public DefaultObjectNamer(Class<TO> clazz)
      Constructor. For technical reasons related to type erasure, an argument providing the same class as the one used for the type parameter TO must be passed to the constructor explicitly.
      Parameters:
      clazz - the class of the type named by the type parameter TO
  • Method Details

    • getNamedObjectClass

      public Class<TO> getNamedObjectClass()
      Description copied from interface: ObjectNamerOps
      Get the class common to all named objects.
      Specified by:
      getNamedObjectClass in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Returns:
      the class
    • addObjectNamer

      public final void addObjectNamer(ObjectNamerOps<TO> altNamer)
      Add an alternative object namer for use by getObject methods.
      Specified by:
      addObjectNamer in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      altNamer - the alternative object namer
    • checkAltList

      public boolean checkAltList(ObjectNamerOps<TO> altNamer)
      Determine if the argument is equal to this object namer or a member of this object namer's object-namer list. This test is recursive. It is specified in this interface for technical reasons, and is not intended to be called otherwise. It is called by addObjectNamer to ensure that a depth-first search will terminate.
      Specified by:
      checkAltList in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      altNamer - the object namer to test
      Returns:
      true if he argument is equal to this object namer or a member of this object namer's object-namer list, tested recursively; false otherwise
    • getObject

      public TO getObject(String name)
      Get an object from the object namer's tables.
      Specified by:
      getObject in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      name - the name of the object
      Returns:
      the object corresponding to the name provided
    • getObjectNames

      public Set<String> getObjectNames()
      Get all the object names from a object namer's tables.
      Specified by:
      getObjectNames in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Returns:
      a set containing the names of all objects interned in the object namer's tables
    • getObjectNames

      public Set<String> getObjectNames(Class<?> clazz)
      Get a set of object names from a object namer's tables. The objects are those for which either their classes or one of their superclasses match the class passed as an argument.
      Specified by:
      getObjectNames in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      clazz - the class of the objects to get.
      Returns:
      a set containing the names of all objects interned in the object namer's tables such that the class of that object or the class of a subclass of that object matches the clazz argument
    • getObject

      public <T> T getObject(String name, Class<T> clazz)
      Get a named object with a particular name, provided that the object is a subclass of a specific class.
      Specified by:
      getObject in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Type Parameters:
      T - the type specified by the argument clazz
      Parameters:
      name - the name of the object
      clazz - the class of the object
      Returns:
      the object or null if the object cannot be found
    • getObjects

      public Collection<TO> getObjects()
      Get a collection of all the interned named objects associated with an object namer.
      Specified by:
      getObjects in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Returns:
      an unmodifiable collection of the objects interned in the object namer's tables.
    • getObjects

      public <T> Collection<T> getObjects(Class<T> clazz)
      Get a collection of objects with a class constraint from a object namer's tables. The objects are those for which either their classes or one of their superclasses match the class passed as an argument.
      Specified by:
      getObjects in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Type Parameters:
      T - the type specified by the argument clazz
      Parameters:
      clazz - the class of the objects to get.
      Returns:
      an unmodifiable collection containing all interned objects whose class or one of its superclasses matches the class clazz passed as the method's argument
    • getObjects

      public Collection<TO> getObjects(String className)
      Get a collection of objects with a class constraint, expressed as a class name, from a object namer's tables. The objects are those for which either their classes or one of their superclasses match the class passed as an argument.

      Note: this method is provided because script engines do not provide a standard way of denoting a Java class (the class not an instance of it). The collection it returns is a collection of the common named object type. Except for scripting languages that do not provide compile-time type checking, one should nearly always use getObjects(Class).

      Parameters:
      className - the fully qualified class name of the objects to get.
      Returns:
      an unmodifiable collection containing all interned objects whose class or one of its superclasses matches the class clazz passed as the method's argument; an empty (and unmodifiable) collection if no objects can be found
    • configureFactorySupported

      public boolean configureFactorySupported()
      Determine if the configureFactory method is supported. The default implementation always returns false, and subclasses that implement configureFactory should override this method.
      Specified by:
      configureFactorySupported in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Returns:
      true if configure() is supported; false otherwise
    • configureFactory

      public void configureFactory(NamedObjectFactory factory, Object scriptObject) throws UnsupportedOperationException
      Configure a factory. This is an optional operation. The default behavior is that the object namer handles the operation and throws an UnsupportedOperationException if it cannot. The default behavior is to throw an UnsupportedOperationException. A subclass should override this method and the method configureFactorySupported if the subclass provides support for scripting and the ability to configure objects in this way.
      Specified by:
      configureFactory in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      factory - the factory to configure
      scriptObject - an object in a scripting language representing a specification for how this factory should be configured
      Throws:
      UnsupportedOperationException - the factory cannot be configured using a script object
      IllegalArgumentException - the scriptObject is ill formed
    • createFactory

      public NamedObjectFactory createFactory(String varName, String packageName, String className) throws UnsupportedOperationException, IllegalArgumentException
      Create a factory and store it using a scripting-language variable.. This method just throws an UnsupportedOperationException as this class is not an instance of ScriptingContext. in case a user of this method tries to assign the value it returns.
      Specified by:
      createFactory in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      varName - the name of a scripting-language variable
      packageName - the name of the package (null or an empty string for the unnamed package)
      className - the class name of a factory, excluding the package name
      Returns:
      the factory that was created
      Throws:
      IllegalArgumentException - the factory is not listed in a META-INF/services/org.bzdev.NamedObjectFactory resource or the class name does not refer to subclass of NamedObjectFactory
      UnsupportedOperationException - this object namer does not support scripting;
      IllegalArgumentException - the factory is not listed in a META-INF/services/org.bzdev.NamedObjectFactory resource or the class name does not refer to subclass of NamedObjectFactory
    • createFactory

      public NamedObjectFactory createFactory(String className) throws IllegalArgumentException
      Create a factory. The factory must have a single-argument constructor that takes the class name as its argument
      Specified by:
      createFactory in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      className - the fully-qualified class name of a factory.
      Returns:
      a new factory
      Throws:
      IllegalArgumentException - the factory is not listed in a META-INF/services/org.bzdev.NamedObjectFactory resource or the class name does not refer to subclass of NamedObjectFactory
    • createFactory

      public NamedObjectFactory createFactory(Class clazz) throws IllegalArgumentException
      Create a factory given a class. The factory must have a single-argument constructor that takes its object namer as its argument.
      Specified by:
      createFactory in interface ObjectNamerOps<TO extends DefaultNamedObject<TO>>
      Parameters:
      clazz - the factory's class.
      Returns:
      a new factory
      Throws:
      IllegalArgumentException - the factory is not listed in a META-INF/services/org.bzdev.NamedObjectFactory resource or the class name does not refer to subclass of NamedObjectFactory