code: static String normalize(String s) {
          StringBuilder sb = new StringBuilder();
          boolean changed = false;
          boolean lastCharWasWS = false;
          final int len = s.length();
          for (int i = 0; i < len; i++) {
              char c = s.charAt(i);
              if (Character.isWhitespace(c)) {
                  if (!lastCharWasWS) {
                      sb.append(SPACE);
                      changed |= c != SPACE;
                  } else {
                      changed = true;
                  }
                  lastCharWasWS = true;
              } else {
                  sb.append(c);
                  lastCharWasWS = false;
              }
          }
          return changed ? sb.toString() : s;
      }
docs: Normalize a string.
  
  <p>"normalized" in this context means all whitespace characters
  are replaced by space characters and consecutive whitespace
  characaters are collapsed.</p>