code: final static String normalizeWhitespace(String orig) {
          StringBuilder sb = new StringBuilder();
          boolean lastCharWasWhitespace = false;
          boolean changed = false;
          char[] characters = orig.toCharArray();
          for (int i = 0; i < characters.length; i++) {
              if (Character.isWhitespace(characters[i])) {
                  if (lastCharWasWhitespace) {
                      // suppress character
                      changed = true;
                  } else {
                      sb.append(' ');
                      changed |= characters[i] != ' ';
                      lastCharWasWhitespace = true;
                  }
              } else {
                  sb.append(characters[i]);
                  lastCharWasWhitespace = false;
              }
          }
          return changed ? sb.toString() : orig;
      }
docs: Replace all whitespace characters with SPACE and collapse
  consecutive whitespace chars to a single SPACE.