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.
  • Field Details

    • listOfModelObjects

      List<ModelObject extends Model> listOfModelObjects
      The list of model objects stored in the repository.
  • Constructor Details

    • Repository

      public Repository()
      Creates a new instance of the Repository class.
  • Method Details

    • getFilePath

      public abstract String getFilePath()
      Gets the path of the repository file.
      Returns:
      the path of the repository file
    • getAll

      protected List<ModelObject> getAll()
      Gets the list of mappable objects.
      Specified by:
      getAll in class Savable<ModelObject extends Model>
      Returns:
      the list of mappable objects
    • getByID

      public ModelObject getByID(String modelObjectID) throws ModelNotFoundException
      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

      public boolean contains(String modelObjectID)
      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

      public void add(ModelObject modelObject) throws ModelAlreadyExistsException
      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

      public void remove(String modelObjectID) throws ModelNotFoundException
      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

      public void update(ModelObject modelObject) throws ModelNotFoundException
      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

      public void updateAll(List<ModelObject> modelObjects)
      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

      public Iterator<ModelObject> iterator()
      Returns an iterator over the list of model objects of type T.
      Specified by:
      iterator in interface Iterable<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

      public List<ModelObject> getList()
      Gets a list of all model objects in the repository.
      Returns:
      a list of all model objects in the repository