code: private static double weightedAverageSorted(double x1, double w1, double x2, double w2) {
          assert x1 <= x2;
          final double x = (x1 * w1 + x2 * w2) / (w1 + w2);
          return Math.max(x1, Math.min(x, x2));
      }
docs: Compute the weighted average between <code>x1</code> with a weight of
  <code>w1</code> and <code>x2</code> with a weight of <code>w2</code>.
  This expects <code>x1</code> to be less than or equal to <code>x2</code>
  and is guaranteed to return a number between <code>x1</code> and
  <code>x2</code>.