Module org.bzdev.obnaming
Package org.bzdev.obnaming
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:
Additional code (indicated by an ellipsis) is, of course, necessary to do anything useful.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); } }
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void
addObjectNamer
(ObjectNamerOps<TO> altNamer) Add an alternative object namer for use by getObject methods.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.void
configureFactory
(NamedObjectFactory factory, Object scriptObject) Configure a factory.boolean
Determine if the configureFactory method is supported.createFactory
(Class clazz) Create a factory given a class.createFactory
(String className) Create a factory.createFactory
(String varName, String packageName, String className) Create a factory and store it using a scripting-language variable..Get the class common to all named objects.Get an object from the object namer's tables.<T> T
Get a named object with a particular name, provided that the object is a subclass of a specific class.Get all the object names from a object namer's tables.getObjectNames
(Class<?> clazz) Get a set of object names from a object namer's tables.Get a collection of all the interned named objects associated with an object namer.<T> Collection<T>
getObjects
(Class<T> clazz) Get a collection of objects with a class constraint from a object namer's tables.getObjects
(String className) Get a collection of objects with a class constraint, expressed as a class name, from a object namer's tables.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.bzdev.obnaming.ObjectNamerOps
createFactories
-
Constructor Details
-
DefaultObjectNamer
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
Description copied from interface:ObjectNamerOps
Get the class common to all named objects.- Specified by:
getNamedObjectClass
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Returns:
- the class
-
addObjectNamer
Add an alternative object namer for use by getObject methods.- Specified by:
addObjectNamer
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Parameters:
altNamer
- the alternative object namer
-
checkAltList
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 interfaceObjectNamerOps<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
Get an object from the object namer's tables.- Specified by:
getObject
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Parameters:
name
- the name of the object- Returns:
- the object corresponding to the name provided
-
getObjectNames
Get all the object names from a object namer's tables.- Specified by:
getObjectNames
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Returns:
- a set containing the names of all objects interned in the object namer's tables
-
getObjectNames
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 interfaceObjectNamerOps<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
Get a named object with a particular name, provided that the object is a subclass of a specific class.- Specified by:
getObject
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Type Parameters:
T
- the type specified by the argument clazz- Parameters:
name
- the name of the objectclazz
- the class of the object- Returns:
- the object or null if the object cannot be found
-
getObjects
Get a collection of all the interned named objects associated with an object namer.- Specified by:
getObjects
in interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Returns:
- an unmodifiable collection of the objects interned in the object namer's tables.
-
getObjects
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 interfaceObjectNamerOps<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
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 interfaceObjectNamerOps<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 interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Parameters:
factory
- the factory to configurescriptObject
- 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 objectIllegalArgumentException
- 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 interfaceObjectNamerOps<TO extends DefaultNamedObject<TO>>
- Parameters:
varName
- the name of a scripting-language variablepackageName
- 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 NamedObjectFactoryUnsupportedOperationException
- 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
Create a factory. The factory must have a single-argument constructor that takes the class name as its argument- Specified by:
createFactory
in interfaceObjectNamerOps<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
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 interfaceObjectNamerOps<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
-