Class ModernConcurrentMaterialStore

java.lang.Object
com.university.bookstore.impl.ModernConcurrentMaterialStore
All Implemented Interfaces:
MaterialStore, AutoCloseable

public class ModernConcurrentMaterialStore extends Object implements MaterialStore, AutoCloseable
Modern thread-safe implementation of MaterialStore using best practices. Features: - StampedLock for optimized read performance - ExecutorService for async operations - CompletableFuture for non-blocking operations - Proper resource management with AutoCloseable - Virtual thread support (when available)
Since:
2024-09-15
Version:
4.0
Author:
Navid Mohaghegh
  • Constructor Details

    • ModernConcurrentMaterialStore

      public ModernConcurrentMaterialStore()
      Creates a new modern thread-safe material store.
    • ModernConcurrentMaterialStore

      public ModernConcurrentMaterialStore(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
    • addMaterialAsync

      public CompletableFuture<Boolean> addMaterialAsync(Material material)
      Adds material asynchronously.
      Parameters:
      material - the material to add
      Returns:
      CompletableFuture with the result
    • addMaterialsBatchAsync

      public CompletableFuture<Map<String,Boolean>> addMaterialsBatchAsync(Collection<Material> materials)
      Adds multiple materials in batch asynchronously.
      Parameters:
      materials - collection of materials to add
      Returns:
      CompletableFuture with results map
    • 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
    • findByIdAsync

      public CompletableFuture<Optional<Material>> findByIdAsync(String id)
      Finds material by ID asynchronously.
      Parameters:
      id - the material ID
      Returns:
      CompletableFuture with the result
    • findByIdsAsync

      public CompletableFuture<Map<String,Material>> findByIdsAsync(List<String> ids)
      Finds multiple materials by IDs asynchronously.
      Parameters:
      ids - list of material IDs
      Returns:
      CompletableFuture with results map
    • 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
    • searchByTitleAsync

      public CompletableFuture<List<Material>> searchByTitleAsync(String title)
      Searches by title asynchronously.
      Parameters:
      title - the title to search for
      Returns:
      CompletableFuture with the results
    • 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
    • getTotalInventoryValueAsync

      public CompletableFuture<Double> getTotalInventoryValueAsync()
      Gets total inventory value asynchronously.
      Returns:
      CompletableFuture with the total value
    • 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
    • getInventoryStatsAsync

      public CompletableFuture<MaterialStore.InventoryStats> getInventoryStatsAsync()
      Gets inventory statistics asynchronously.
      Returns:
      CompletableFuture with the statistics
    • 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
    • parallelSearchAsync

      public CompletableFuture<List<Material>> parallelSearchAsync(String title, String creator, Material.MaterialType type)
      Performs parallel search across multiple criteria.
      Parameters:
      title - optional title search term
      creator - optional creator search term
      type - optional material type
      Returns:
      CompletableFuture with combined results
    • groupByType

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

      public List<Material> getDiscountedMaterials()
      Gets materials with active discounts.
      Returns:
      list of discounted materials
    • getTotalDiscountAmount

      public double getTotalDiscountAmount()
      Calculates total savings from discounts.
      Returns:
      total discount amount
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object