code: private static int upper_bound(int first, int last, int x, IntComparator comp) {
  	//if (comp==null) throw new NullPointerException();
  	int len = last - first;
  	while (len > 0) {
  		int half = len / 2;
  		int middle = first + half;
  		if (comp.compare(x, middle)<0) {
  			len = half;
  		}
  		else {
  			first = middle + 1;
  			len -= half + 1;
  		}
  	}
  	return first;
  }
docs: Performs a binary search on an already-sorted range: finds the last
  position where an element can be inserted without violating the ordering.
  Sorting is by a user-supplied comparison function.
  @param array    Array containing the range.
  @param first    Beginning of the range.
  @param last     One past the end of the range.
  @param x        Element to be searched for.
  @param comp     Comparison function.
  @return         The largest index i such that, for every j in the
  range <code>[first, i)</code>,
  <code>comp.apply(x, array[j])</code> is
  <code>false</code>.
  @see Sorting#lower_bound
  @see Sorting#equal_range
  @see Sorting#binary_search