Class JsonMaterialRepository
java.lang.Object
com.university.bookstore.repository.JsonMaterialRepository
- All Implemented Interfaces:
MaterialRepository
JSON-based implementation of MaterialRepository (Adapter in hexagonal architecture).
Persists materials to a JSON file using Jackson for serialization.
This adapter implements the MaterialRepository port, providing JSON file-based persistence without the domain layer knowing about the storage mechanism.
- Since:
- 2024-09-15
- Version:
- 3.0
- Author:
- Navid Mohaghegh
-
Constructor Summary
ConstructorsConstructorDescriptionJsonMaterialRepository(String filePath) Creates a new JSON material repository. -
Method Summary
Modifier and TypeMethodDescriptionlongcount()Counts the total number of materials in the repository.booleanChecks if the data file exists.booleanDeletes a material by its ID.voidDeletes all materials from the repository.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.voidSaves a material to the repository.
-
Constructor Details
-
JsonMaterialRepository
Creates a new 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
-
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
-
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
-
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
-