@Test public void testRelativize_oneAbsoluteOneRelative() { try { pathService.parsePath("/foo/bar").relativize(pathService.parsePath("foo")); fail(); } catch (IllegalArgumentException expected) { } try { pathService.parsePath("foo").relativize(pathService.parsePath("/foo/bar")); fail(); } catch (IllegalArgumentException expected) { } }
@Override public JimfsPath relativize(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } checkArgument( Objects.equals(root, otherPath.root), "Paths have different roots: %s, %s", this, other); if (equals(other)) { return pathService.emptyPath(); } if (isEmptyPath()) { return otherPath; } ImmutableList<Name> otherNames = otherPath.names; int sharedSubsequenceLength = 0; for (int i = 0; i < Math.min(getNameCount(), otherNames.size()); i++) { if (names.get(i).equals(otherNames.get(i))) { sharedSubsequenceLength++; } else { break; } } int extraNamesInThis = Math.max(0, getNameCount() - sharedSubsequenceLength); ImmutableList<Name> extraNamesInOther = (otherNames.size() <= sharedSubsequenceLength) ? ImmutableList.<Name>of() : otherNames.subList(sharedSubsequenceLength, otherNames.size()); List<Name> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size()); parts.addAll(Collections.nCopies(extraNamesInThis, Name.PARENT)); parts.addAll(extraNamesInOther); return pathService.createRelativePath(parts); }
JimfsPath implements Path { @Override public JimfsPath relativize(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } checkArgument( Objects.equals(root, otherPath.root), "Paths have different roots: %s, %s", this, other); if (equals(other)) { return pathService.emptyPath(); } if (isEmptyPath()) { return otherPath; } ImmutableList<Name> otherNames = otherPath.names; int sharedSubsequenceLength = 0; for (int i = 0; i < Math.min(getNameCount(), otherNames.size()); i++) { if (names.get(i).equals(otherNames.get(i))) { sharedSubsequenceLength++; } else { break; } } int extraNamesInThis = Math.max(0, getNameCount() - sharedSubsequenceLength); ImmutableList<Name> extraNamesInOther = (otherNames.size() <= sharedSubsequenceLength) ? ImmutableList.<Name>of() : otherNames.subList(sharedSubsequenceLength, otherNames.size()); List<Name> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size()); parts.addAll(Collections.nCopies(extraNamesInThis, Name.PARENT)); parts.addAll(extraNamesInOther); return pathService.createRelativePath(parts); } }
JimfsPath implements Path { @Override public JimfsPath relativize(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } checkArgument( Objects.equals(root, otherPath.root), "Paths have different roots: %s, %s", this, other); if (equals(other)) { return pathService.emptyPath(); } if (isEmptyPath()) { return otherPath; } ImmutableList<Name> otherNames = otherPath.names; int sharedSubsequenceLength = 0; for (int i = 0; i < Math.min(getNameCount(), otherNames.size()); i++) { if (names.get(i).equals(otherNames.get(i))) { sharedSubsequenceLength++; } else { break; } } int extraNamesInThis = Math.max(0, getNameCount() - sharedSubsequenceLength); ImmutableList<Name> extraNamesInOther = (otherNames.size() <= sharedSubsequenceLength) ? ImmutableList.<Name>of() : otherNames.subList(sharedSubsequenceLength, otherNames.size()); List<Name> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size()); parts.addAll(Collections.nCopies(extraNamesInThis, Name.PARENT)); parts.addAll(extraNamesInOther); return pathService.createRelativePath(parts); } JimfsPath(PathService pathService, @NullableDecl Name root, Iterable<Name> names); }
JimfsPath implements Path { @Override public JimfsPath relativize(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } checkArgument( Objects.equals(root, otherPath.root), "Paths have different roots: %s, %s", this, other); if (equals(other)) { return pathService.emptyPath(); } if (isEmptyPath()) { return otherPath; } ImmutableList<Name> otherNames = otherPath.names; int sharedSubsequenceLength = 0; for (int i = 0; i < Math.min(getNameCount(), otherNames.size()); i++) { if (names.get(i).equals(otherNames.get(i))) { sharedSubsequenceLength++; } else { break; } } int extraNamesInThis = Math.max(0, getNameCount() - sharedSubsequenceLength); ImmutableList<Name> extraNamesInOther = (otherNames.size() <= sharedSubsequenceLength) ? ImmutableList.<Name>of() : otherNames.subList(sharedSubsequenceLength, otherNames.size()); List<Name> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size()); parts.addAll(Collections.nCopies(extraNamesInThis, Name.PARENT)); parts.addAll(extraNamesInOther); return pathService.createRelativePath(parts); } JimfsPath(PathService pathService, @NullableDecl Name root, Iterable<Name> names); @NullableDecl Name root(); ImmutableList<Name> names(); @NullableDecl Name name(); boolean isEmptyPath(); @Override FileSystem getFileSystem(); JimfsFileSystem getJimfsFileSystem(); @Override boolean isAbsolute(); @Override JimfsPath getRoot(); @Override JimfsPath getFileName(); @Override JimfsPath getParent(); @Override int getNameCount(); @Override JimfsPath getName(int index); @Override JimfsPath subpath(int beginIndex, int endIndex); @Override boolean startsWith(Path other); @Override boolean startsWith(String other); @Override boolean endsWith(Path other); @Override boolean endsWith(String other); @Override JimfsPath normalize(); @Override JimfsPath resolve(Path other); @Override JimfsPath resolve(String other); @Override JimfsPath resolveSibling(Path other); @Override JimfsPath resolveSibling(String other); @Override JimfsPath relativize(Path other); @Override JimfsPath toAbsolutePath(); @Override JimfsPath toRealPath(LinkOption... options); @Override WatchKey register(
      WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers); @Override WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events); @Override URI toUri(); @Override File toFile(); @Override Iterator<Path> iterator(); @Override int compareTo(Path other); @Override boolean equals(@NullableDecl Object obj); @Override int hashCode(); @Override String toString(); }
