| 1 | package edu.ucsb.cs156.happiercows.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Commons; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.User; | |
| 5 | import edu.ucsb.cs156.happiercows.entities.UserCommons; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.UserRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.services.jobs.JobContext; | |
| 10 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 11 | import edu.ucsb.cs156.happiercows.strategies.CowHealthUpdateStrategy; | |
| 12 | import lombok.AllArgsConstructor; | |
| 13 | import lombok.Getter; | |
| 14 | ||
| 15 | @AllArgsConstructor | |
| 16 | public class UpdateCowHealthJob implements JobContextConsumer { | |
| 17 | ||
| 18 | @Getter | |
| 19 | private CommonsRepository commonsRepository; | |
| 20 | @Getter | |
| 21 | private UserCommonsRepository userCommonsRepository; | |
| 22 | @Getter | |
| 23 | private UserRepository userRepository; | |
| 24 | ||
| 25 | @Override | |
| 26 | public void accept(JobContext ctx) throws Exception { | |
| 27 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Updating cow health..."); |
| 28 | ||
| 29 | Iterable<Commons> allCommons = commonsRepository.findAll(); | |
| 30 | ||
| 31 | for (Commons commons : allCommons) { | |
| 32 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Commons " + commons.getName() + ", degradationRate: " + commons.getDegradationRate() + ", carryingCapacity: " + commons.getCarryingCapacity()); |
| 33 |
1
1. lambda$accept$0 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$0 → KILLED |
int numUsers = commonsRepository.getNumUsers(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumUsers(" + commons.getId() + ")")); |
| 34 | ||
| 35 |
1
1. accept : negated conditional → KILLED |
if (numUsers==0) { |
| 36 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("No users in this commons, skipping"); |
| 37 | continue; | |
| 38 | } | |
| 39 | ||
| 40 | int carryingCapacity = commons.getCarryingCapacity(); | |
| 41 | Iterable<UserCommons> allUserCommons = userCommonsRepository.findByCommonsId(commons.getId()); | |
| 42 | ||
| 43 |
1
1. lambda$accept$1 : replaced return value with null for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::lambda$accept$1 → KILLED |
Integer totalCows = commonsRepository.getNumCows(commons.getId()).orElseThrow(() -> new RuntimeException("Error calling getNumCows(" + commons.getId() + ")")); |
| 44 | ||
| 45 |
2
1. accept : changed conditional boundary → KILLED 2. accept : negated conditional → KILLED |
var isAboveCapacity = totalCows > carryingCapacity; |
| 46 |
1
1. accept : negated conditional → KILLED |
var cowHealthUpdateStrategy = isAboveCapacity ? commons.getAboveCapacityHealthUpdateStrategy() : commons.getBelowCapacityHealthUpdateStrategy(); |
| 47 | ||
| 48 | for (UserCommons userCommons : allUserCommons) { | |
| 49 | User user = userCommons.getUser(); | |
| 50 | var newCowHealth = calculateNewCowHealthUsingStrategy(cowHealthUpdateStrategy, commons, userCommons, totalCows); | |
| 51 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("User: " + user.getFullName() + ", numCows: " + userCommons.getNumOfCows() + ", cowHealth: " + userCommons.getCowHealth()); |
| 52 | ||
| 53 | double oldHealth = userCommons.getCowHealth(); | |
| 54 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(newCowHealth); |
| 55 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateCowDeaths → KILLED |
calculateCowDeaths(userCommons, ctx); |
| 56 | ||
| 57 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" old cow health: " + oldHealth + ", new cow health: " + userCommons.getCowHealth()); |
| 58 | userCommonsRepository.save(userCommons); | |
| 59 | } | |
| 60 | } | |
| 61 | ||
| 62 |
1
1. accept : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log("Cow health has been updated!"); |
| 63 | } | |
| 64 | ||
| 65 | // exposed for testing | |
| 66 | public static double calculateNewCowHealthUsingStrategy( | |
| 67 | CowHealthUpdateStrategy strategy, | |
| 68 | Commons commons, | |
| 69 | UserCommons userCommons, | |
| 70 | int totalCows | |
| 71 | ) { | |
| 72 | var health = strategy.calculateNewCowHealth(commons, userCommons, totalCows); | |
| 73 |
1
1. calculateNewCowHealthUsingStrategy : replaced double return with 0.0d for edu/ucsb/cs156/happiercows/jobs/UpdateCowHealthJob::calculateNewCowHealthUsingStrategy → KILLED |
return Math.max(0, Math.min(health, 100)); |
| 74 | } | |
| 75 | ||
| 76 | public static void calculateCowDeaths(UserCommons userCommons, JobContext ctx) { | |
| 77 |
1
1. calculateCowDeaths : negated conditional → KILLED |
if (userCommons.getCowHealth() == 0.0) { |
| 78 |
2
1. calculateCowDeaths : Replaced integer addition with subtraction → KILLED 2. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowDeaths → KILLED |
userCommons.setCowDeaths(userCommons.getCowDeaths() + userCommons.getNumOfCows()); |
| 79 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setNumOfCows → KILLED |
userCommons.setNumOfCows(0); |
| 80 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/entities/UserCommons::setCowHealth → KILLED |
userCommons.setCowHealth(100.0); |
| 81 | ||
| 82 |
1
1. calculateCowDeaths : removed call to edu/ucsb/cs156/happiercows/services/jobs/JobContext::log → KILLED |
ctx.log(" " + userCommons.getCowDeaths() + " cows for this user died." ); |
| 83 | } | |
| 84 | } | |
| 85 | } | |
Mutations | ||
| 27 |
1.1 |
|
| 32 |
1.1 |
|
| 33 |
1.1 |
|
| 35 |
1.1 |
|
| 36 |
1.1 |
|
| 43 |
1.1 |
|
| 45 |
1.1 2.2 |
|
| 46 |
1.1 |
|
| 51 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 57 |
1.1 |
|
| 62 |
1.1 |
|
| 73 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 2.2 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |
|
| 82 |
1.1 |