/** * "Разрешает" ссылку. <br> * Примеры: <br> * * <table> * <tr> * <td>current dir</td> * <td>this</td> * <td>link</td> * <td>result</td> * </tr> * * <tr> * <td>/opt/jdk/default</td> * <td>/opt/jdk/default</td> * <td>jdk1.8.0_05</td> * <td>/opt/jdk/jdk1.8.0_05</td> * </tr> * * <tr> * <td>/opt/jdk/default</td> * <td>/opt/jdk/default</td> * <td>./jdk1.8.0_05</td> * <td>/opt/jdk/jdk1.8.0_05</td> * </tr> * * <tr> * <td>/opt/jdk/default</td> * <td>/opt/jdk/default</td> * <td>../jdk1.8.0_05</td> * <td>/opt/jdk1.8.0_05</td> * </tr> * * <tr> * <td>/opt/jdk/default</td> * <td>/opt/jdk/default</td> * <td>../../jdk1.8.0_05</td> * <td>/jdk1.8.0_05</td> * </tr> * * <tr> * <td>/opt/jdk/default</td> * <td>/opt/jdk/default</td> * <td>../../../../jdk1.8.0_05</td> * <td>/jdk1.8.0_05</td> * </tr> * * </table> * * @param link куда указывает ссылка * @param currentDir текущий каталог * @return цель */ public UnixPath resolveLink(UnixPath link, UnixPath currentDir) { if (currentDir == null) throw new IllegalArgumentException("currentDir==null"); if (link == null) throw new IllegalArgumentException("path==null"); if (link.isAbsolute()) return link; UnixPath p = !isAbsolute() ? getAbsolute(currentDir) : this; ArrayList<String> cmpts = new ArrayList<String>(); cmpts.addAll(Arrays.asList(p.getComponents())); if (cmpts.size() > 0) cmpts.remove(cmpts.size() - 1); for (String cmpt : link.getComponents()) { if (cmpt.equals(current)) continue; if (cmpt.equals(empty)) continue; if (cmpt.equals(parent)) { if (cmpts.size() > 1) { cmpts.remove(cmpts.size() - 1); continue; } else { if (resolveLink_allowRetNull()) { return null; } else { continue; } } } cmpts.add(cmpt); } return new UnixPath(cmpts.toArray(new String[] {})); }
/** * Возвращает абсолютный путь относительно текущего каталога * * @param currentDir текущий каталог * @return абсолютный путь */ public UnixPath getAbsolute(UnixPath currentDir) { if (this.isAbsolute()) return this; if (currentDir == null) throw new IllegalArgumentException("currentDir==null"); if (!currentDir.isAbsolute()) throw new IllegalArgumentException("!currentDir.isAbsolute()"); // ArrayList<String> cmpts = new ArrayList<String>(); // cmpts.addAll(Arrays.asList(currentDir.getComponents())); // for( String cmpt : components ){ // if( cmpt.equals(empty) )continue; // if( cmpt.equals(current) )continue; // if( cmpt.equals(parent) ){ // if( cmpts.size()>1 ){ // cmpts.remove(cmpts.size()-1); // continue; // }else{ // if( getAbsolute_allowRetNull() ){ // return null; // }else{ // continue; // } // } // } // cmpts.add(cmpt); // } // return new UnixPath(cmpts.toArray(new String[]{})); ArrayList<String> cmpts = new ArrayList<String>(); for (String c : currentDir.getComponents()) { cmpts.add(c); } for (String c : this.getComponents()) { cmpts.add(c); } return new UnixPath(cmpts.toArray(new String[] {})); }
public String getName() { if (components.length == 0) return ""; if (components.length == 1) return components[0]; UnixPath canon = getCanonical(); if (canon != null) { String[] cmpts = canon.getComponents(); if (cmpts != null && cmpts.length > 0) return cmpts[cmpts.length - 1]; } return components[components.length - 1]; }
@Override public int compareTo(UnixPath o) { String p1 = toString(); String p2 = o.toString(); return p1.compareTo(p2); }