TSK-1064: refactored PrioDurationHolder -> generic Pair

This commit is contained in:
Mustapha Zorgati 2020-01-30 17:56:43 +01:00
parent 6c73ed18f2
commit a5b3254c8d
2 changed files with 62 additions and 55 deletions

View File

@ -0,0 +1,30 @@
package pro.taskana.common.internal.util;
public class Pair<L, R> {
private final L left;
private final R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
public static <L, R> Pair<L, R> of(L left, R right) {
return new Pair<>(left, right);
}
@Override
public String toString() {
return "Pair [left=" + left + ", right=" + right + "]";
}
}

View File

@ -35,6 +35,7 @@ import pro.taskana.common.internal.InternalTaskanaEngine;
import pro.taskana.common.internal.security.CurrentUserContext; import pro.taskana.common.internal.security.CurrentUserContext;
import pro.taskana.common.internal.util.DaysToWorkingDaysConverter; import pro.taskana.common.internal.util.DaysToWorkingDaysConverter;
import pro.taskana.common.internal.util.IdGenerator; import pro.taskana.common.internal.util.IdGenerator;
import pro.taskana.common.internal.util.Pair;
import pro.taskana.history.api.events.task.ClaimCancelledEvent; import pro.taskana.history.api.events.task.ClaimCancelledEvent;
import pro.taskana.history.api.events.task.ClaimedEvent; import pro.taskana.history.api.events.task.ClaimedEvent;
import pro.taskana.history.api.events.task.CompletedEvent; import pro.taskana.history.api.events.task.CompletedEvent;
@ -709,7 +710,7 @@ public class TaskServiceImpl implements TaskService {
if (newClassificationSummary.getServiceLevel() == null) { if (newClassificationSummary.getServiceLevel() == null) {
return null; return null;
} }
Duration minDuration = prioDurationFromAttachments.getDuration(); Duration minDuration = prioDurationFromAttachments.getLeft();
Duration durationFromClassification = Duration durationFromClassification =
Duration.parse(newClassificationSummary.getServiceLevel()); Duration.parse(newClassificationSummary.getServiceLevel());
if (minDuration != null) { if (minDuration != null) {
@ -1000,7 +1001,9 @@ public class TaskServiceImpl implements TaskService {
} }
private void standardSettings( private void standardSettings(
TaskImpl task, Classification classification, PrioDurationHolder prioDurationFromAttachments) TaskImpl task,
Classification classification,
PrioDurationHolder prioDurationFromAttachments)
throws InvalidArgumentException { throws InvalidArgumentException {
LOGGER.debug("entry to standardSettings()"); LOGGER.debug("entry to standardSettings()");
final Instant now = Instant.now(); final Instant now = Instant.now();
@ -1039,7 +1042,7 @@ public class TaskServiceImpl implements TaskService {
prioDurationFromAttachments, prioDurationFromAttachments,
classification.getPriority(), classification.getPriority(),
classification.getServiceLevel()); classification.getServiceLevel());
Duration finalDuration = finalPrioDuration.getDuration(); Duration finalDuration = finalPrioDuration.getLeft();
if (finalDuration != null && !MAX_DURATION.equals(finalDuration)) { if (finalDuration != null && !MAX_DURATION.equals(finalDuration)) {
// if we have a due date we need to go x days backwards, // if we have a due date we need to go x days backwards,
// else we take the planned date (or now as fallback) and add x Days // else we take the planned date (or now as fallback) and add x Days
@ -1060,7 +1063,7 @@ public class TaskServiceImpl implements TaskService {
task.setDue(due); task.setDue(due);
} }
} }
task.setPriority(finalPrioDuration.getPrio()); task.setPriority(finalPrioDuration.getRight());
} }
if (task.getName() == null && classification != null) { if (task.getName() == null && classification != null) {
@ -1499,8 +1502,8 @@ public class TaskServiceImpl implements TaskService {
actualPrioDuration = handleNonNullAttachment(actualPrioDuration, attachment); actualPrioDuration = handleNonNullAttachment(actualPrioDuration, attachment);
} }
} }
if (MAX_DURATION.equals(actualPrioDuration.getDuration())) { if (MAX_DURATION.equals(actualPrioDuration.getLeft())) {
actualPrioDuration.setDuration(null); actualPrioDuration = new PrioDurationHolder(null, actualPrioDuration.getRight());
} }
LOGGER.debug("exit from handleAttachments(), returning {}", actualPrioDuration); LOGGER.debug("exit from handleAttachments(), returning {}", actualPrioDuration);
@ -1578,7 +1581,9 @@ public class TaskServiceImpl implements TaskService {
} }
private void updateClassificationRelatedProperties( private void updateClassificationRelatedProperties(
TaskImpl oldTaskImpl, TaskImpl newTaskImpl, PrioDurationHolder prioDurationFromAttachments) TaskImpl oldTaskImpl,
TaskImpl newTaskImpl,
PrioDurationHolder prioDurationFromAttachments)
throws ClassificationNotFoundException { throws ClassificationNotFoundException {
LOGGER.debug("entry to updateClassificationRelatedProperties()"); LOGGER.debug("entry to updateClassificationRelatedProperties()");
// insert Classification specifications if Classification is given. // insert Classification specifications if Classification is given.
@ -1637,7 +1642,7 @@ public class TaskServiceImpl implements TaskService {
} }
int newPriority = int newPriority =
Math.max(newClassificationSummary.getPriority(), prioDurationFromAttachments.getPrio()); Math.max(newClassificationSummary.getPriority(), prioDurationFromAttachments.getRight());
newTaskImpl.setPriority(newPriority); newTaskImpl.setPriority(newPriority);
LOGGER.debug("exit from updateTaskPrioDurationFromClassification()"); LOGGER.debug("exit from updateTaskPrioDurationFromClassification()");
} }
@ -1668,8 +1673,8 @@ public class TaskServiceImpl implements TaskService {
// DELETE, when an Attachment was only represented before // DELETE, when an Attachment was only represented before
deleteAttachmentOnTaskUpdate(oldTaskImpl, newTaskImpl); deleteAttachmentOnTaskUpdate(oldTaskImpl, newTaskImpl);
if (MAX_DURATION.equals(prioDuration.getDuration())) { if (MAX_DURATION.equals(prioDuration.getLeft())) {
prioDuration.setDuration(null); prioDuration = new PrioDurationHolder(null, prioDuration.getRight());
} }
LOGGER.debug("exit from handleAttachmentsOnTaskUpdate()"); LOGGER.debug("exit from handleAttachmentsOnTaskUpdate()");
@ -1810,8 +1815,8 @@ public class TaskServiceImpl implements TaskService {
} }
} }
} }
if (MAX_DURATION.equals(prioDuration.getDuration())) { if (MAX_DURATION.equals(prioDuration.getLeft())) {
prioDuration.setDuration(null); prioDuration = new PrioDurationHolder(null, prioDuration.getRight());
} }
LOGGER.debug("exit from handleAttachmentsOnClassificationUpdate(), returning {}", prioDuration); LOGGER.debug("exit from handleAttachmentsOnClassificationUpdate(), returning {}", prioDuration);
@ -1828,13 +1833,13 @@ public class TaskServiceImpl implements TaskService {
prioDurationHolder, prioDurationHolder,
prioFromClassification, prioFromClassification,
serviceLevelFromClassification); serviceLevelFromClassification);
Duration minDuration = prioDurationHolder.getDuration(); Duration minDuration = prioDurationHolder.getLeft();
int maxPrio = prioDurationHolder.getPrio(); int maxPrio = prioDurationHolder.getRight();
if (serviceLevelFromClassification != null) { if (serviceLevelFromClassification != null) {
Duration currentDuration = Duration.parse(serviceLevelFromClassification); Duration currentDuration = Duration.parse(serviceLevelFromClassification);
if (prioDurationHolder.getDuration() != null) { if (prioDurationHolder.getLeft() != null) {
if (prioDurationHolder.getDuration().compareTo(currentDuration) > 0) { if (prioDurationHolder.getLeft().compareTo(currentDuration) > 0) {
minDuration = currentDuration; minDuration = currentDuration;
} }
} else { } else {
@ -1845,10 +1850,9 @@ public class TaskServiceImpl implements TaskService {
maxPrio = prioFromClassification; maxPrio = prioFromClassification;
} }
LOGGER.debug( PrioDurationHolder pair = new PrioDurationHolder(minDuration, maxPrio);
"exit from getNewPrioDuration(), returning {}", LOGGER.debug("exit from getNewPrioDuration(), returning {}", pair);
new PrioDurationHolder(minDuration, maxPrio)); return pair;
return new PrioDurationHolder(minDuration, maxPrio);
} }
private void initAttachment(AttachmentImpl attachment, Task newTask) { private void initAttachment(AttachmentImpl attachment, Task newTask) {
@ -1899,7 +1903,7 @@ public class TaskServiceImpl implements TaskService {
} }
int newPriority = int newPriority =
Math.max(classificationSummary.getPriority(), prioDurationFromAttachments.getPrio()); Math.max(classificationSummary.getPriority(), prioDurationFromAttachments.getRight());
task.setPriority(newPriority); task.setPriority(newPriority);
LOGGER.debug("exit from updateTaskPrioDurationFromClassificationAndAttachments()"); LOGGER.debug("exit from updateTaskPrioDurationFromClassificationAndAttachments()");
} }
@ -1907,15 +1911,15 @@ public class TaskServiceImpl implements TaskService {
private void updateTaskPrioDurationFromAttachments( private void updateTaskPrioDurationFromAttachments(
TaskImpl task, PrioDurationHolder prioDurationFromAttachments) { TaskImpl task, PrioDurationHolder prioDurationFromAttachments) {
LOGGER.debug("entry to updateTaskPrioDurationFromAttachments()"); LOGGER.debug("entry to updateTaskPrioDurationFromAttachments()");
if (prioDurationFromAttachments.getDuration() != null) { if (prioDurationFromAttachments.getLeft() != null) {
long days = long days =
converter.convertWorkingDaysToDays( converter.convertWorkingDaysToDays(
task.getPlanned(), prioDurationFromAttachments.getDuration().toDays()); task.getPlanned(), prioDurationFromAttachments.getLeft().toDays());
Instant due = task.getPlanned().plus(Duration.ofDays(days)); Instant due = task.getPlanned().plus(Duration.ofDays(days));
task.setDue(due); task.setDue(due);
} }
if (prioDurationFromAttachments.getPrio() > Integer.MIN_VALUE) { if (prioDurationFromAttachments.getRight() > Integer.MIN_VALUE) {
task.setPriority(prioDurationFromAttachments.getPrio()); task.setPriority(prioDurationFromAttachments.getRight());
} }
LOGGER.debug("exit from updateTaskPrioDurationFromAttachments()"); LOGGER.debug("exit from updateTaskPrioDurationFromAttachments()");
} }
@ -1966,37 +1970,10 @@ public class TaskServiceImpl implements TaskService {
taskSummaries.forEach(task -> historyEventProducer.createEvent(new CompletedEvent(task))); taskSummaries.forEach(task -> historyEventProducer.createEvent(new CompletedEvent(task)));
} }
/** private static class PrioDurationHolder extends Pair<Duration, Integer> {
* hold a pair of priority and Duration.
*
* @author bbr
*/
static class PrioDurationHolder {
private Duration duration; public PrioDurationHolder(Duration left, Integer right) {
super(left, right);
private int prio;
PrioDurationHolder(Duration duration, int prio) {
this.duration = duration;
this.prio = prio;
}
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
public int getPrio() {
return prio;
}
@Override
public String toString() {
return "PrioDurationHolder [duration=" + duration + ", prio=" + prio + "]";
} }
} }
} }