Class MaterialIteratorFactory

java.lang.Object
com.university.bookstore.iterator.MaterialIteratorFactory

public class MaterialIteratorFactory extends Object
Factory for creating different types of material iterators. Provides a centralized way to create iterators with different traversal strategies.

This factory demonstrates the Factory pattern by providing methods to create various types of iterators without exposing the concrete iterator classes.

Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
  • Constructor Details

    • MaterialIteratorFactory

      public MaterialIteratorFactory()
  • Method Details

    • createTypeIterator

      public MaterialIterator createTypeIterator(List<Material> materials, Material.MaterialType type)
      Creates a type-filtering iterator.
      Parameters:
      materials - the list of materials to iterate over
      type - the material type to filter for
      Returns:
      iterator for materials of the specified type
    • createPriceSortedIterator

      public MaterialIterator createPriceSortedIterator(List<Material> materials, boolean ascending)
      Creates a price-sorted iterator.
      Parameters:
      materials - the list of materials to iterate over
      ascending - true for ascending order, false for descending
      Returns:
      iterator for materials sorted by price
    • createPriceRangeIterator

      public MaterialIterator createPriceRangeIterator(List<Material> materials, double minPrice, double maxPrice)
      Creates a price range iterator.
      Parameters:
      materials - the list of materials to iterate over
      minPrice - the minimum price (inclusive)
      maxPrice - the maximum price (inclusive)
      Returns:
      iterator for materials within the price range
    • createEBookIterator

      public MaterialIterator createEBookIterator(List<Material> materials)
      Creates an e-book iterator.
      Parameters:
      materials - the list of materials to iterate over
      Returns:
      iterator for e-books only
    • createExpensiveIterator

      public MaterialIterator createExpensiveIterator(List<Material> materials, double threshold)
      Creates an expensive materials iterator.
      Parameters:
      materials - the list of materials to iterate over
      threshold - the minimum price threshold
      Returns:
      iterator for materials above the price threshold
    • createCheapIterator

      public MaterialIterator createCheapIterator(List<Material> materials, double threshold)
      Creates a cheap materials iterator.
      Parameters:
      materials - the list of materials to iterate over
      threshold - the maximum price threshold
      Returns:
      iterator for materials below the price threshold
    • createBookIterator

      public MaterialIterator createBookIterator(List<Material> materials)
      Creates a book iterator.
      Parameters:
      materials - the list of materials to iterate over
      Returns:
      iterator for books only
    • createMagazineIterator

      public MaterialIterator createMagazineIterator(List<Material> materials)
      Creates a magazine iterator.
      Parameters:
      materials - the list of materials to iterate over
      Returns:
      iterator for magazines only
    • createAudioBookIterator

      public MaterialIterator createAudioBookIterator(List<Material> materials)
      Creates an audio book iterator.
      Parameters:
      materials - the list of materials to iterate over
      Returns:
      iterator for audio books only
    • createVideoIterator

      public MaterialIterator createVideoIterator(List<Material> materials)
      Creates a video iterator.
      Parameters:
      materials - the list of materials to iterate over
      Returns:
      iterator for videos only
    • collectAll

      public List<Material> collectAll(MaterialIterator iterator)
      Collects all materials from an iterator into a list.
      Parameters:
      iterator - the iterator to collect from
      Returns:
      list of all materials from the iterator
    • findFirst

      public Optional<Material> findFirst(MaterialIterator iterator, Predicate<Material> predicate)
      Finds the first material matching a predicate.
      Parameters:
      iterator - the iterator to search
      predicate - the predicate to test
      Returns:
      Optional containing the first matching material
    • findAll

      public List<Material> findAll(MaterialIterator iterator, Predicate<Material> predicate)
      Finds all materials matching a predicate.
      Parameters:
      iterator - the iterator to search
      predicate - the predicate to test
      Returns:
      list of all matching materials
    • count

      public int count(MaterialIterator iterator, Predicate<Material> predicate)
      Counts materials matching a predicate.
      Parameters:
      iterator - the iterator to count
      predicate - the predicate to test
      Returns:
      the count of matching materials
    • anyMatch

      public boolean anyMatch(MaterialIterator iterator, Predicate<Material> predicate)
      Checks if any material matches a predicate.
      Parameters:
      iterator - the iterator to check
      predicate - the predicate to test
      Returns:
      true if any material matches
    • allMatch

      public boolean allMatch(MaterialIterator iterator, Predicate<Material> predicate)
      Checks if all materials match a predicate.
      Parameters:
      iterator - the iterator to check
      predicate - the predicate to test
      Returns:
      true if all materials match
    • getTotalCount

      public int getTotalCount(MaterialIterator iterator)
      Gets the total count of materials in an iterator.
      Parameters:
      iterator - the iterator to count
      Returns:
      the total count
    • getRemainingCount

      public int getRemainingCount(MaterialIterator iterator)
      Gets the remaining count of materials in an iterator.
      Parameters:
      iterator - the iterator to check
      Returns:
      the remaining count
    • toString

      public String toString()
      Overrides:
      toString in class Object