kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.util; kitaev@213: kitaev@213: /** kitaev@213: * Identify repository files (not String nor io.File). Convenient for pattern matching. Memory-friendly. kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public final class Path implements CharSequence, Comparable/*Cloneable? - although clone for paths make no sense*/{ kitaev@213: // private String[] segments; kitaev@213: // private int flags; // dir, unparsed kitaev@213: private String path; kitaev@213: kitaev@213: /*package-local*/Path(String p) { kitaev@213: path = p; kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * Check if this is directory's path. kitaev@213: * Note, this method doesn't perform any file system operation. kitaev@213: * kitaev@213: * @return true when this path points to a directory kitaev@213: */ kitaev@213: public boolean isDirectory() { kitaev@213: // XXX simple logic for now. Later we may decide to have an explicit factory method to create directory paths kitaev@213: return path.charAt(path.length() - 1) == '/'; kitaev@213: } kitaev@213: kitaev@213: public int length() { kitaev@213: return path.length(); kitaev@213: } kitaev@213: kitaev@213: public char charAt(int index) { kitaev@213: return path.charAt(index); kitaev@213: } kitaev@213: kitaev@213: public CharSequence subSequence(int start, int end) { kitaev@213: // new Path if start-end matches boundaries of any subpath kitaev@213: return path.substring(start, end); kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public String toString() { kitaev@213: return path; // CharSequence demands toString() impl kitaev@213: } kitaev@213: kitaev@213: public int compareTo(Path o) { kitaev@213: return path.compareTo(o.path); kitaev@213: } kitaev@213: kitaev@213: @Override kitaev@213: public boolean equals(Object obj) { kitaev@213: if (obj != null && getClass() == obj.getClass()) { kitaev@213: return this == obj || path.equals(((Path) obj).path); kitaev@213: } kitaev@213: return false; kitaev@213: } kitaev@213: @Override kitaev@213: public int hashCode() { kitaev@213: return path.hashCode(); kitaev@213: } kitaev@213: kitaev@213: public static Path create(String path) { kitaev@213: if (path == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: if (path.indexOf('\\') != -1) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: Path rv = new Path(path); kitaev@213: return rv; kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * Path filter. kitaev@213: */ kitaev@213: public interface Matcher { kitaev@213: boolean accept(Path path); kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * Factory for paths kitaev@213: */ kitaev@213: public interface Source { kitaev@213: Path path(String p); kitaev@213: } kitaev@213: kitaev@213: /** kitaev@213: * Straightforward {@link Source} implementation that creates new Path instance for each supplied string kitaev@213: */ kitaev@213: public static class SimpleSource implements Source { kitaev@213: private final PathRewrite normalizer; kitaev@213: kitaev@213: public SimpleSource(PathRewrite pathRewrite) { kitaev@213: if (pathRewrite == null) { kitaev@213: throw new IllegalArgumentException(); kitaev@213: } kitaev@213: normalizer = pathRewrite; kitaev@213: } kitaev@213: kitaev@213: public Path path(String p) { kitaev@213: return Path.create(normalizer.rewrite(p)); kitaev@213: } kitaev@213: } kitaev@213: }