Class ModelList<T>

java.lang.Object
entity.list.ModelList<T>
Type Parameters:
T - The type of the model objects managed by this list.
All Implemented Interfaces:
Saveable
Direct Known Subclasses:
ApplicantList, ManagerList, OfficerList, ProjectList, RequestList

public abstract class ModelList<T> extends Object implements Saveable
An abstract generic base class for managing lists of model objects of type T. Provides common functionality including an internal list storage, basic CRUD operations (add, update, delete, get all), size management, and persistence mechanisms for loading from and saving to a file (presumably CSV format, utilizing a Converter utility). This class implements the Saveable interface.

Subclasses must implement getFilePath() to define the data storage location and getByID(String) to provide type-specific retrieval by a unique ID.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ModelList(String filePath, Class<T> clazz)
    Initializes the ModelList.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T item)
    Adds a single item to the end of the list and saves the list to the file.
    void
    Removes all items from the internal list.
    void
    Deletes an item from the list based on its ID.
    Returns a defensive copy of the internal list of items.
    abstract T
    Retrieves an item of type T from the list based on its unique String identifier.
    abstract String
    Gets the specific file path used by the concrete subclass for data persistence.
    void
    load(String filePath, boolean hasHeader)
    Loads list data from the specified file path.
    void
    save(String filePath)
    Saves the current state of the internal list to the specified file path, overwriting existing content.
    int
    Returns the current number of items in the list.
    void
    update(String ID, T newItem)
    Updates an item in the list.
    void
    updateAll(List<T> newItems)
    Replaces the entire contents of the current list with the provided list of new items.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ModelList

      public ModelList(String filePath, Class<T> clazz)
      Initializes the ModelList. Creates an empty internal list, stores the class type T for potential reflection use, and immediately attempts to load existing data from the specified file path.
      Parameters:
      filePath - The path to the file used for loading and saving the list data.
      clazz - The Class object corresponding to the generic type T, required for type-specific operations like CSV conversion.
  • Method Details

    • getFilePath

      public abstract String getFilePath()
      Gets the specific file path used by the concrete subclass for data persistence. Subclasses must implement this method to define where their data is stored.
      Returns:
      The file path string for loading and saving data.
    • getByID

      public abstract T getByID(String ID)
      Retrieves an item of type T from the list based on its unique String identifier. Subclasses must implement the specific logic for searching based on their ID structure.
      Parameters:
      ID - The unique identifier of the item to retrieve.
      Returns:
      The item of type T if found, otherwise null.
    • getAll

      public List<T> getAll()
      Returns a defensive copy of the internal list of items. Modifications to the returned list will not affect the internal list managed by this class.
      Returns:
      A new ArrayList containing all items currently in the list.
    • delete

      public void delete(String ID)
      Deletes an item from the list based on its ID. Uses the subclass's getByID(String) implementation to find the item. If the item is found, it is removed from the list, and the list is saved to the file.
      Parameters:
      ID - The unique identifier of the item to delete.
    • update

      public void update(String ID, T newItem)
      Updates an item in the list. This implementation performs an update by first removing the old item identified by ID and then adding the newItem. The list is saved after the operation. Note: This ensures the item is replaced but might affect order if the list is ordered.
      Parameters:
      ID - The unique identifier of the item to replace.
      newItem - The new item object to add in place of the old one.
    • updateAll

      public void updateAll(List<T> newItems)
      Replaces the entire contents of the current list with the provided list of new items. The internal list is cleared, the new items are added, and the list is saved.
      Parameters:
      newItems - The list of items that will replace the current contents.
    • add

      public void add(T item)
      Adds a single item to the end of the list and saves the list to the file.
      Parameters:
      item - The item of type T to add.
    • size

      public int size()
      Returns the current number of items in the list.
      Returns:
      The size of the internal list.
    • clear

      public void clear()
      Removes all items from the internal list. Note: This method only clears the in-memory list; it does not automatically save the empty state to the file. A subsequent save operation (e.g., via add, delete, update) is required to persist the cleared state.
    • load

      public void load(String filePath, boolean hasHeader)
      Loads list data from the specified file path. If the file exists, it reads each line (optionally skipping a header), converts the line to an object of type T using Converter.stringtoObj(String, Class), and adds it to the internal list. If the file does not exist, it attempts to create it. Errors during file reading or creation are printed to standard error.
      Specified by:
      load in interface Saveable
      Parameters:
      filePath - The path to the file from which to load data.
      hasHeader - If true, the first line of the file is skipped as a header.
    • save

      public void save(String filePath)
      Saves the current state of the internal list to the specified file path, overwriting existing content. Implements the Saveable.save(String) method. If the list is not empty, it generates a header line using Converter.getField(Object) based on the first item, then writes the header followed by each item converted to a string using Converter.objToString(Object).
      Specified by:
      save in interface Saveable
      Parameters:
      filePath - The path to the file where the data should be saved.
      Throws:
      RuntimeException - if an IOException occurs during file writing.