# Class ConcurrentMaterialStore

java.lang.Object
com.university.bookstore.impl.ConcurrentMaterialStore

All Implemented Interfaces:
  `MaterialStore`

public class ConcurrentMaterialStore extends Object implements MaterialStore
Thread-safe implementation of MaterialStore using synchronization primitives. Demonstrates concurrency patterns and thread safety in multi-threaded environments.

This implementation uses ReentrantReadWriteLock to optimize for read-heavy workloads and ConcurrentHashMap for thread-safe storage with minimal locking overhead.

Since:
  2024-09-15
Version:
  3.0
Author:
  Navid Mohaghegh

- ## Nested Class Summary

  ### Nested classes/interfaces inherited from interface com.university.bookstore.api.MaterialStore

  `MaterialStore.InventoryStats`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ConcurrentMaterialStore()`
  Creates a new thread-safe material store.
  `ConcurrentMaterialStore(Collection<Material> initialMaterials)`
  Creates a material store with initial materials.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `addMaterial(Material material)`
  Adds a material to the store inventory.
  `void`
  `clearInventory()`
  Clears all materials from the store.
  `List<Material>`
  `filterMaterials(Predicate<Material> predicate)`
  Filters materials by a custom predicate.
  `List<Material>`
  `findByCreators(String... creators)`
  Finds materials by multiple creators (OR condition).
  `Optional<Material>`
  `findById(String id)`
  Finds a material by its ID.
  `List<Material>`
  `findRecentMaterials(int years)`
  Finds materials published in the last N years.
  `List<Material>`
  `findWithPredicate(Predicate<Material> condition)`
  Finds materials with custom filtering using predicate.
  `List<String>`
  `getAllDisplayInfo()`
  Gets all display info for materials (thread-safe).
  `List<Material>`
  `getAllMaterials()`
  Gets all materials (unsorted).
  `List<Material>`
  `getAllMaterialsSorted()`
  Gets all materials sorted by title.
  `List<Material>`
  `getDiscountedMaterials()`
  Gets materials with active discounts (thread-safe).
  `MaterialStore.InventoryStats`
  `getInventoryStats()`
  Gets inventory statistics.
  `List<Material>`
  `getMaterialsByPriceRange(double minPrice, double maxPrice)`
  Gets materials within a price range.
  `List<Material>`
  `getMaterialsByType(Material.MaterialType type)`
  Gets all materials of a specific type.
  `List<Material>`
  `getMaterialsByYear(int year)`
  Gets materials published/released in a specific year.
  `List<Media>`
  `getMediaMaterials()`
  Gets all materials that implement the Media interface.
  `List<Material>`
  `getSorted(Comparator<Material> comparator)`
  Gets materials sorted by custom comparator.
  `double`
  `getTotalDiscountAmount()`
  Calculates total savings from discounts (thread-safe).
  `double`
  `getTotalDiscountedValue()`
  Calculates total discounted inventory value.
  `double`
  `getTotalInventoryValue()`
  Calculates total inventory value.
  `Map<Material.MaterialType, List<Material>>`
  `groupByType()`
  Groups materials by type for reporting (thread-safe).
  `boolean`
  `isEmpty()`
  Checks if the inventory is empty.
  `Optional<Material>`
  `removeMaterial(String id)`
  Removes a material by its ID.
  `List<Material>`
  `searchByCreator(String creator)`
  Searches materials by creator (author/director/publisher).
  `List<Material>`
  `searchByTitle(String title)`
  Searches materials by title (case-insensitive partial match).
  `int`
  `size()`
  Gets the number of materials in inventory.
  `String`
  `toString()`

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

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

- ## Constructor Details

  - ### ConcurrentMaterialStore

    public ConcurrentMaterialStore()
    Creates a new thread-safe material store.

  - ### ConcurrentMaterialStore

    public ConcurrentMaterialStore(Collection<Material> initialMaterials)
    Creates a material store with initial materials.
    Parameters:
      `initialMaterials` - materials to add initially

- ## Method Details

  - ### addMaterial

    public boolean addMaterial(Material material)
    Description copied from interface: `MaterialStore`
    Adds a material to the store inventory.
    Specified by:
      `addMaterial` in interface `MaterialStore`
    Parameters:
      `material` - the material to add
    Returns:
      true if added successfully, false if duplicate ID

  - ### removeMaterial

    public Optional<Material> removeMaterial(String id)
    Description copied from interface: `MaterialStore`
    Removes a material by its ID.
    Specified by:
      `removeMaterial` in interface `MaterialStore`
    Parameters:
      `id` - the material ID
    Returns:
      the removed material, or Optional.empty() if not found

  - ### findById

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

  - ### searchByTitle

    public List<Material> searchByTitle(String title)
    Description copied from interface: `MaterialStore`
    Searches materials by title (case-insensitive partial match).
    Specified by:
      `searchByTitle` in interface `MaterialStore`
    Parameters:
      `title` - the title to search for
    Returns:
      list of matching materials

  - ### searchByCreator

    public List<Material> searchByCreator(String creator)
    Description copied from interface: `MaterialStore`
    Searches materials by creator (author/director/publisher).
    Specified by:
      `searchByCreator` in interface `MaterialStore`
    Parameters:
      `creator` - the creator name
    Returns:
      list of matching materials

  - ### getMaterialsByType

    public List<Material> getMaterialsByType(Material.MaterialType type)
    Description copied from interface: `MaterialStore`
    Gets all materials of a specific type. Demonstrates polymorphic filtering.
    Specified by:
      `getMaterialsByType` in interface `MaterialStore`
    Parameters:
      `type` - the material type
    Returns:
      list of materials of the specified type

  - ### getMediaMaterials

    public List<Media> getMediaMaterials()
    Description copied from interface: `MaterialStore`
    Gets all materials that implement the Media interface. Demonstrates interface-based polymorphism.
    Specified by:
      `getMediaMaterials` in interface `MaterialStore`
    Returns:
      list of media materials

  - ### filterMaterials

    public List<Material> filterMaterials(Predicate<Material> predicate)
    Description copied from interface: `MaterialStore`
    Filters materials by a custom predicate. Demonstrates functional programming with polymorphism.
    Specified by:
      `filterMaterials` in interface `MaterialStore`
    Parameters:
      `predicate` - the filter condition
    Returns:
      filtered list of materials

  - ### getMaterialsByPriceRange

    public List<Material> getMaterialsByPriceRange(double minPrice, double maxPrice)
    Description copied from interface: `MaterialStore`
    Gets materials within a price range.
    Specified by:
      `getMaterialsByPriceRange` in interface `MaterialStore`
    Parameters:
      `minPrice` - minimum price (inclusive)
      `maxPrice` - maximum price (inclusive)
    Returns:
      list of materials in price range

  - ### getMaterialsByYear

    public List<Material> getMaterialsByYear(int year)
    Description copied from interface: `MaterialStore`
    Gets materials published/released in a specific year.
    Specified by:
      `getMaterialsByYear` in interface `MaterialStore`
    Parameters:
      `year` - the year
    Returns:
      list of materials from that year

  - ### getAllMaterialsSorted

    public List<Material> getAllMaterialsSorted()
    Description copied from interface: `MaterialStore`
    Gets all materials sorted by title.
    Specified by:
      `getAllMaterialsSorted` in interface `MaterialStore`
    Returns:
      sorted list of all materials

  - ### getAllMaterials

    public List<Material> getAllMaterials()
    Description copied from interface: `MaterialStore`
    Gets all materials (unsorted).
    Specified by:
      `getAllMaterials` in interface `MaterialStore`
    Returns:
      list of all materials

  - ### getTotalInventoryValue

    public double getTotalInventoryValue()
    Description copied from interface: `MaterialStore`
    Calculates total inventory value.
    Specified by:
      `getTotalInventoryValue` in interface `MaterialStore`
    Returns:
      sum of all material prices

  - ### getTotalDiscountedValue

    public double getTotalDiscountedValue()
    Description copied from interface: `MaterialStore`
    Calculates total discounted inventory value. Uses polymorphic discount calculation.
    Specified by:
      `getTotalDiscountedValue` in interface `MaterialStore`
    Returns:
      sum of all discounted prices

  - ### getInventoryStats

    public MaterialStore.InventoryStats getInventoryStats()
    Description copied from interface: `MaterialStore`
    Gets inventory statistics.
    Specified by:
      `getInventoryStats` in interface `MaterialStore`
    Returns:
      statistics object with counts and averages

  - ### clearInventory

    public void clearInventory()
    Description copied from interface: `MaterialStore`
    Clears all materials from the store.
    Specified by:
      `clearInventory` in interface `MaterialStore`

  - ### size

    public int size()
    Description copied from interface: `MaterialStore`
    Gets the number of materials in inventory.
    Specified by:
      `size` in interface `MaterialStore`
    Returns:
      material count

  - ### isEmpty

    public boolean isEmpty()
    Description copied from interface: `MaterialStore`
    Checks if the inventory is empty.
    Specified by:
      `isEmpty` in interface `MaterialStore`
    Returns:
      true if no materials in inventory

  - ### findRecentMaterials

    public List<Material> findRecentMaterials(int years)
    Description copied from interface: `MaterialStore`
    Finds materials published in the last N years.
    Specified by:
      `findRecentMaterials` in interface `MaterialStore`
    Parameters:
      `years` - the number of years to look back
    Returns:
      list of recent materials

  - ### findByCreators

    public List<Material> findByCreators(String... creators)
    Description copied from interface: `MaterialStore`
    Finds materials by multiple creators (OR condition).
    Specified by:
      `findByCreators` in interface `MaterialStore`
    Parameters:
      `creators` - the creator names to search for
    Returns:
      list of materials by any of the specified creators

  - ### findWithPredicate

    public List<Material> findWithPredicate(Predicate<Material> condition)
    Description copied from interface: `MaterialStore`
    Finds materials with custom filtering using predicate.
    Specified by:
      `findWithPredicate` in interface `MaterialStore`
    Parameters:
      `condition` - the filter condition
    Returns:
      filtered list of materials

  - ### getSorted

    public List<Material> getSorted(Comparator<Material> comparator)
    Description copied from interface: `MaterialStore`
    Gets materials sorted by custom comparator.
    Specified by:
      `getSorted` in interface `MaterialStore`
    Parameters:
      `comparator` - the sorting comparator
    Returns:
      sorted list of materials

  - ### getAllDisplayInfo

    public List<String> getAllDisplayInfo()
    Gets all display info for materials (thread-safe).
    Returns:
      list of display strings

  - ### groupByType

    public Map<Material.MaterialType, List<Material>> groupByType()
    Groups materials by type for reporting (thread-safe).
    Returns:
      map of type to materials

  - ### getDiscountedMaterials

    public List<Material> getDiscountedMaterials()
    Gets materials with active discounts (thread-safe).
    Returns:
      list of discounted materials

  - ### getTotalDiscountAmount

    public double getTotalDiscountAmount()
    Calculates total savings from discounts (thread-safe).
    Returns:
      total discount amount

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`