Class ModernJsonMaterialRepository
java.lang.Object
com.university.bookstore.repository.ModernJsonMaterialRepository
- All Implemented Interfaces:
MaterialRepository,AutoCloseable
public class ModernJsonMaterialRepository
extends Object
implements MaterialRepository, AutoCloseable
Modern JSON-based implementation of MaterialRepository using best practices.
Features:
- Try-with-resources for automatic resource management
- NIO.2 for better file operations
- Thread-safe file operations with ReadWriteLock
- Better error handling and logging
- Atomic file operations to prevent corruption
- Since:
- 2024-09-15
- Version:
- 4.0
- Author:
- Navid Mohaghegh
-
Constructor Summary
ConstructorsConstructorDescriptionModernJsonMaterialRepository(String filePath) Creates a new modern JSON material repository. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()longcount()Counts the total number of materials in the repository.booleanCreates a backup of the current data file.booleanChecks if the data file exists.booleanDeletes a material by its ID.voidDeletes all materials from the repository.intdeleteAll(Collection<String> ids) Deletes multiple materials in batch.booleanChecks if a material with the given ID exists.findAll()Retrieves all materials from the repository.Finds a material by its unique identifier.longGets the size of the data file in bytes.Gets the file path used for persistence.voidPerforms maintenance operations like cleanup and optimization.booleanrestoreFromBackup(String backupPath) Restores data from a backup file.voidSaves a material to the repository.voidsaveAll(Collection<Material> materialsToSave) Saves multiple materials in batch for better performance.
-
Constructor Details
-
ModernJsonMaterialRepository
Creates a new modern JSON material repository.- Parameters:
filePath- the path to the JSON file for persistence
-
-
Method Details
-
save
Description copied from interface:MaterialRepositorySaves a material to the repository. If a material with the same ID already exists, it will be updated.- Specified by:
savein interfaceMaterialRepository- Parameters:
material- the material to save
-
saveAll
Saves multiple materials in batch for better performance.- Parameters:
materialsToSave- collection of materials to save
-
findById
Description copied from interface:MaterialRepositoryFinds a material by its unique identifier.- Specified by:
findByIdin interfaceMaterialRepository- Parameters:
id- the material ID- Returns:
- the material if found, empty Optional otherwise
-
findAll
Description copied from interface:MaterialRepositoryRetrieves all materials from the repository.- Specified by:
findAllin interfaceMaterialRepository- Returns:
- list of all materials
-
delete
Description copied from interface:MaterialRepositoryDeletes a material by its ID.- Specified by:
deletein interfaceMaterialRepository- Parameters:
id- the material ID to delete- Returns:
- true if the material was deleted, false if not found
-
deleteAll
Deletes multiple materials in batch.- Parameters:
ids- collection of IDs to delete- Returns:
- number of materials deleted
-
exists
Description copied from interface:MaterialRepositoryChecks if a material with the given ID exists.- Specified by:
existsin interfaceMaterialRepository- Parameters:
id- the material ID- Returns:
- true if the material exists, false otherwise
-
count
public long count()Description copied from interface:MaterialRepositoryCounts the total number of materials in the repository.- Specified by:
countin interfaceMaterialRepository- Returns:
- the number of materials
-
deleteAll
public void deleteAll()Description copied from interface:MaterialRepositoryDeletes all materials from the repository.- Specified by:
deleteAllin interfaceMaterialRepository
-
createBackup
public boolean createBackup()Creates a backup of the current data file.- Returns:
- true if backup was created successfully
-
restoreFromBackup
Restores data from a backup file.- Parameters:
backupPath- path to the backup file- Returns:
- true if restore was successful
-
getFilePath
-
dataFileExists
public boolean dataFileExists()Checks if the data file exists.- Returns:
- true if the file exists
-
getDataFileSize
public long getDataFileSize()Gets the size of the data file in bytes.- Returns:
- file size in bytes
-
performMaintenance
public void performMaintenance()Performs maintenance operations like cleanup and optimization. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-