Class ConcurrentMaterialStore
java.lang.Object
com.university.bookstore.impl.ConcurrentMaterialStore
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionCreates a new thread-safe material store.ConcurrentMaterialStore(Collection<Material> initialMaterials) Creates a material store with initial materials. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddMaterial(Material material) Adds a material to the store inventory.voidClears all materials from the store.filterMaterials(Predicate<Material> predicate) Filters materials by a custom predicate.findByCreators(String... creators) Finds materials by multiple creators (OR condition).Finds a material by its ID.findRecentMaterials(int years) Finds materials published in the last N years.findWithPredicate(Predicate<Material> condition) Finds materials with custom filtering using predicate.Gets all display info for materials (thread-safe).Gets all materials (unsorted).Gets all materials sorted by title.Gets materials with active discounts (thread-safe).Gets inventory statistics.getMaterialsByPriceRange(double minPrice, double maxPrice) Gets materials within a price range.Gets all materials of a specific type.getMaterialsByYear(int year) Gets materials published/released in a specific year.Gets all materials that implement the Media interface.getSorted(Comparator<Material> comparator) Gets materials sorted by custom comparator.doubleCalculates total savings from discounts (thread-safe).doubleCalculates total discounted inventory value.doubleCalculates total inventory value.Groups materials by type for reporting (thread-safe).booleanisEmpty()Checks if the inventory is empty.removeMaterial(String id) Removes a material by its ID.searchByCreator(String creator) Searches materials by creator (author/director/publisher).searchByTitle(String title) Searches materials by title (case-insensitive partial match).intsize()Gets the number of materials in inventory.toString()
-
Constructor Details
-
ConcurrentMaterialStore
public ConcurrentMaterialStore()Creates a new thread-safe material store. -
ConcurrentMaterialStore
Creates a material store with initial materials.- Parameters:
initialMaterials- materials to add initially
-
-
Method Details
-
addMaterial
Description copied from interface:MaterialStoreAdds a material to the store inventory.- Specified by:
addMaterialin interfaceMaterialStore- Parameters:
material- the material to add- Returns:
- true if added successfully, false if duplicate ID
-
removeMaterial
Description copied from interface:MaterialStoreRemoves a material by its ID.- Specified by:
removeMaterialin interfaceMaterialStore- Parameters:
id- the material ID- Returns:
- the removed material, or Optional.empty() if not found
-
findById
Description copied from interface:MaterialStoreFinds a material by its ID.- Specified by:
findByIdin interfaceMaterialStore- Parameters:
id- the material ID- Returns:
- the material, or Optional.empty() if not found
-
searchByTitle
Description copied from interface:MaterialStoreSearches materials by title (case-insensitive partial match).- Specified by:
searchByTitlein interfaceMaterialStore- Parameters:
title- the title to search for- Returns:
- list of matching materials
-
searchByCreator
Description copied from interface:MaterialStoreSearches materials by creator (author/director/publisher).- Specified by:
searchByCreatorin interfaceMaterialStore- Parameters:
creator- the creator name- Returns:
- list of matching materials
-
getMaterialsByType
Description copied from interface:MaterialStoreGets all materials of a specific type. Demonstrates polymorphic filtering.- Specified by:
getMaterialsByTypein interfaceMaterialStore- Parameters:
type- the material type- Returns:
- list of materials of the specified type
-
getMediaMaterials
Description copied from interface:MaterialStoreGets all materials that implement the Media interface. Demonstrates interface-based polymorphism.- Specified by:
getMediaMaterialsin interfaceMaterialStore- Returns:
- list of media materials
-
filterMaterials
Description copied from interface:MaterialStoreFilters materials by a custom predicate. Demonstrates functional programming with polymorphism.- Specified by:
filterMaterialsin interfaceMaterialStore- Parameters:
predicate- the filter condition- Returns:
- filtered list of materials
-
getMaterialsByPriceRange
Description copied from interface:MaterialStoreGets materials within a price range.- Specified by:
getMaterialsByPriceRangein interfaceMaterialStore- Parameters:
minPrice- minimum price (inclusive)maxPrice- maximum price (inclusive)- Returns:
- list of materials in price range
-
getMaterialsByYear
Description copied from interface:MaterialStoreGets materials published/released in a specific year.- Specified by:
getMaterialsByYearin interfaceMaterialStore- Parameters:
year- the year- Returns:
- list of materials from that year
-
getAllMaterialsSorted
Description copied from interface:MaterialStoreGets all materials sorted by title.- Specified by:
getAllMaterialsSortedin interfaceMaterialStore- Returns:
- sorted list of all materials
-
getAllMaterials
Description copied from interface:MaterialStoreGets all materials (unsorted).- Specified by:
getAllMaterialsin interfaceMaterialStore- Returns:
- list of all materials
-
getTotalInventoryValue
public double getTotalInventoryValue()Description copied from interface:MaterialStoreCalculates total inventory value.- Specified by:
getTotalInventoryValuein interfaceMaterialStore- Returns:
- sum of all material prices
-
getTotalDiscountedValue
public double getTotalDiscountedValue()Description copied from interface:MaterialStoreCalculates total discounted inventory value. Uses polymorphic discount calculation.- Specified by:
getTotalDiscountedValuein interfaceMaterialStore- Returns:
- sum of all discounted prices
-
getInventoryStats
Description copied from interface:MaterialStoreGets inventory statistics.- Specified by:
getInventoryStatsin interfaceMaterialStore- Returns:
- statistics object with counts and averages
-
clearInventory
public void clearInventory()Description copied from interface:MaterialStoreClears all materials from the store.- Specified by:
clearInventoryin interfaceMaterialStore
-
size
public int size()Description copied from interface:MaterialStoreGets the number of materials in inventory.- Specified by:
sizein interfaceMaterialStore- Returns:
- material count
-
isEmpty
public boolean isEmpty()Description copied from interface:MaterialStoreChecks if the inventory is empty.- Specified by:
isEmptyin interfaceMaterialStore- Returns:
- true if no materials in inventory
-
findRecentMaterials
Description copied from interface:MaterialStoreFinds materials published in the last N years.- Specified by:
findRecentMaterialsin interfaceMaterialStore- Parameters:
years- the number of years to look back- Returns:
- list of recent materials
-
findByCreators
Description copied from interface:MaterialStoreFinds materials by multiple creators (OR condition).- Specified by:
findByCreatorsin interfaceMaterialStore- Parameters:
creators- the creator names to search for- Returns:
- list of materials by any of the specified creators
-
findWithPredicate
Description copied from interface:MaterialStoreFinds materials with custom filtering using predicate.- Specified by:
findWithPredicatein interfaceMaterialStore- Parameters:
condition- the filter condition- Returns:
- filtered list of materials
-
getSorted
Description copied from interface:MaterialStoreGets materials sorted by custom comparator.- Specified by:
getSortedin interfaceMaterialStore- Parameters:
comparator- the sorting comparator- Returns:
- sorted list of materials
-
getAllDisplayInfo
-
groupByType
Groups materials by type for reporting (thread-safe).- Returns:
- map of type to materials
-
getDiscountedMaterials
-
getTotalDiscountAmount
public double getTotalDiscountAmount()Calculates total savings from discounts (thread-safe).- Returns:
- total discount amount
-
toString
-