Mercurial > hg4j
comparison src/org/tmatesoft/hg/util/Path.java @ 229:1ec6b327a6ac
Scope for status reworked: explicit files or a general matcher
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 31 May 2011 05:23:07 +0200 |
parents | 1a7a9a20e1f9 |
children | a415fe296a50 |
comparison
equal
deleted
inserted
replaced
228:fffe4f882248 | 229:1ec6b327a6ac |
---|---|
13 * For information on how to redistribute this software under | 13 * For information on how to redistribute this software under |
14 * the terms of a license other than GNU General Public License | 14 * the terms of a license other than GNU General Public License |
15 * contact TMate Software at support@hg4j.com | 15 * contact TMate Software at support@hg4j.com |
16 */ | 16 */ |
17 package org.tmatesoft.hg.util; | 17 package org.tmatesoft.hg.util; |
18 | |
19 import java.util.Collection; | |
18 | 20 |
19 /** | 21 /** |
20 * Identify repository files (not String nor io.File). Convenient for pattern matching. Memory-friendly. | 22 * Identify repository files (not String nor io.File). Convenient for pattern matching. Memory-friendly. |
21 * | 23 * |
22 * @author Artem Tikhomirov | 24 * @author Artem Tikhomirov |
73 } | 75 } |
74 @Override | 76 @Override |
75 public int hashCode() { | 77 public int hashCode() { |
76 return path.hashCode(); | 78 return path.hashCode(); |
77 } | 79 } |
80 | |
81 public enum CompareResult { | |
82 Same, Unrelated, Nested, Parent, /* perhaps, also ImmediateParent, DirectChild? */ | |
83 } | |
84 | |
85 /* | |
86 * a/file and a/dir ? | |
87 */ | |
88 public CompareResult compareWith(Path another) { | |
89 if (another == null) { | |
90 return CompareResult.Unrelated; // XXX perhaps, IAE? | |
91 } | |
92 if (another == this || (another.length() == length() && equals(another))) { | |
93 return CompareResult.Same; | |
94 } | |
95 if (path.startsWith(another.path)) { | |
96 return CompareResult.Nested; | |
97 } | |
98 if (another.path.startsWith(path)) { | |
99 return CompareResult.Parent; | |
100 } | |
101 return CompareResult.Unrelated; | |
102 } | |
78 | 103 |
79 public static Path create(String path) { | 104 public static Path create(String path) { |
80 if (path == null) { | 105 if (path == null) { |
81 throw new IllegalArgumentException(); | 106 throw new IllegalArgumentException(); |
82 } | 107 } |
90 /** | 115 /** |
91 * Path filter. | 116 * Path filter. |
92 */ | 117 */ |
93 public interface Matcher { | 118 public interface Matcher { |
94 boolean accept(Path path); | 119 boolean accept(Path path); |
120 | |
121 final class Any implements Matcher { | |
122 public boolean accept(Path path) { return true; } | |
123 } | |
124 class Composite implements Matcher { | |
125 private final Path.Matcher[] elements; | |
126 | |
127 public Composite(Collection<Path.Matcher> matchers) { | |
128 elements = matchers.toArray(new Path.Matcher[matchers.size()]); | |
129 } | |
130 | |
131 public boolean accept(Path path) { | |
132 for (Path.Matcher m : elements) { | |
133 if (m.accept(path)) { | |
134 return true; | |
135 } | |
136 } | |
137 return false; | |
138 } | |
139 } | |
95 } | 140 } |
96 | 141 |
97 /** | 142 /** |
98 * Factory for paths | 143 * Factory for paths |
99 */ | 144 */ |