public Path makeAbsolute() { if (isAbsolute()) { return this; } Path result = new Path(segments, separators | HAS_LEADING); // may need canonicalizing if it has leading ".." or "." segments if (result.segmentCount() > 0) { String first = result.segment(0); if (first.equals("..") || first.equals(".")) { //$NON-NLS-1$ //$NON-NLS-2$ result.canonicalize(); } } return result; }
/** * Initializes the current path with the given string. */ private Path initialize(String path) { assert path != null; path = collapseSlashes(path); int len = path.length(); // compute the separators array if (len < 2) { if (len == 1 && path.charAt(0) == SEPARATOR) { separators = HAS_LEADING; } else { separators = 0; } } else { boolean hasLeading = path.charAt(0) == SEPARATOR; boolean hasTrailing = path.charAt(len - 1) == SEPARATOR; separators = hasLeading ? HAS_LEADING : 0; if (hasTrailing) { separators |= HAS_TRAILING; } } // compute segments and ensure canonical form segments = computeSegments(path); if (!canonicalize()) { // compute hash now because canonicalize didn't need to do it separators = (separators & ALL_SEPARATORS) | (computeHashCode() << USED_BITS); } return this; }
public Path append(Path tail) { // optimize some easy cases if (tail == null || tail.segmentCount() == 0) { return this; } if (isEmpty()) { return tail.makeRelative(); } if (isRoot()) { return tail.makeAbsolute(); } // concatenate the two segment arrays int myLen = segments.length; int tailLen = tail.segmentCount(); String[] newSegments = new String[myLen + tailLen]; System.arraycopy(segments, 0, newSegments, 0, myLen); for (int i = 0; i < tailLen; i++) { newSegments[myLen + i] = tail.segment(i); } // use my leading separators and the tail's trailing separator Path result = new Path(newSegments, (separators & HAS_LEADING) | (tail.hasTrailingSeparator() ? HAS_TRAILING : 0)); String tailFirstSegment = newSegments[myLen]; if (tailFirstSegment.equals("..") || tailFirstSegment.equals(".")) { //$NON-NLS-1$ //$NON-NLS-2$ result.canonicalize(); } return result; }