code: static String safeName(String name) {
        if (name == null) {
          return null;
        }
        boolean prevCharIsUnderscore = false;
        StringBuilder safeNameBuilder = new StringBuilder(name.length());
        if (!name.isEmpty() && Character.isDigit(name.charAt(0))) {
          // prevent a numeric prefix.
          safeNameBuilder.append("_");
        }
        for (char nameChar : name.toCharArray()) {
          boolean isUnsafeChar = !JmxCollector.isLegalCharacter(nameChar);
          if ((isUnsafeChar || nameChar == '_')) {
            if (prevCharIsUnderscore) {
              continue;
            } else {
              safeNameBuilder.append("_");
              prevCharIsUnderscore = true;
            }
          } else {
            safeNameBuilder.append(nameChar);
            prevCharIsUnderscore = false;
          }
        }
  
        return safeNameBuilder.toString();
      }
docs: Change invalid chars to underscore, and merge underscores.
  @param name Input string
  @return