class FilepathUtils {

  public static String getCanonicalForm(final String path) {
    final String[] fragments = getCanonicalFragments(path);
    if (0 == fragments.length) {
      if (path.startsWith(DIRECTORY_SEPARATOR)) {
        return DIRECTORY_SEPARATOR;
      } else {
        return path;
      }
    }
    final StringBuilder builder = new StringBuilder();
    if (path.startsWith(DIRECTORY_SEPARATOR)) {
      builder.append(DIRECTORY_SEPARATOR);
    }
    for (int i = 0; i < fragments.length; ++i) {
      if (0 != i) {
        builder.append(DIRECTORY_SEPARATOR);
      }
      builder.append(fragments[i]);
    }
    return builder.toString();
  }

}