Class IdentifiedObjectSet<T extends IdentifiedObject>
- Object
-
- AbstractCollection<E>
-
- AbstractSet<T>
-
- IdentifiedObjectSet<T>
-
- Type Parameters:
T- the type of objects to be included in this set.
- All Implemented Interfaces:
Iterable<T>,Collection<T>,Set<T>,CheckedContainer<T>,Localized
public class IdentifiedObjectSet<T extends IdentifiedObject> extends AbstractSet<T> implements CheckedContainer<T>, Localized
A lazy set ofIdentifiedObjectinstances created from their authority codes only when first needed. This set delegatesIdentifiedObjectcreation to the most appropriatecreateFoo(String)method of theAuthorityFactorygiven at construction time.Elements can be added to this collection with calls to
addAuthorityCode(String)for deferred object creation, or toadd(IdentifiedObject)for objects that are already instantiated. This collection can not contain twoIdentifiedObjectinstances having the same identifier. However the identifiers used by this class can be controlled by overridinggetAuthorityCode(IdentifiedObject).Iterations over elements in this collection preserve insertion order.
PurposeIdentifiedObjectSetcan be used as the set returned by implementations of theGeodeticAuthorityFactory.createFromCoordinateReferenceSystemCodes(String, String)method. Deferred creation can have great performance impact since some set may contain as much as 40 entries (e.g. transformations from "ED50" (EPSG:4230) to "WGS 84" (EPSG:4326)) while some users only want to look for the first entry.Exception handlingIf the underlying factory failed to creates an object because of an unsupported operation method (NoSuchIdentifierException), the exception is logged atLevel.WARNINGand the iteration continue. If the operation creation failed for any other kind of reason (FactoryException), then the exception is re-thrown as an uncheckedBackingStoreException. This default behavior can be changed by overriding theisRecoverableFailure(FactoryException)method.Thread safetyThis class is thread-safe is the underlying factory is also thread-safe. However, implementers are encouraged to wrap in unmodifiable set if they intent to cacheIdentifiedObjectSetinstances.- Since:
- 0.7
Defined in the
sis-referencingmodule
-
-
Field Summary
Fields Modifier and Type Field Description protected AuthorityFactoryfactoryThe factory to use for creatingIdentifiedObjects when first needed.
-
Constructor Summary
Constructors Constructor Description IdentifiedObjectSet(AuthorityFactory factory, Class<T> type)Creates an initially empty set.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T object)Ensures that this collection contains the specified object.voidaddAuthorityCode(String code)Ensures that this collection contains an object for the specified authority code.voidclear()Removes all of the elements from this collection.booleancontains(Object object)Returnstrueif this collection contains the specifiedIdentifiedObject.protected TcreateObject(String code)Creates an object for the specified authority code.protected StringgetAuthorityCode(T object)Returns the identifier for the specified object.String[]getAuthorityCodes()Returns the authority codes of allIdentifiedObjects contained in this collection, in insertion order.Class<T>getElementType()Returns the type ofIdentifiedObjectincluded in this set.LocalegetLocale()Returns the locale to use for error messages and warnings.protected booleanisRecoverableFailure(FactoryException exception)Returnstrueif the specified exception should be handled as a recoverable failure.Iterator<T>iterator()Returns an iterator over the objects in this set.booleanremove(Object object)Removes the specifiedIdentifiedObjectfrom this collection, if it is present.booleanremoveAll(Collection<?> collection)Removes from this collection all of its elements that are contained in the specified collection.voidresolve(int n)Ensures that the n first objects in this set are created.voidsetAuthorityCodes(String... codes)Sets the content of this collection to the object identified by the given codes.intsize()Returns the number of objects available in this set.-
Methods inherited from class AbstractSet
equals, hashCode
-
Methods inherited from class AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray, toString
-
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface Set
addAll, containsAll, isEmpty, retainAll, spliterator, toArray, toArray
-
-
-
-
Field Detail
-
factory
protected final AuthorityFactory factory
The factory to use for creatingIdentifiedObjects when first needed. This is the authority factory given at construction time.
-
-
Constructor Detail
-
IdentifiedObjectSet
public IdentifiedObjectSet(AuthorityFactory factory, Class<T> type)
Creates an initially empty set. The set can be populated after construction by calls toaddAuthorityCode(String)for deferredIdentifiedObjectcreation, or toadd(IdentifiedObject)for already instantiated objects.- Parameters:
factory- the factory to use for deferredIdentifiedObjectinstances creation.type- the type of objects included in this set.
-
-
Method Detail
-
getLocale
public Locale getLocale()
Returns the locale to use for error messages and warnings. The default implementation inherits thefactorylocale, if any.- Specified by:
getLocalein interfaceLocalized- Returns:
- the locale, or
nullif not explicitly defined.
-
getElementType
public Class<T> getElementType()
Returns the type ofIdentifiedObjectincluded in this set.- Specified by:
getElementTypein interfaceCheckedContainer<T extends IdentifiedObject>- Returns:
- the type of
IdentifiedObjectincluded in this set.
-
clear
public void clear()
Removes all of the elements from this collection.- Specified by:
clearin interfaceCollection<T extends IdentifiedObject>- Specified by:
clearin interfaceSet<T extends IdentifiedObject>- Overrides:
clearin classAbstractCollection<T extends IdentifiedObject>
-
size
public int size()
Returns the number of objects available in this set. Note that this number may decrease during the iteration process if the creation of someIdentifiedObjects failed.- Specified by:
sizein interfaceCollection<T extends IdentifiedObject>- Specified by:
sizein interfaceSet<T extends IdentifiedObject>- Specified by:
sizein classAbstractCollection<T extends IdentifiedObject>- Returns:
- the number of objects available in this set.
-
getAuthorityCodes
public String[] getAuthorityCodes()
Returns the authority codes of allIdentifiedObjects contained in this collection, in insertion order. This method does not trig the creation of any object.- Returns:
- the authority codes in iteration order.
-
setAuthorityCodes
public void setAuthorityCodes(String... codes)
Sets the content of this collection to the object identified by the given codes. For any code in the given sequence, this method will preserve the correspondingIdentifiedObjectinstance if it was already created. Otherwise objects will be created only when first needed.Purpose: this method is typically used together withgetAuthorityCodes()for altering the iteration order on the basis of authority codes. If the specifiedcodessequence contains the same elements than the ones in the array returned bygetAuthorityCodes()but in a different order, then this method just sets the new ordering.- Parameters:
codes- the authority codes of identified objects to store in this set.- See Also:
addAuthorityCode(String)
-
addAuthorityCode
public void addAuthorityCode(String code)
Ensures that this collection contains an object for the specified authority code. If this collection does not contain any element for the given code, then this method will instantiate anIdentifiedObjectfor the given code only when first needed. Otherwise this collection is unchanged.- Parameters:
code- the code authority code of theIdentifiedObjectto include in this set.
-
add
public boolean add(T object)
Ensures that this collection contains the specified object. This collection does not allow multiple objects for the same authority code. If this collection already contains an object using the same authority code than the given object, then the old object is replaced by the new one regardless of whether the objects themselves are equal or not.- Specified by:
addin interfaceCollection<T extends IdentifiedObject>- Specified by:
addin interfaceSet<T extends IdentifiedObject>- Overrides:
addin classAbstractCollection<T extends IdentifiedObject>- Parameters:
object- the object to add to the set.- Returns:
trueif this set changed as a result of this call.- See Also:
getAuthorityCode(IdentifiedObject)
-
contains
public boolean contains(Object object)
Returnstrueif this collection contains the specifiedIdentifiedObject.- Specified by:
containsin interfaceCollection<T extends IdentifiedObject>- Specified by:
containsin interfaceSet<T extends IdentifiedObject>- Overrides:
containsin classAbstractCollection<T extends IdentifiedObject>- Parameters:
object- theIdentifiedObjectto test for presence in this set.- Returns:
trueif the given object is presents in this set.
-
remove
public boolean remove(Object object)
Removes the specifiedIdentifiedObjectfrom this collection, if it is present.- Specified by:
removein interfaceCollection<T extends IdentifiedObject>- Specified by:
removein interfaceSet<T extends IdentifiedObject>- Overrides:
removein classAbstractCollection<T extends IdentifiedObject>- Parameters:
object- theIdentifiedObjectto remove from this set.- Returns:
trueif this set changed as a result of this call.
-
removeAll
public boolean removeAll(Collection<?> collection)
Removes from this collection all of its elements that are contained in the specified collection.- Specified by:
removeAllin interfaceCollection<T extends IdentifiedObject>- Specified by:
removeAllin interfaceSet<T extends IdentifiedObject>- Overrides:
removeAllin classAbstractSet<T extends IdentifiedObject>- Parameters:
collection- theIdentifiedObjects to remove from this set.- Returns:
trueif this set changed as a result of this call.
-
iterator
public Iterator<T> iterator() throws BackingStoreException
Returns an iterator over the objects in this set. If the iteration encounter any kind ofFactoryExceptionother thanNoSuchIdentifierException, then the exception will be re-thrown as an uncheckedBackingStoreException.This iterator is not thread safe – iteration should be done in a single thread. However the iterator is robust to concurrent changes in
IdentifiedObjectSetduring iteration.- Specified by:
iteratorin interfaceCollection<T extends IdentifiedObject>- Specified by:
iteratorin interfaceIterable<T extends IdentifiedObject>- Specified by:
iteratorin interfaceSet<T extends IdentifiedObject>- Specified by:
iteratorin classAbstractCollection<T extends IdentifiedObject>- Returns:
- an iterator over all
IdentifiedObjectinstances in this set, in insertion order. - Throws:
BackingStoreException- if an error occurred while creating the iterator.
-
resolve
public void resolve(int n) throws FactoryExceptionEnsures that the n first objects in this set are created. This method can be invoked for making sure that the underlying factory is really capable to create at least one object.FactoryException(except the ones accepted as recoverable failures) are thrown as if they were never wrapped intoBackingStoreException.- Parameters:
n- the number of object to resolve. If this number is equals or greater thansize(), then this method ensures that allIdentifiedObjectinstances in this collection are created.- Throws:
FactoryException- if an object creation failed.
-
getAuthorityCode
protected String getAuthorityCode(T object)
Returns the identifier for the specified object. The default implementation takes the first of the following identifier which is found:- An identifier allocated by the authority given by
factory.getAuthority(). - The first object identifier, regardless its authority.
- The first object name, regardless its authority.
- Parameters:
object- the object for which to get the authority code.- Returns:
- the authority code of the given identified object.
- An identifier allocated by the authority given by
-
createObject
protected T createObject(String code) throws FactoryException
Creates an object for the specified authority code. This method is invoked during the iteration process if an object was not already created.- Parameters:
code- the code for which to create the identified object.- Returns:
- the identified object created from the given code.
- Throws:
FactoryException- if the object creation failed.
-
isRecoverableFailure
protected boolean isRecoverableFailure(FactoryException exception)
Returnstrueif the specified exception should be handled as a recoverable failure. This method is invoked during the iteration process if the factory failed to create some objects. If this method returnstruefor the given exception, then the exception will be logged atLevel.WARNING. If this method returnsfalse, then the exception will be re-thrown as aBackingStoreException.The default implementation applies the following rules:
- If
NoSuchAuthorityCodeException, returnsfalsesince failure to find a code declared in the collection would be an inconsistency. Note that this exception is a subtype ofNoSuchIdentifierException, so it must be tested before the last case below. - If
NoSuchIdentifierException, returnstruesince this exception is caused by an attempt to create a parameterized transform for an unimplemented operation. - If
MissingFactoryResourceException, returnstrue. - Otherwise returns
false.
- Parameters:
exception- the exception that occurred while creating an object.- Returns:
trueif the given exception should be considered recoverable, orfalseif it should be considered fatal.
- If
-
-