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>
Factorybase 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 Tapply(Member member, Object... arguments)Method to apply a factoryMember(factoryMethod, staticMethod, orConstructor) to manufacture or get anObject.Memberget(Object key)ObjectgetFactory()Method to get the underlying factoryObject.MembergetFactoryMethod(Class<?>... parameters)Method to get a factoryMember(factoryMethod, staticMethod, orConstructor) to manufacture or get anObject.TgetInstance(Class<?>[] parameters, Object... arguments)Method to get anObjectinstance.TgetInstance(Object... arguments)Method to get anObjectinstance.Class<? extends T>getType()booleanhasFactoryMethodFor(Class<?>... parameters)Method to determine if there is a factoryMember(factoryMethod, staticMethod, orConstructor) to manufacture or get anObject.protected static booleanisApplicable(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- TheClassofObjectthisFactorywill produce.- Throws:
NullPointerException- Iftypeisnull.
-
Factory
@ConstructorProperties({"type","factory"}) protected Factory(Class<? extends T> type, Object factory)
Construct aFactoryby wrapping a factory instance.- Parameters:
type- TheClassofObjectthisFactorywill produce.factory- AnObjectfactory for thistype(may benull).- Throws:
NullPointerException- Iftypeisnull.
-
-
Method Detail
-
getFactory
public Object getFactory()
Method to get the underlying factoryObject.- Returns:
- The underlying factory
Objectornullif there is none.
-
getInstance
public T getInstance(Class<?>[] parameters, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException
Method to get anObjectinstance. 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 theObjectstatic factory method or constructor.arguments- The arguments to theObjectstatic factory method or constructor.- Returns:
- The
Objectinstance. - Throws:
IllegalAccessException- If the specifiedConstructororMethodenforces Java language access control and the underlyingConstructororMethodis 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 thisConstructororMethodpertains to anEnumtype.InstantiationException- If the underlyingConstructororMethodrepresents an abstractClass.InvocationTargetException- If the underlyingConstructororMethodfails for some reason.NoSuchMethodException- If the specifiedConstructororMethoddoes not exist.
-
getInstance
public T getInstance(Object... arguments) throws IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException, NoSuchMethodException
Method to get anObjectinstance. 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 theObjectstatic factory method or constructor.- Returns:
- The
Objectinstance. - Throws:
IllegalAccessException- If the specifiedConstructororMethodenforces Java language access control and the underlyingConstructororMethodis 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 thisConstructororMethodpertains to anEnumtype.InstantiationException- If the underlyingConstructororMethodrepresents an abstractClass.InvocationTargetException- If the underlyingConstructororMethodfails for some reason.NoSuchMethodException- If the specifiedConstructororMethoddoes 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- TheConstructororMethodparameter list.- Returns:
trueif there is such aMember;falseotherwise.
-
getFactoryMethod
public Member getFactoryMethod(Class<?>... parameters) throws NoSuchMethodException
Method to get a factoryMember(factoryMethod, staticMethod, orConstructor) to manufacture or get anObject.- Parameters:
parameters- TheConstructororMethodparameter list.- Returns:
- The factory
Member. - Throws:
NoSuchMethodException- If the specifiedConstructororMethoddoes 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(Constructoror staticMethod) to invoke.arguments- The array of arguments to apply.- Returns:
- The
Objectinstance. - Throws:
IllegalAccessException- If the specifiedConstructororMethodenforces Java language access control and the underlyingConstructororMethodis inaccessible.InstantiationException- If the underlyingConstructororMethodrepresents an abstractClass.InvocationTargetException- If the underlyingConstructororMethodfails 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 (
Classs).
-
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:
trueif 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;falseotherwise.- See Also:
Class.isAssignableFrom(Class)
-
-