public class AbstractFactory extends Object implements Factory, RegistrableFactory
createFoo
methods,
(they must be provided by subclasses), but provides two convenience features:
getImplementationHints()
.
When more than one factory implementation is registered for the same category (i.e. they implement
the same Factory
sub-interface), the actual instance to be used is selected according
their ordering and user-supplied hints. Hints have precedence. If more than one factory matches the hints (including the common
case where the user doesn't provide any hint at all), then ordering matter.
The ordering is unspecified for every pairs of factories with the same priority. This implies that the ordering is unspecified between all factories created with the default constructor, since they all have the same default priority level.
AbstractFactory
do not provides any facility for the first case.
Factories implementations shall inspect themselves all relevant hints supplied by the user, and
pass them to any dependencies. Do not use the hints
field for that; use
the hints provided by the user in the constructor. If all dependencies are created at
construction time (constructor injection), there is no need to keep user's hints
once the construction is finished.
The hints
field is for the second case only. Implementations shall copy in this field
only the user's hints that are know to be relevant to this factory. If a hint is relevant but the
user didn't specified any value, the hint key should be added to the hints
map anyway
with a null
value. Only direct dependencies shall be put in the hints
map.
Indirect dependencies (i.e. hints used by other factories used by this factory) will be inspected
automatically by FactoryRegistry
in a recursive way.
Note: The lack of constructor expecting a Map
argument is
intentional. This is in order to discourage blind-copy of all user-supplied hints to the hints
map.
Example: Lets two factories, A and B. Factory A need an instance of Factory B. Factory A can be implemented as below:
Code | Observations |
---|---|
class FactoryA extends AbstractFactory { FactoryB fb; FactoryA(Hints userHints) { fb = FactoryFinder.getFactoryB(userHints); hints.put(Hints.FACTORY_B, fb); } } |
|
Modifier and Type | Field and Description |
---|---|
protected Map<RenderingHints.Key,Object> |
hints
The implementation hints.
|
static int |
MAXIMUM_PRIORITY
The maximum priority for a factory, which is 100.
|
static int |
MINIMUM_PRIORITY
The minimum priority for a factory, which is 1.
|
static int |
NORMAL_PRIORITY
The default priority, which is 50.
|
protected int |
priority
The priority for this factory, as a number between
MINIMUM_PRIORITY and MAXIMUM_PRIORITY inclusive. |
Modifier | Constructor and Description |
---|---|
protected |
AbstractFactory()
Creates a new factory with the default priority.
|
protected |
AbstractFactory(int priority)
Constructs a factory with the specified priority.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
addImplementationHints(RenderingHints map)
Adds the specified hints to this factory hints.
|
boolean |
equals(Object object)
Compares this factory with the specified object for equality.
|
Map<RenderingHints.Key,?> |
getImplementationHints()
Returns an unmodifiable view of hints.
|
int |
getPriority()
Returns the priority for this factory, as a number between
MINIMUM_PRIORITY and
MAXIMUM_PRIORITY inclusive. |
int |
hashCode()
Returns a hash value for this factory.
|
void |
onDeregistration(FactoryRegistry registry,
Class category)
Called when this factory is removed from the given
category of the given registry . |
void |
onRegistration(FactoryRegistry registry,
Class<?> category)
Called when this factory is added to the given
category of the given registry . |
String |
toString()
Returns a string representation of this factory.
|
public static final int MINIMUM_PRIORITY
public static final int NORMAL_PRIORITY
public static final int MAXIMUM_PRIORITY
protected final int priority
MINIMUM_PRIORITY
and MAXIMUM_PRIORITY
inclusive. Priorities are used by FactoryRegistry
for selecting a
preferred factory when many are found for the same service.getPriority()
protected final Map<RenderingHints.Key,Object> hints
Note: This field is not an instance of Hints
because:
null
values, as of implementation hints contract.
protected AbstractFactory()
protected AbstractFactory(int priority)
priority
- The priority for this factory, as a number between MINIMUM_PRIORITY
and MAXIMUM_PRIORITY
inclusive.public int getPriority()
MINIMUM_PRIORITY
and
MAXIMUM_PRIORITY
inclusive. Priorities are used by FactoryRegistry
for
selecting a preferred factory when many are found for the same service. The default
implementation returns priority
with no change. Subclasses should override this
method if they want to return a higher or lower priority.protected boolean addImplementationHints(RenderingHints map)
hints.putAll(map)
when the map is an instance of
Hints
- the above was allowed in Java 4, but is no longuer allowed since Java 5 and
parameterized types.map
- The hints to add.true
if at least one value changed as a result of this call.public Map<RenderingHints.Key,?> getImplementationHints()
getImplementationHints
in interface Factory
public void onRegistration(FactoryRegistry registry, Class<?> category)
category
of the given registry
. The factory may already be registered under another category or categories.
This method is invoked automatically when this factory is registered as a plugin, and
should not be invoked directly by the user. The default implementation iterates through all
services under the same category that extends the AbstractFactory
class, and set the
ordering according the priority given at construction time.
onRegistration
in interface RegistrableFactory
registry
- A factory registry where this factory has been registered.category
- The registry category under which this object has been registered.MINIMUM_PRIORITY
,
MAXIMUM_PRIORITY
public void onDeregistration(FactoryRegistry registry, Class category)
category
of the given registry
. The object may still be registered under another category or categories.
This method is invoked automatically when this factory is no longer registered as a plugin, and should not be invoked directly by the user.
onDeregistration
in interface RegistrableFactory
registry
- A service registry from which this object is being (wholly or partially)
deregistered.category
- The registry category from which this object is being deregistered.public final int hashCode()
public final boolean equals(Object object)
true
if and only if:
The requirement for the exact same class is needed for consistency with the factory registry working, since at most one instance of a given class FactoryRegistry#getFactoryByClass) is allowed in a registry.
public String toString()
Copyright © 1996–2019 Geotools. All rights reserved.