code: public static String truncate(String text, Integer maxCharacters, Boolean truncateWords) {
  
          if (isNotBlank(text)) {
              StringBuilder truncatedTextBuilder = new StringBuilder();
              if (text.length() > maxCharacters) {
                  if (truncateWords) {
  
                      // The words are truncated and the ellipsis is added when is possible
                      if (maxCharacters <= ELLIPSIS.length()) {
                          truncatedTextBuilder.append(text.substring(0, maxCharacters));
                      } else {
                          truncatedTextBuilder.append(text.substring(0, maxCharacters - ELLIPSIS.length()));
                          truncatedTextBuilder.append(ELLIPSIS);
                      }
                  } else {
  
                      // The words are not truncated and the ellipsis is not added
                      List<String> words = Lists.newArrayList(text.split(SPACE));
                      Iterator<String> it = words.iterator();
                      int usedChars = 0;
                      Boolean exit = false;
                      while (it.hasNext() && !exit) {
                          String word = it.next();
                          int increment = usedChars == 0 ? word.length() : word.length() + 1;
                          if ((usedChars + increment) <= maxCharacters) {
                              truncatedTextBuilder.append(usedChars == 0 ? word : SPACE + word);
                              usedChars += increment;
                          } else {
                              exit = true;
                          }
                      }
                  }
              } else {
                  truncatedTextBuilder.append(text);
              }
              return truncatedTextBuilder.toString();
          }
          return text;
      }
docs: Truncate the text adding "..." if the truncateWords parameter is true. The ellipsis will be taken into account
  when counting the amount of characters.
  
  @param text          The text to truncate
  @param maxCharacters The maximum amount of characters allowed for the returned text
  @param truncateWords True if the words should be truncated
  @return The truncated text