Package uk.ac.starlink.util

# Class ObjectFactory<T>

java.lang.Object
uk.ac.starlink.util.ObjectFactory<T>

public class ObjectFactory<T> extends Object
Manages dynamic creation of objects from a known set of classes. An ObjectFactory keeps a list of classes with associated nicknames; the idea is that you can obtain an instance of a given class by supplying the nickname in question. Instead of a nickname you can use the fully qualified classname, whether or not it has previously been registered. Any class registered must be a subclass of the superclass specified when this factory is constructed, and must have a no-arg constructor.

Since:
  10 Aug 2005
Author:
  Mark Taylor

## Constructor Summary

Constructors
Constructor
Description
`ObjectFactory(Class<T> clazz)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`T`
`createObject(String spec)`
Constructs and returns an object from one of the classes registered with this factory.
`Class<T>`
`getFactoryClass()`
Returns the class of which any object created by this factory is guaranteed to be an instance.
`String`
`getNickName(Class<? extends T> clazz)`
Returns the nickname corresponding to the no-arg constructor of the given class.
`String[]`
`getNickNames()`
Returns a list of the nicknames which have been registered.
`boolean`
`isRegistered(String name)`
Indicates whether this factory knows about a given name.
`void`
`register(String nickName, String className)`
Registers a class with its nickname.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ObjectFactory

public ObjectFactory(Class<T> clazz)
Constructor.

Parameters:
  `clazz` - type which must be a supertype of any class registered with this factory

## Method Details

### getFactoryClass

public Class<T> getFactoryClass()
Returns the class of which any object created by this factory is guaranteed to be an instance.

Returns:
  clazz

### register

public void register(String nickName, String className)
Registers a class with its nickname.

Parameters:
  `nickName` - nickname
  `className` - fully-qualified class name

### getNickNames

public String[] getNickNames()
Returns a list of the nicknames which have been registered.

Returns:
  nickname array

### isRegistered

public boolean isRegistered(String name)
Indicates whether this factory knows about a given name. This may either be a registered nickname or a fully qualified classname for a class which is a subclass of this factory's produced class.

Parameters:
  `name` - name
Returns:
  true iff `name` can sensibly be passed to `createObject(java.lang.String)`

### createObject

public T createObject(String spec) throws LoadException
Constructs and returns an object from one of the classes registered with this factory. If construction fails because the required class is not on the classpath or there is some error in class initialization, a LoadException is thrown. If the class is of the wrong sort (has no no-arg constructor, is not a subtype of this factory's supertype) a RuntimeException will be thrown.

Parameters:
  `spec` - classname/nickname of class to instantiate, followed by optional config text
Throws:
  `LoadException` - if the load fails for unsurprising reasons
See Also:
  - `BeanConfig`

### getNickName

public String getNickName(Class<? extends T> clazz)
Returns the nickname corresponding to the no-arg constructor of the given class.

Parameters:
  `clazz` - class to be constructed by this factory
Returns:
  nickname associated with no-arg constructor of supplied class, or null