JimfsPath implements Path { @Override public JimfsPath relativize(Path other) { JimfsPath otherPath = checkPath(other); if (otherPath == null) { throw new ProviderMismatchException(other.toString()); } checkArgument( Objects.equals(root, otherPath.root), "Paths have different roots: %s, %s", this, other); if (equals(other)) { return pathService.emptyPath(); } if (isEmptyPath()) { return otherPath; } ImmutableList<Name> otherNames = otherPath.names; int sharedSubsequenceLength = 0; for (int i = 0; i < Math.min(getNameCount(), otherNames.size()); i++) { if (names.get(i).equals(otherNames.get(i))) { sharedSubsequenceLength++; } else { break; } } int extraNamesInThis = Math.max(0, getNameCount() - sharedSubsequenceLength); ImmutableList<Name> extraNamesInOther = (otherNames.size() <= sharedSubsequenceLength) ? ImmutableList.<Name>of() : otherNames.subList(sharedSubsequenceLength, otherNames.size()); List<Name> parts = new ArrayList<>(extraNamesInThis + extraNamesInOther.size()); parts.addAll(Collections.nCopies(extraNamesInThis, Name.PARENT)); parts.addAll(extraNamesInOther); return pathService.createRelativePath(parts); } JimfsPath(PathService pathService, @NullableDecl Name root, Iterable<Name> names); @NullableDecl Name root(); ImmutableList<Name> names(); @NullableDecl Name name(); boolean isEmptyPath(); @Override FileSystem getFileSystem(); JimfsFileSystem getJimfsFileSystem(); @Override boolean isAbsolute(); @Override JimfsPath getRoot(); @Override JimfsPath getFileName(); @Override JimfsPath getParent(); @Override int getNameCount(); @Override JimfsPath getName(int index); @Override JimfsPath subpath(int beginIndex, int endIndex); @Override boolean startsWith(Path other); @Override boolean startsWith(String other); @Override boolean endsWith(Path other); @Override boolean endsWith(String other); @Override JimfsPath normalize(); @Override JimfsPath resolve(Path other); @Override JimfsPath resolve(String other); @Override JimfsPath resolveSibling(Path other); @Override JimfsPath resolveSibling(String other); @Override JimfsPath relativize(Path other); @Override JimfsPath toAbsolutePath(); @Override JimfsPath toRealPath(LinkOption... options); @Override WatchKey register(
      WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers); @Override WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events); @Override URI toUri(); @Override File toFile(); @Override Iterator<Path> iterator(); @Override int compareTo(Path other); @Override boolean equals(@NullableDecl Object obj); @Override int hashCode(); @Override String toString();  }