Package ball.util
Class Factory<T>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.TreeMap<Class<?>[],Member>
-
- ball.util.Factory<T>
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Map<Class<?>[],Member>
,NavigableMap<Class<?>[],Member>
,SortedMap<Class<?>[],Member>
public class Factory<T> extends TreeMap<Class<?>[],Member>
Factory
base class.- Author:
- Allen D. Ball
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
apply(Member member, Object... arguments)
Method to apply a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.Member
get(Object key)
Object
getFactory()
Method to get the underlying factoryObject
.Member
getFactoryMethod(Class<?>... parameters)
Method to get a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.T
getInstance(Class<?>[] parameters, Object... arguments)
Method to get anObject
instance.T
getInstance(Object... arguments)
Method to get anObject
instance.Class<? extends T>
getType()
boolean
hasFactoryMethodFor(Class<?>... parameters)
Method to determine if there is a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.protected static boolean
isApplicable(Class<?>[] parameters, Class<?>... arguments)
Method to determine if an argument of the specified types may be applied to a method or constructor with the specified parameters.protected static Class<?>[]
typesOf(Object... arguments)
Convenience method to get the types of an argument array.-
Methods inherited from class java.util.TreeMap
ceilingEntry, ceilingKey, clear, clone, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, forEach, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, put, putAll, remove, replace, replace, replaceAll, size, subMap, subMap, tailMap, tailMap, values
-
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, getOrDefault, hashCode, isEmpty, merge, putIfAbsent, remove
-
-
-
-
Constructor Detail
-
Factory
@ConstructorProperties("type") public Factory(Class<? extends T> type)
Sole public constructor.- Parameters:
type
- TheClass
ofObject
thisFactory
will produce.- Throws:
NullPointerException
- Iftype
isnull
.
-
Factory
@ConstructorProperties({"type","factory"}) protected Factory(Class<? extends T> type, Object factory)
Construct aFactory
by wrapping a factory instance.- Parameters:
type
- TheClass
ofObject
thisFactory
will produce.factory
- AnObject
factory for thistype
(may benull
).- Throws:
NullPointerException
- Iftype
isnull
.
-
-
Method Detail
-
getFactory
public Object getFactory()
Method to get the underlying factoryObject
.- Returns:
- The underlying factory
Object
ornull
if there is none.
-
getInstance
public T getInstance(Class<?>[] parameters, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException
Method to get anObject
instance. This method will first attempt to find and invoke a static factory method. If no factory method is found, it will then attempt to construct a new instance.- Parameters:
parameters
- The parameter types to use to search for theObject
static factory method or constructor.arguments
- The arguments to theObject
static factory method or constructor.- Returns:
- The
Object
instance. - Throws:
IllegalAccessException
- If the specifiedConstructor
orMethod
enforces Java language access control and the underlyingConstructor
orMethod
is inaccessible.IllegalArgumentException
- If the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if thisConstructor
orMethod
pertains to anEnum
type.InstantiationException
- If the underlyingConstructor
orMethod
represents an abstractClass
.InvocationTargetException
- If the underlyingConstructor
orMethod
fails for some reason.NoSuchMethodException
- If the specifiedConstructor
orMethod
does not exist.
-
getInstance
public T getInstance(Object... arguments) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException
Method to get anObject
instance. This method will first attempt to find and invoke a static factory method. If no factory method is found, it will then attempt to construct a new instance.- Parameters:
arguments
- The arguments to theObject
static factory method or constructor.- Returns:
- The
Object
instance. - Throws:
IllegalAccessException
- If the specifiedConstructor
orMethod
enforces Java language access control and the underlyingConstructor
orMethod
is inaccessible.IllegalArgumentException
- If the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if thisConstructor
orMethod
pertains to anEnum
type.InstantiationException
- If the underlyingConstructor
orMethod
represents an abstractClass
.InvocationTargetException
- If the underlyingConstructor
orMethod
fails for some reason.NoSuchMethodException
- If the specifiedConstructor
orMethod
does not exist.
-
hasFactoryMethodFor
public boolean hasFactoryMethodFor(Class<?>... parameters)
Method to determine if there is a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.- Parameters:
parameters
- TheConstructor
orMethod
parameter list.- Returns:
true
if there is such aMember
;false
otherwise.
-
getFactoryMethod
public Member getFactoryMethod(Class<?>... parameters) throws NoSuchMethodException
Method to get a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.- Parameters:
parameters
- TheConstructor
orMethod
parameter list.- Returns:
- The factory
Member
. - Throws:
NoSuchMethodException
- If the specifiedConstructor
orMethod
does not exist.
-
apply
public T apply(Member member, Object... arguments) throws IllegalAccessException, InstantiationException, InvocationTargetException
Method to apply a factoryMember
(factoryMethod
, staticMethod
, orConstructor
) to manufacture or get anObject
.- Parameters:
member
- TheMember
(Constructor
or staticMethod
) to invoke.arguments
- The array of arguments to apply.- Returns:
- The
Object
instance. - Throws:
IllegalAccessException
- If the specifiedConstructor
orMethod
enforces Java language access control and the underlyingConstructor
orMethod
is inaccessible.InstantiationException
- If the underlyingConstructor
orMethod
represents an abstractClass
.InvocationTargetException
- If the underlyingConstructor
orMethod
fails for some reason.
-
typesOf
protected static Class<?>[] typesOf(Object... arguments)
Convenience method to get the types of an argument array.- Parameters:
arguments
- The argument array.- Returns:
- An array of types (
Class
s).
-
isApplicable
protected static boolean isApplicable(Class<?>[] parameters, Class<?>... arguments)
Method to determine if an argument of the specified types may be applied to a method or constructor with the specified parameters.- Parameters:
parameters
- The parameter types.arguments
- The argument types.- Returns:
true
if the length of the argument array is the same as the length of the parameter array and each parameter is assignable from its corresponding argument;false
otherwise.- See Also:
Class.isAssignableFrom(Class)
-
-