ObjectType
- the type of the object contained in this collectionpublic class BaseConditionsObjectCollection<ObjectType extends ConditionsObject> extends Object implements ConditionsObjectCollection<ObjectType>
ConditionsObjectCollection
interface.Modifier and Type | Field and Description |
---|---|
private int |
collectionId
The collection ID.
|
private Connection |
connection
The database connection.
|
private Set<ObjectType> |
objects
The set of objects contained in the collection (no duplicate references allowed to the same object).
|
private TableMetaData |
tableMetaData
The collection's table meta data mapping it to the database.
|
Modifier | Constructor and Description |
---|---|
protected |
BaseConditionsObjectCollection()
No argument constructor; usable by sub-classes.
|
|
BaseConditionsObjectCollection(Connection connection,
TableMetaData tableMetaData)
Constructor with connection and meta data without field values.
|
|
BaseConditionsObjectCollection(Connection connection,
TableMetaData tableMetaData,
int collectionId)
Constructor which selects data into the collection using a collection ID.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(ObjectType object)
Add an object to the collection.
|
void |
addAll(ConditionsObjectCollection<ObjectType> collection)
Add all objects from a collection.
|
private String |
buildInsertStatement()
Build a SQL insert statement.
|
private void |
checkCollectionId(ObjectType object) |
void |
clear()
Clear the objects from this collection and reset its ID.
|
boolean |
contains(Object object)
Return
true if object is contained in this collection. |
void |
delete()
Delete the objects in the collection from database.
|
private boolean |
exists()
Return
true if collection exists in the database. |
ObjectType |
get(int index)
Get the object at the index.
|
int |
getCollectionId()
Get the collection ID.
|
private int |
getNextCollectionId()
Add a row for a new collection in the collections table and return the new collection ID assigned to it.
|
protected Set<ObjectType> |
getObjects()
Get the set of objects in this collection.
|
TableMetaData |
getTableMetaData()
Get the table meta data.
|
void |
insert()
Insert the objects from this collection into the database.
|
boolean |
isNew()
Return
true if the collection is new, e.g. |
Iterator<ObjectType> |
iterator()
Get an iterator for the collection.
|
void |
loadCsv(File file)
Load data from a CSV file.
|
boolean |
select(int collectionId)
Select objects into this collection by their collection ID in the database.
|
void |
setCollectionId(int collectionId)
Set the collection ID of this collection.
|
private void |
setConditionsObjectCollectionIds()
Iterate over the objects in the collection and set their collection IDs.
|
private void |
setConditionsObjectConnections()
Iterate over the objects in the collection and set their database connection reference.
|
void |
setConnection(Connection connection)
Set the database connection of the collection.
|
void |
setTableMetaData(TableMetaData tableMetaData)
Set the table meta data of the collection.
|
int |
size()
Get the size of the collection.
|
void |
sort(Comparator<ObjectType> comparator)
Sort the collection in place.
|
ConditionsObjectCollection<ObjectType> |
sorted(Comparator<ObjectType> comparator)
Return a sorted copy of this collection, leaving the original unchanged.
|
String |
toString()
Convert object to string.
|
boolean |
update()
Perform database updates on the objects in the collection.
|
void |
writeCsv(File file)
Write contents of this collection to a CSV file.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
private int collectionId
private Connection connection
private final Set<ObjectType extends ConditionsObject> objects
private TableMetaData tableMetaData
protected BaseConditionsObjectCollection()
public BaseConditionsObjectCollection(Connection connection, TableMetaData tableMetaData)
connection
- the database connectiontableMetaData
- the table meta dataSQLException
- if thereDatabaseObjectException
public BaseConditionsObjectCollection(Connection connection, TableMetaData tableMetaData, int collectionId) throws SQLException, DatabaseObjectException
connection
- the database connectiontableMetaData
- the table meta datacollectionId
- the collection IDSQLException
- if there is an error executing SQL queryDatabaseObjectException
- if there is an error relating to the conditions object APIpublic boolean add(ObjectType object) throws ConditionsObjectException
add
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
object
- the object to add to the collectiontrue
if object was added successfullyConditionsObjectException
- if there was an error adding the objectprivate void checkCollectionId(ObjectType object)
public void addAll(ConditionsObjectCollection<ObjectType> collection)
addAll
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
collection
- the collection with objects to addprivate String buildInsertStatement()
public void clear()
This has no effect on the underlying database values.
clear
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
public final boolean contains(Object object)
true
if object is contained in this collection.contains
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
object
- the object to checktrue
if object is contained in this collectionpublic final void delete() throws DatabaseObjectException, SQLException
delete
in interface DatabaseObject
ConditionsObjectException
- if there is an error deleting the objectSQLException
- if there was an error executing the queryDatabaseObjectException
private boolean exists() throws SQLException
true
if collection exists in the database.true
if collection exists in the databaseSQLException
- if there is a query errorpublic final ObjectType get(int index)
get
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
index
- the object indexIndexOutOfBoundsException
- if the index is invalidpublic final int getCollectionId()
getCollectionId
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
private int getNextCollectionId() throws SQLException, DatabaseObjectException
tableName
- the name of the tablecomment
- an optional comment about this new collectionSQLException
DatabaseObjectException
protected Set<ObjectType> getObjects()
public final TableMetaData getTableMetaData()
getTableMetaData
in interface DatabaseObject
TableMetaData
public final void insert() throws DatabaseObjectException, SQLException
The collection ID will be determined automatically if it is not already set.
insert
in interface DatabaseObject
ConditionsObjectException
- if there is an error inserting the objectsSQLException
- if there is a query errorDatabaseObjectException
public final boolean isNew()
true
if the collection is new, e.g. it is not in the database.isNew
in interface DatabaseObject
true
if the collection is newpublic final Iterator<ObjectType> iterator()
iterator
in interface Iterable<ObjectType extends ConditionsObject>
public void loadCsv(File file) throws IOException, FileNotFoundException, ConditionsObjectException
loadCsv
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
file
- the CSV fileIOException
- if there is an error closing the readerFileNotFoundException
- if the input file does not existConditionsObjectException
- if there is an error creating a conditions objectpublic final boolean select(int collectionId) throws SQLException, DatabaseObjectException
select
in interface DatabaseObject
collectionId
- the row IDtrue
if at least one object was selectedSQLException
- if there was a query errorDatabaseObjectException
- if there was an error selecting information into this objectpublic final void setCollectionId(int collectionId)
setCollectionId
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
collectionId
- the collection IDprivate final void setConditionsObjectCollectionIds() throws ConditionsObjectException
ConditionsObjectException
- if there is an error setting one of the collection IDsprivate final void setConditionsObjectConnections()
public final void setConnection(Connection connection)
setConnection
in interface DatabaseObject
connection
- the database connection of the collectionpublic void setTableMetaData(TableMetaData tableMetaData)
setTableMetaData
in interface DatabaseObject
tableMetaData
- the table meta data of the collectionTableMetaData
public int size()
size
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
public final void sort(Comparator<ObjectType> comparator)
sort
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
comparator
- the comparison operator to use for sortingpublic ConditionsObjectCollection<ObjectType> sorted(Comparator<ObjectType> comparator)
sorted
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
comparator
- the comparison class to use for sortingpublic String toString()
public boolean update() throws DatabaseObjectException, SQLException
update
in interface DatabaseObject
true
if at least one object was updatedSQLException
- if there is a SQL query errorDatabaseObjectException
public void writeCsv(File file) throws IOException
writeCsv
in interface ConditionsObjectCollection<ObjectType extends ConditionsObject>
file
- the output CSV fileIOException
Copyright © 2019. All rights reserved.