Class RewardsService
java.lang.Object
com.openclassrooms.tourguide.service.RewardsService
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
private final int
private final ExecutorService
private final gpsUtil.GpsUtil
private static final int
private int
private int
private final rewardCentral.RewardCentral
private static final Semaphore
private static final double
-
Constructor Summary
ConstructorsConstructorDescriptionRewardsService
(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. -
Method Summary
Modifier and TypeMethodDescriptionprivate double
cachedDistance
(gpsUtil.location.Location location, gpsUtil.location.Attraction attraction) Calculates the distance between a location and an attraction with caching.void
calculateRewards
(User user, List<gpsUtil.location.Attraction> attractions) Calculates rewards for a user based on their past visits.calculateRewardsAsync
(User user, List<gpsUtil.location.Attraction> attractions) Calculates rewards for a user asynchronously (in the background).void
calculateRewardsForAllUsers
(List<User> users, List<gpsUtil.location.Attraction> attractions) Calculates rewards for a list of users in parallel.private String
getCacheKey
(gpsUtil.location.Location location, gpsUtil.location.Attraction attraction) Creates a unique key to associate a geographic location with an attraction.double
getDistance
(gpsUtil.location.Location loc1, gpsUtil.location.Location loc2) Calculates the distance between two geographic locations.int
getRewardPoints
(gpsUtil.location.Attraction attraction, User user) Returns the number of reward points earned by a user at a given attraction.boolean
isWithinAttractionProximity
(gpsUtil.location.Attraction attraction, gpsUtil.location.Location location) Check if an attraction is close enough to a location.private boolean
nearAttraction
(gpsUtil.location.VisitedLocation visitedLocation, gpsUtil.location.Attraction attraction) void
setAllUsers
(List<User> allUsers) Replaces the list of service users with a new one.void
Resets the proximity distance to its default value.void
setMaxAttractionsToCheck
(int maxAttractionsToCheck) Sets the maximum number of attractions to analyze to calculate rewards.void
setProximityBuffer
(int proximityBuffer) Changes the maximum distance to consider an attraction as close.void
Cleanly shuts down the thread pool if it is still active.
-
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
-
executor
-
MAX_THREADS
private static final int MAX_THREADS- See Also:
-
semaphore
-
maxAttractionsToCheck
private int maxAttractionsToCheck -
allUsers
-
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 servicerewardCentral
- Service to get reward pointsexecutorService
- 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
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
Calculates rewards for a user based on their past visits.- Parameters:
user
- Concerned userattractions
- 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 userattractions
- 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 listattractions
- 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 locationattraction
- 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 locationattraction
- 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 analyzelocation
- 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
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 visiteduser
- 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
-