# 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

  Constructors
  Constructor
  Description
  `ModernJsonMaterialRepository(String filePath)`
  Creates a new modern JSON material repository.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `long`
  `count()`
  Counts the total number of materials in the repository.
  `boolean`
  `createBackup()`
  Creates a backup of the current data file.
  `boolean`
  `dataFileExists()`
  Checks if the data file exists.
  `boolean`
  `delete(String id)`
  Deletes a material by its ID.
  `void`
  `deleteAll()`
  Deletes all materials from the repository.
  `int`
  `deleteAll(Collection<String> ids)`
  Deletes multiple materials in batch.
  `boolean`
  `exists(String id)`
  Checks if a material with the given ID exists.
  `List<Material>`
  `findAll()`
  Retrieves all materials from the repository.
  `Optional<Material>`
  `findById(String id)`
  Finds a material by its unique identifier.
  `long`
  `getDataFileSize()`
  Gets the size of the data file in bytes.
  `String`
  `getFilePath()`
  Gets the file path used for persistence.
  `void`
  `performMaintenance()`
  Performs maintenance operations like cleanup and optimization.
  `boolean`
  `restoreFromBackup(String backupPath)`
  Restores data from a backup file.
  `void`
  `save(Material material)`
  Saves a material to the repository.
  `void`
  `saveAll(Collection<Material> materialsToSave)`
  Saves multiple materials in batch for better performance.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ModernJsonMaterialRepository

    public ModernJsonMaterialRepository(String filePath)
    Creates a new modern JSON material repository.
    Parameters:
      `filePath` - the path to the JSON file for persistence

- ## Method Details

  - ### save

    public void save(Material material)
    Description copied from interface: `MaterialRepository`
    Saves a material to the repository. If a material with the same ID already exists, it will be updated.
    Specified by:
      `save` in interface `MaterialRepository`
    Parameters:
      `material` - the material to save

  - ### saveAll

    public void saveAll(Collection<Material> materialsToSave)
    Saves multiple materials in batch for better performance.
    Parameters:
      `materialsToSave` - collection of materials to save

  - ### findById

    public Optional<Material> findById(String id)
    Description copied from interface: `MaterialRepository`
    Finds a material by its unique identifier.
    Specified by:
      `findById` in interface `MaterialRepository`
    Parameters:
      `id` - the material ID
    Returns:
      the material if found, empty Optional otherwise

  - ### findAll

    public List<Material> findAll()
    Description copied from interface: `MaterialRepository`
    Retrieves all materials from the repository.
    Specified by:
      `findAll` in interface `MaterialRepository`
    Returns:
      list of all materials

  - ### delete

    public boolean delete(String id)
    Description copied from interface: `MaterialRepository`
    Deletes a material by its ID.
    Specified by:
      `delete` in interface `MaterialRepository`
    Parameters:
      `id` - the material ID to delete
    Returns:
      true if the material was deleted, false if not found

  - ### deleteAll

    public int deleteAll(Collection<String> ids)
    Deletes multiple materials in batch.
    Parameters:
      `ids` - collection of IDs to delete
    Returns:
      number of materials deleted

  - ### exists

    public boolean exists(String id)
    Description copied from interface: `MaterialRepository`
    Checks if a material with the given ID exists.
    Specified by:
      `exists` in interface `MaterialRepository`
    Parameters:
      `id` - the material ID
    Returns:
      true if the material exists, false otherwise

  - ### count

    public long count()
    Description copied from interface: `MaterialRepository`
    Counts the total number of materials in the repository.
    Specified by:
      `count` in interface `MaterialRepository`
    Returns:
      the number of materials

  - ### deleteAll

    public void deleteAll()
    Description copied from interface: `MaterialRepository`
    Deletes all materials from the repository.
    Specified by:
      `deleteAll` in interface `MaterialRepository`

  - ### createBackup

    public boolean createBackup()
    Creates a backup of the current data file.
    Returns:
      true if backup was created successfully

  - ### restoreFromBackup

    public boolean restoreFromBackup(String backupPath)
    Restores data from a backup file.
    Parameters:
      `backupPath` - path to the backup file
    Returns:
      true if restore was successful

  - ### getFilePath

    public String getFilePath()
    Gets the file path used for persistence.
    Returns:
      the file path

  - ### 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:
      `close` in interface `AutoCloseable`