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
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a single item to the end of the list and saves the list to the file.voidclear()Removes all items from the internal list.voidDeletes an item from the list based on its ID.getAll()Returns a defensive copy of the internal list of items.abstract TRetrieves an item of typeTfrom the list based on its unique String identifier.abstract StringGets the specific file path used by the concrete subclass for data persistence.voidLoads list data from the specified file path.voidSaves the current state of the internal list to the specified file path, overwriting existing content.intsize()Returns the current number of items in the list.voidUpdates an item in the list.voidReplaces the entire contents of the current list with the provided list of new items.
-
Constructor Details
-
ModelList
Initializes the ModelList. Creates an empty internal list, stores the class typeTfor 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- TheClassobject corresponding to the generic typeT, required for type-specific operations like CSV conversion.
-
-
Method Details
-
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
Retrieves an item of typeTfrom 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
Tif found, otherwisenull.
-
getAll
-
delete
Deletes an item from the list based on its ID. Uses the subclass'sgetByID(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
Updates an item in the list. This implementation performs an update by first removing the old item identified byIDand then adding thenewItem. 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
-
add
Adds a single item to the end of the list and saves the list to the file.- Parameters:
item- The item of typeTto 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
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 typeTusingConverter.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. -
save
Saves the current state of the internal list to the specified file path, overwriting existing content. Implements theSaveable.save(String)method. If the list is not empty, it generates a header line usingConverter.getField(Object)based on the first item, then writes the header followed by each item converted to a string usingConverter.objToString(Object).- Specified by:
savein interfaceSaveable- Parameters:
filePath- The path to the file where the data should be saved.- Throws:
RuntimeException- if anIOExceptionoccurs during file writing.
-