public static boolean isRestartRecommended(
      ProcessingUnit pu,
      Machine source,
      Machine target,
      Fraction optimalCpuCoresPerPrimary,
      CapacityRequirementsPerAgent allocatedCapacity) {

    boolean isRestartRecommended = false;
    final int numberOfPrimaryInstancesOnSource = getNumberOfPrimaryInstancesOnMachine(pu, source);
    if (numberOfPrimaryInstancesOnSource > 0) {

      final int numberOfPrimaryInstancesOnTarget = getNumberOfPrimaryInstancesOnMachine(pu, target);
      Fraction cpuCoresOnSource = getNumberOfCpuCores(source, allocatedCapacity);
      Fraction cpuCoresOnTarget = getNumberOfCpuCores(target, allocatedCapacity);
      final Fraction missingCpuCoresBeforeRestart =
          max(
                  Fraction.ZERO,
                  optimalCpuCoresPerPrimary
                      .multiply(numberOfPrimaryInstancesOnSource)
                      .subtract(cpuCoresOnSource))
              .add(
                  max(
                      Fraction.ZERO,
                      optimalCpuCoresPerPrimary
                          .multiply(numberOfPrimaryInstancesOnTarget)
                          .subtract(cpuCoresOnTarget)));

      final Fraction missingCpuCoresAfterRestart =
          max(
                  Fraction.ZERO,
                  optimalCpuCoresPerPrimary
                      .multiply(numberOfPrimaryInstancesOnSource - 1)
                      .subtract(cpuCoresOnSource))
              .add(
                  max(
                      Fraction.ZERO,
                      optimalCpuCoresPerPrimary
                          .multiply(numberOfPrimaryInstancesOnTarget + 1)
                          .subtract(cpuCoresOnTarget)));

      isRestartRecommended =
          missingCpuCoresAfterRestart.compareTo(missingCpuCoresBeforeRestart) < 0;
    }

    return isRestartRecommended;
  }
 private static Fraction max(Fraction a, Fraction b) {
   if (b.compareTo(a) > 0) {
     return b;
   }
   return a;
 }