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