@Test public void testGetCanonicalPath() { Path instance = new Path("/usr/./../usr/local/../g.txt"); Path expResult = new Path("/usr/g.txt"); Path result = instance.getCanonicalPath(); assertEquals(expResult, result); }
public Path getCanonicalPath() { if(isRoot()) return this; List<String> str = new LinkedList<>(); for (String pe : pathElements) { if (pe.equalsIgnoreCase("..")) { if (str.isEmpty()) return null; str.remove(str.size() - 1); } else if (!pe.equalsIgnoreCase(".")) { str.add(pe); } } if (str.isEmpty()) return null; String[] els = new String[str.size()]; return new Path(str.toArray(els)); }
Path implements Iterable<Path> { public Path getCanonicalPath() { if(isRoot()) return this; List<String> str = new LinkedList<>(); for (String pe : pathElements) { if (pe.equalsIgnoreCase("..")) { if (str.isEmpty()) return null; str.remove(str.size() - 1); } else if (!pe.equalsIgnoreCase(".")) { str.add(pe); } } if (str.isEmpty()) return null; String[] els = new String[str.size()]; return new Path(str.toArray(els)); } }
Path implements Iterable<Path> { public Path getCanonicalPath() { if(isRoot()) return this; List<String> str = new LinkedList<>(); for (String pe : pathElements) { if (pe.equalsIgnoreCase("..")) { if (str.isEmpty()) return null; str.remove(str.size() - 1); } else if (!pe.equalsIgnoreCase(".")) { str.add(pe); } } if (str.isEmpty()) return null; String[] els = new String[str.size()]; return new Path(str.toArray(els)); } Path();  Path(String path); private  Path(String[] pathElements); }
Path implements Iterable<Path> { public Path getCanonicalPath() { if(isRoot()) return this; List<String> str = new LinkedList<>(); for (String pe : pathElements) { if (pe.equalsIgnoreCase("..")) { if (str.isEmpty()) return null; str.remove(str.size() - 1); } else if (!pe.equalsIgnoreCase(".")) { str.add(pe); } } if (str.isEmpty()) return null; String[] els = new String[str.size()]; return new Path(str.toArray(els)); } Path();  Path(String path); private  Path(String[] pathElements); String[] getPathElements(); String getFirstElement(); String getName(); Path getParent(); Path getNext(); boolean hasNext(); boolean isRoot(); boolean isSelf(); boolean isParent(); boolean isLast(); Path getCanonicalPath(); @Override String toString(); String toString(String pathSep); Path join(Path path); Path join(String path); @Override Iterator<Path> iterator(); String getExtension(); @Override int hashCode(); @Override boolean equals(Object obj); boolean startsWith(Path path); Path leftTrim(Path path); }
Path implements Iterable<Path> { public Path getCanonicalPath() { if(isRoot()) return this; List<String> str = new LinkedList<>(); for (String pe : pathElements) { if (pe.equalsIgnoreCase("..")) { if (str.isEmpty()) return null; str.remove(str.size() - 1); } else if (!pe.equalsIgnoreCase(".")) { str.add(pe); } } if (str.isEmpty()) return null; String[] els = new String[str.size()]; return new Path(str.toArray(els)); } Path();  Path(String path); private  Path(String[] pathElements); String[] getPathElements(); String getFirstElement(); String getName(); Path getParent(); Path getNext(); boolean hasNext(); boolean isRoot(); boolean isSelf(); boolean isParent(); boolean isLast(); Path getCanonicalPath(); @Override String toString(); String toString(String pathSep); Path join(Path path); Path join(String path); @Override Iterator<Path> iterator(); String getExtension(); @Override int hashCode(); @Override boolean equals(Object obj); boolean startsWith(Path path); Path leftTrim(Path path);  }