Package main.repository
Class Repository<ModelObject extends Model>
java.lang.Object
main.utils.iocontrol.Savable<ModelObject>
main.repository.Repository<ModelObject>
- Type Parameters:
ModelObject
- the type of model object stored in the repository
- All Implemented Interfaces:
Iterable<ModelObject>
- Direct Known Subclasses:
CoordinatorRepository
,FacultyRepository
,ProjectRepository
,RequestRepository
,StudentRepository
public abstract class Repository<ModelObject extends Model>
extends Savable<ModelObject>
implements Iterable<ModelObject>
The Repository abstract class provides the basic functionality for storing, retrieving, and managing a list of model objects.
It implements the Savable interface and provides methods for adding, removing, updating, and finding model objects.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Provides a rule for filtering model objects in the repository. -
Field Summary
Modifier and TypeFieldDescription(package private) List<ModelObject>
The list of model objects stored in the repository. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(ModelObject modelObject) Adds a model object to the repository.void
clear()
Removes all model objects from this repository.boolean
Checks whether the repository contains a model object with the given ID.final List<ModelObject>
findByRules
(Repository.RepositoryRule<ModelObject>... rules) Finds all model objects in the repository that match the specified rules.protected List<ModelObject>
getAll()
Gets the list of mappable objects.Gets a model object by IDabstract String
Gets the path of the repository file.getList()
Gets a list of all model objects in the repository.boolean
isEmpty()
Checks whether the repository is empty.iterator()
Returns an iterator over the list of model objects of typeT
.void
load()
Loads the list of model objects from the repository file.void
Removes a model object from the repository by ID.void
save()
Saves the list of model objects to the repository file.int
size()
Gets the size of the repository.void
update
(ModelObject modelObject) Updates the specified model object in the repository.void
updateAll
(List<ModelObject> modelObjects) Updates all model objects in the repository with the specified list of model objects.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
listOfModelObjects
List<ModelObject extends Model> listOfModelObjectsThe list of model objects stored in the repository.
-
-
Constructor Details
-
Repository
public Repository()Creates a new instance of the Repository class.
-
-
Method Details
-
getFilePath
Gets the path of the repository file.- Returns:
- the path of the repository file
-
getAll
Gets the list of mappable objects.- Specified by:
getAll
in classSavable<ModelObject extends Model>
- Returns:
- the list of mappable objects
-
getByID
Gets a model object by ID- Parameters:
modelObjectID
- the ID of the model object to get- Returns:
- the model object with the given ID
- Throws:
ModelNotFoundException
- if the model object with the given ID does not exist
-
contains
Checks whether the repository contains a model object with the given ID.- Parameters:
modelObjectID
- the ID of the model object to check- Returns:
- true if the repository contains a model object with the given ID, false otherwise
-
add
Adds a model object to the repository.- Parameters:
modelObject
- the model object to add- Throws:
ModelAlreadyExistsException
- if a model object with the same ID already exists in the repository
-
remove
Removes a model object from the repository by ID.- Parameters:
modelObjectID
- the ID of the model object to remove- Throws:
ModelNotFoundException
- if the model object with the given ID does not exist
-
isEmpty
public boolean isEmpty()Checks whether the repository is empty.- Returns:
- true if the repository is empty, false otherwise
-
size
public int size()Gets the size of the repository.- Returns:
- the size of the repository
-
clear
public void clear()Removes all model objects from this repository. -
update
Updates the specified model object in the repository.- Parameters:
modelObject
- the model object to update- Throws:
ModelNotFoundException
- if the specified model object is not found in the repository
-
updateAll
Updates all model objects in the repository with the specified list of model objects.- Parameters:
modelObjects
- the list of model objects to update
-
load
public void load()Loads the list of model objects from the repository file. -
save
public void save()Saves the list of model objects to the repository file. -
iterator
Returns an iterator over the list of model objects of typeT
.- Specified by:
iterator
in interfaceIterable<ModelObject extends Model>
- Returns:
- an iterator over the list of model objects
-
findByRules
@SafeVarargs public final List<ModelObject> findByRules(Repository.RepositoryRule<ModelObject>... rules) Finds all model objects in the repository that match the specified rules.Multiple rules can be specified, and all rules must be satisfied for a model object to be considered a match.
The rules are specified as lambda expressions that take a model object as a parameter and return a boolean.
Here is an example of how to use this method:
List<Student> modelObjects = repository.findByRules( student -> student.getFirstName().equals("John"), student -> student.getLastName().equals("Smith") );
This will return a list of all students whose first name is "John" and whose last name is "Smith".
- Parameters:
rules
- the rules to match- Returns:
- a list of all model objects in the repository that match the specified rules
-
getList
Gets a list of all model objects in the repository.- Returns:
- a list of all model objects in the repository
-