Class AuthorityCodes
java.lang.Object
org.apache.sis.internal.util.AbstractMap<String,String>
org.apache.sis.referencing.factory.sql.AuthorityCodes
- All Implemented Interfaces:
Serializable
,Map<String,
String>
A map of EPSG authority codes as keys and object names as values.
This map requires a living connection to the EPSG database.
Serialization
Serialization of this class stores a copy of all authority codes. The serialization does not preserve any connection to the database.Garbage collection
This method does not implementAutoCloseable
because the same instance may be shared by many users,
since EPSGDataAccess.getAuthorityCodes(Class)
caches AuthorityCodes
instances. Furthermore, we can
not rely on the users closing AuthorityCodes
themselves because this is not part of the usual contract
for Java collection classes (we could document that recommendation in method Javadoc, but not every developers
read Javadoc). Relying on the garbage collector for disposing this resource is far from ideal, but alternatives
are not very convincing either (load the same codes many time, have the risk that users do not dispose resources,
have the risk to return to user an already closed AuthorityCodes
instance).- Since:
- 0.7
- Version:
- 1.2
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.sis.internal.util.AbstractMap
AbstractMap.EntryIterator<K,
V>, AbstractMap.IteratorAdapter<K, V>, AbstractMap.KeyIterator -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Index in thesql
andstatements
arrays.private IntegerList
A cache of integer codes.private final EPSGDataAccess
The factory which is the owner of this map.private final boolean
private static final int
Highest code value (inclusive) that thisAuthorityCodes
support during iterations.private static final int
Index in thesql
andstatements
arrays.private ResultSet
The result ofstatements[ALL]
, created only if requested.private final String[]
The SQL commands that thisAuthorityCodes
may need to execute.private final Statement[]
The JDBC statements for the SQL commands in thesql
array, created when first needed.(package private) final Class<?>
The interface of referencing objects for which this map contains the code. -
Constructor Summary
ConstructorsConstructorDescriptionAuthorityCodes
(Connection connection, TableInfo table, Class<?> type, EPSGDataAccess factory) Creates a new map of authority codes for the specified type. -
Method Summary
Modifier and TypeMethodDescription(package private) final CloseableReference
Creates a weak reference to this map.Returns an iterator over the entries.private BackingStoreException
factoryFailure
(SQLException exception) Invoked when a SQL statement cannot be executed, or the result retrieved.private boolean
filter
(int code) Returnstrue
if the specified code should be included in this map.Returns the object name associated to the given authority code, ornull
if none.private int
getCodeAt
(int index) Returns the code at the given index, or -1 if the index is out of bounds.boolean
isEmpty()
Returnstrue
if this map contains no element.int
size()
Counts the number of elements in this map.toString()
Returns a string representation of this map for debugging purpose.protected Object
Returns a serializable copy of this set.Methods inherited from class org.apache.sis.internal.util.AbstractMap
addKey, addValue, clear, containsKey, containsValue, entrySet, equals, getOrDefault, hashCode, keySet, put, putAll, remove, values
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
MAX_CODE
private static final int MAX_CODEHighest code value (inclusive) that thisAuthorityCodes
support during iterations. This is based on the upper value of the highest range of codes once used by EPSG.- See Also:
-
ALL
private static final int ALLIndex in thesql
andstatements
arrays.- See Also:
-
ONE
private static final int ONEIndex in thesql
andstatements
arrays.- See Also:
-
factory
The factory which is the owner of this map. One purpose of this field is to prevent garbage collection of that factory as long as this map is in use. This is required becauseCloseableReference.dispose()
closes the JDBC connections. -
type
The interface of referencing objects for which this map contains the code. May be a super-interface of the type specified to the constructor. -
isProjection
private final transient boolean isProjection -
sql
The SQL commands that thisAuthorityCodes
may need to execute. In this array:sql[ALL]
is a statement for querying all codes.sql[ONE]
is a statement for querying a single code. This statement is similar tosql[ALL]
with the addition of aWHERE
clause.
-
statements
The JDBC statements for the SQL commands in thesql
array, created when first needed. All usages of those statements shall be synchronized on the factory. This array will also be stored inCloseableReference
for closing the statements when the garbage collector detected thatAuthorityCodes
is no longer in use. -
results
The result ofstatements[ALL]
, created only if requested. The codes will be queried at most once and cached in thecodes
list.Note that if this result set is not closed explicitly, it will be closed implicitly when
statements[ALL]
will be closed. This is because JDBC specification said that closing a statement also close its result set. -
codes
A cache of integer codes. Created only if the user wants to iterate over all codes or asked for the map size.
-
-
Constructor Details
-
AuthorityCodes
AuthorityCodes(Connection connection, TableInfo table, Class<?> type, EPSGDataAccess factory) throws SQLException Creates a new map of authority codes for the specified type.- Parameters:
connection
- the connection to the EPSG database.table
- the table to query.type
- the type to query.factory
- the factory originator.- Throws:
SQLException
-
-
Method Details
-
createReference
Creates a weak reference to this map. That reference will also be in charge of closing the JDBC statements when the garbage collector determined that thisAuthorityCodes
instance is no longer in use. See class Javadoc for more information. -
filter
Returnstrue
if the specified code should be included in this map.- Throws:
SQLException
-
getCodeAt
Returns the code at the given index, or -1 if the index is out of bounds.- Parameters:
index
- index of the code to fetch.- Returns:
- the code at the given index, or -1 if out of bounds.
- Throws:
SQLException
- if an error occurred while querying the database.
-
isEmpty
public boolean isEmpty()Returnstrue
if this map contains no element. This method fetches at most one row instead of counting all rows. -
size
public int size()Counts the number of elements in this map. -
get
Returns the object name associated to the given authority code, ornull
if none. If there is no name for the type of object being queried, then this method returns the code itself. -
entryIterator
Returns an iterator over the entries.- Specified by:
entryIterator
in classAbstractMap<String,
String> - Returns:
- an iterator over the entries in this map, or
null
.
-
toString
Returns a string representation of this map for debugging purpose. This method does not let the default implementation format all entry, since it would be a costly operation.- Overrides:
toString
in classAbstractMap<String,
String> - Returns:
- a string representation of this map.
-
factoryFailure
Invoked when a SQL statement cannot be executed, or the result retrieved. -
writeReplace
Returns a serializable copy of this set. This method is invoked automatically during serialization. The serialized map of authority codes is disconnected from the underlying database.- Throws:
ObjectStreamException
-