code: public static URI setFragment(final URI path, final String fragment) {
          try {
              if (path.getPath() != null) {
                  return new URI(path.getScheme(), path.getUserInfo(), path.getHost(), path.getPort(), path.getPath(), path.getQuery(), fragment);
              } else {
                  return new URI(path.getScheme(), path.getSchemeSpecificPart(), fragment);
              }
          } catch (final URISyntaxException e) {
              throw new RuntimeException(e.getMessage(), e);
          }
      }
docs: Create new URI with a given fragment.
  
  @param path URI to set fragment on
  @param fragment new fragment, {@code null} for no fragment
  @return new URI instance with given fragment