Class RewardsService

java.lang.Object
com.openclassrooms.tourguide.service.RewardsService

@Service public class RewardsService extends Object
  • Field Details

    • STATUTE_MILES_PER_NAUTICAL_MILE

      private static final double STATUTE_MILES_PER_NAUTICAL_MILE
      See Also:
    • defaultProximityBuffer

      private final int defaultProximityBuffer
      See Also:
    • proximityBuffer

      private int proximityBuffer
    • attractionProximityRange

      private final int attractionProximityRange
      See Also:
    • gpsUtil

      private final gpsUtil.GpsUtil gpsUtil
    • rewardsCentral

      private final rewardCentral.RewardCentral rewardsCentral
    • distanceCache

      private final Map<String,Double> distanceCache
    • executor

      private final ExecutorService executor
    • MAX_THREADS

      private static final int MAX_THREADS
      See Also:
    • semaphore

      private static final Semaphore semaphore
    • maxAttractionsToCheck

      private int maxAttractionsToCheck
    • allUsers

      private List<User> allUsers
    • rewardPointsCache

      private final com.github.benmanes.caffeine.cache.Cache<String,Integer> rewardPointsCache
  • Constructor Details

    • RewardsService

      public RewardsService(gpsUtil.GpsUtil gpsUtil, rewardCentral.RewardCentral rewardCentral, ExecutorService executorService)
      Creates a rewards management service with GPS and RewardCentral services, and a thread pool for asynchronous tasks.
      Parameters:
      gpsUtil - Geolocation service
      rewardCentral - Service to get reward points
      executorService - Executor for parallel tasks
      Throws:
      IllegalStateException - if the executor is null or already arrested
  • Method Details

    • setMaxAttractionsToCheck

      public void setMaxAttractionsToCheck(int maxAttractionsToCheck)
      Sets the maximum number of attractions to analyze to calculate rewards.
      Parameters:
      maxAttractionsToCheck - Limit on the number of attractions to be considered
    • shutdownExecutor

      @PreDestroy public void shutdownExecutor()
      Cleanly shuts down the thread pool if it is still active.
    • setAllUsers

      public void setAllUsers(List<User> allUsers)
      Replaces the list of service users with a new one.
      Parameters:
      allUsers - List of users to register
      Throws:
      IllegalStateException - if the list is empty or null
    • setProximityBuffer

      public void setProximityBuffer(int proximityBuffer)
      Changes the maximum distance to consider an attraction as close.
      Parameters:
      proximityBuffer - Distance value to use
    • setDefaultProximityBuffer

      public void setDefaultProximityBuffer()
      Resets the proximity distance to its default value.
    • calculateRewards

      public void calculateRewards(User user, List<gpsUtil.location.Attraction> attractions)
      Calculates rewards for a user based on their past visits.
      Parameters:
      user - Concerned user
      attractions - List of available attractions
    • calculateRewardsAsync

      public CompletableFuture<Void> calculateRewardsAsync(User user, List<gpsUtil.location.Attraction> attractions)
      Calculates rewards for a user asynchronously (in the background).
      Parameters:
      user - Concerned user
      attractions - List of available attractions
      Returns:
      An asynchronous task representing the current computation
    • calculateRewardsForAllUsers

      public void calculateRewardsForAllUsers(List<User> users, List<gpsUtil.location.Attraction> attractions)
      Calculates rewards for a list of users in parallel.
      Parameters:
      users - User list
      attractions - List of attractions to consider
    • getCacheKey

      private String getCacheKey(gpsUtil.location.Location location, gpsUtil.location.Attraction attraction)
      Creates a unique key to associate a geographic location with an attraction. Latitude and longitude coordinates are rounded to two decimal places to reduce precision and improve cache reuse.*
      Parameters:
      location - Reference location
      attraction - Attraction concerned
      Returns:
      A string representing the associated cache key
    • cachedDistance

      private double cachedDistance(gpsUtil.location.Location location, gpsUtil.location.Attraction attraction)
      Calculates the distance between a location and an attraction with caching. If the distance has already been calculated for this location and attraction combination, it is retrieved from the cache. Otherwise, it is calculated and stored.
      Parameters:
      location - Reference location
      attraction - Target attraction
      Returns:
      Distance between location and attraction (in miles)
    • isWithinAttractionProximity

      public boolean isWithinAttractionProximity(gpsUtil.location.Attraction attraction, gpsUtil.location.Location location)
      Check if an attraction is close enough to a location.
      Parameters:
      attraction - Attraction to analyze
      location - Position à comparer
      Returns:
      true si la distance est inférieure à la limite, false sinon
    • nearAttraction

      private boolean nearAttraction(gpsUtil.location.VisitedLocation visitedLocation, gpsUtil.location.Attraction attraction)
    • getRewardPoints

      public int getRewardPoints(gpsUtil.location.Attraction attraction, User user)
      Returns the number of reward points earned by a user at a given attraction. Uses a cache to speed up the result.*
      Parameters:
      attraction - The attraction visited
      user - The user concerned
      Returns:
      Number of points awarded
    • getDistance

      public double getDistance(gpsUtil.location.Location loc1, gpsUtil.location.Location loc2)
      Calculates the distance between two geographic locations.
      Parameters:
      loc1 - First point (latitude, longitude)
      loc2 - Second point
      Returns:
      Distance between the two locations in miles