Mercurial > jhg
comparison hg4j/src/main/java/org/tmatesoft/hg/util/Path.java @ 213:6ec4af642ba8 gradle
Project uses Gradle for build - actual changes
| author | Alexander Kitaev <kitaev@gmail.com> |
|---|---|
| date | Tue, 10 May 2011 10:52:53 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 212:edb2e2829352 | 213:6ec4af642ba8 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2011 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.util; | |
| 18 | |
| 19 /** | |
| 20 * Identify repository files (not String nor io.File). Convenient for pattern matching. Memory-friendly. | |
| 21 * | |
| 22 * @author Artem Tikhomirov | |
| 23 * @author TMate Software Ltd. | |
| 24 */ | |
| 25 public final class Path implements CharSequence, Comparable<Path>/*Cloneable? - although clone for paths make no sense*/{ | |
| 26 // private String[] segments; | |
| 27 // private int flags; // dir, unparsed | |
| 28 private String path; | |
| 29 | |
| 30 /*package-local*/Path(String p) { | |
| 31 path = p; | |
| 32 } | |
| 33 | |
| 34 /** | |
| 35 * Check if this is directory's path. | |
| 36 * Note, this method doesn't perform any file system operation. | |
| 37 * | |
| 38 * @return true when this path points to a directory | |
| 39 */ | |
| 40 public boolean isDirectory() { | |
| 41 // XXX simple logic for now. Later we may decide to have an explicit factory method to create directory paths | |
| 42 return path.charAt(path.length() - 1) == '/'; | |
| 43 } | |
| 44 | |
| 45 public int length() { | |
| 46 return path.length(); | |
| 47 } | |
| 48 | |
| 49 public char charAt(int index) { | |
| 50 return path.charAt(index); | |
| 51 } | |
| 52 | |
| 53 public CharSequence subSequence(int start, int end) { | |
| 54 // new Path if start-end matches boundaries of any subpath | |
| 55 return path.substring(start, end); | |
| 56 } | |
| 57 | |
| 58 @Override | |
| 59 public String toString() { | |
| 60 return path; // CharSequence demands toString() impl | |
| 61 } | |
| 62 | |
| 63 public int compareTo(Path o) { | |
| 64 return path.compareTo(o.path); | |
| 65 } | |
| 66 | |
| 67 @Override | |
| 68 public boolean equals(Object obj) { | |
| 69 if (obj != null && getClass() == obj.getClass()) { | |
| 70 return this == obj || path.equals(((Path) obj).path); | |
| 71 } | |
| 72 return false; | |
| 73 } | |
| 74 @Override | |
| 75 public int hashCode() { | |
| 76 return path.hashCode(); | |
| 77 } | |
| 78 | |
| 79 public static Path create(String path) { | |
| 80 if (path == null) { | |
| 81 throw new IllegalArgumentException(); | |
| 82 } | |
| 83 if (path.indexOf('\\') != -1) { | |
| 84 throw new IllegalArgumentException(); | |
| 85 } | |
| 86 Path rv = new Path(path); | |
| 87 return rv; | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Path filter. | |
| 92 */ | |
| 93 public interface Matcher { | |
| 94 boolean accept(Path path); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Factory for paths | |
| 99 */ | |
| 100 public interface Source { | |
| 101 Path path(String p); | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * Straightforward {@link Source} implementation that creates new Path instance for each supplied string | |
| 106 */ | |
| 107 public static class SimpleSource implements Source { | |
| 108 private final PathRewrite normalizer; | |
| 109 | |
| 110 public SimpleSource(PathRewrite pathRewrite) { | |
| 111 if (pathRewrite == null) { | |
| 112 throw new IllegalArgumentException(); | |
| 113 } | |
| 114 normalizer = pathRewrite; | |
| 115 } | |
| 116 | |
| 117 public Path path(String p) { | |
| 118 return Path.create(normalizer.rewrite(p)); | |
| 119 } | |
| 120 } | |
| 121 } |
