@Test public void testRelativePath_3_indirectParent() { final String actual = new RelativePathBase("/home/john").getRelativePathFor("/home/john/documents/important"); Assert.assertEquals("./documents/important", actual); }
public String getRelativePathFor(File file) { final StringBuilder path = new StringBuilder(); if (base.equals(file)) { return settings.appendPathForBaseDir(path, file).toString(); } if (base.equals(file.getParentFile())) { return settings.appendPrefixForChildren(path, base, true).append(file.getName()).toString(); } final List<String> baseParts = FileUtil.getPathElements(base); final List<String> fileParts = FileUtil.getPathElements(file); final int len = Math.min(baseParts.size(), fileParts.size()); int common = 0; while (common < len && baseParts.get(common).equals(fileParts.get(common))) { common++; } if (common == 0) { return file.getAbsolutePath().replace('\\', '/'); } if (common < baseParts.size()) { settings.appendPrefixToCommonAncestor(path, base, baseParts.size() - common); } else { settings.appendPrefixForChildren(path, base, false); } for (int i = common; i < fileParts.size(); i++) { path.append(fileParts.get(i)).append('/'); } return path.deleteCharAt(path.length() - 1).toString(); }
RelativePathBase { public String getRelativePathFor(File file) { final StringBuilder path = new StringBuilder(); if (base.equals(file)) { return settings.appendPathForBaseDir(path, file).toString(); } if (base.equals(file.getParentFile())) { return settings.appendPrefixForChildren(path, base, true).append(file.getName()).toString(); } final List<String> baseParts = FileUtil.getPathElements(base); final List<String> fileParts = FileUtil.getPathElements(file); final int len = Math.min(baseParts.size(), fileParts.size()); int common = 0; while (common < len && baseParts.get(common).equals(fileParts.get(common))) { common++; } if (common == 0) { return file.getAbsolutePath().replace('\\', '/'); } if (common < baseParts.size()) { settings.appendPrefixToCommonAncestor(path, base, baseParts.size() - common); } else { settings.appendPrefixForChildren(path, base, false); } for (int i = common; i < fileParts.size(); i++) { path.append(fileParts.get(i)).append('/'); } return path.deleteCharAt(path.length() - 1).toString(); } }
RelativePathBase { public String getRelativePathFor(File file) { final StringBuilder path = new StringBuilder(); if (base.equals(file)) { return settings.appendPathForBaseDir(path, file).toString(); } if (base.equals(file.getParentFile())) { return settings.appendPrefixForChildren(path, base, true).append(file.getName()).toString(); } final List<String> baseParts = FileUtil.getPathElements(base); final List<String> fileParts = FileUtil.getPathElements(file); final int len = Math.min(baseParts.size(), fileParts.size()); int common = 0; while (common < len && baseParts.get(common).equals(fileParts.get(common))) { common++; } if (common == 0) { return file.getAbsolutePath().replace('\\', '/'); } if (common < baseParts.size()) { settings.appendPrefixToCommonAncestor(path, base, baseParts.size() - common); } else { settings.appendPrefixForChildren(path, base, false); } for (int i = common; i < fileParts.size(); i++) { path.append(fileParts.get(i)).append('/'); } return path.deleteCharAt(path.length() - 1).toString(); } RelativePathBase(String base);  RelativePathBase(File base);  RelativePathBase(String base, Settings settings);  RelativePathBase(File base, Settings settings); }
RelativePathBase { public String getRelativePathFor(File file) { final StringBuilder path = new StringBuilder(); if (base.equals(file)) { return settings.appendPathForBaseDir(path, file).toString(); } if (base.equals(file.getParentFile())) { return settings.appendPrefixForChildren(path, base, true).append(file.getName()).toString(); } final List<String> baseParts = FileUtil.getPathElements(base); final List<String> fileParts = FileUtil.getPathElements(file); final int len = Math.min(baseParts.size(), fileParts.size()); int common = 0; while (common < len && baseParts.get(common).equals(fileParts.get(common))) { common++; } if (common == 0) { return file.getAbsolutePath().replace('\\', '/'); } if (common < baseParts.size()) { settings.appendPrefixToCommonAncestor(path, base, baseParts.size() - common); } else { settings.appendPrefixForChildren(path, base, false); } for (int i = common; i < fileParts.size(); i++) { path.append(fileParts.get(i)).append('/'); } return path.deleteCharAt(path.length() - 1).toString(); } RelativePathBase(String base);  RelativePathBase(File base);  RelativePathBase(String base, Settings settings);  RelativePathBase(File base, Settings settings); File getBase(); Settings getSettings(); String getRelativePathFor(File file); String getRelativePathFor(String file); }
RelativePathBase { public String getRelativePathFor(File file) { final StringBuilder path = new StringBuilder(); if (base.equals(file)) { return settings.appendPathForBaseDir(path, file).toString(); } if (base.equals(file.getParentFile())) { return settings.appendPrefixForChildren(path, base, true).append(file.getName()).toString(); } final List<String> baseParts = FileUtil.getPathElements(base); final List<String> fileParts = FileUtil.getPathElements(file); final int len = Math.min(baseParts.size(), fileParts.size()); int common = 0; while (common < len && baseParts.get(common).equals(fileParts.get(common))) { common++; } if (common == 0) { return file.getAbsolutePath().replace('\\', '/'); } if (common < baseParts.size()) { settings.appendPrefixToCommonAncestor(path, base, baseParts.size() - common); } else { settings.appendPrefixForChildren(path, base, false); } for (int i = common; i < fileParts.size(); i++) { path.append(fileParts.get(i)).append('/'); } return path.deleteCharAt(path.length() - 1).toString(); } RelativePathBase(String base);  RelativePathBase(File base);  RelativePathBase(String base, Settings settings);  RelativePathBase(File base, Settings settings); File getBase(); Settings getSettings(); String getRelativePathFor(File file); String getRelativePathFor(String file); static final Settings DEFAULT; static final Settings ALL_CHILDREN_WITH_DOT_PREFIX; static final Settings CHILDREN_WITHOUT_PREFIX; }