code: public static void normalizeAndCollapseWhitespace(final StringBuilder strBuffer) {
          WhiteSpaceState currentState = WhiteSpaceState.WORD;
          for (int i = strBuffer.length() - 1; i >= 0; i--) {
              final char currentChar = strBuffer.charAt(i);
              if (Character.isWhitespace(currentChar)) {
                  if (currentState == WhiteSpaceState.SPACE) {
                      strBuffer.delete(i, i + 1);
                  } else if (currentChar != ' ') {
                      strBuffer.replace(i, i + 1, " ");
                  }
                  currentState = WhiteSpaceState.SPACE;
              } else {
                  currentState = WhiteSpaceState.WORD;
              }
          }
      }
docs: Normalize and collapse whitespaces from string buffer.
  
  @param strBuffer The string buffer.