comparison src/org/tmatesoft/hg/util/Path.java @ 443:072b5f3ed0c8

Path to tell immediate parent-child relationship; more powerful scope impl; tests for both
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 04 May 2012 17:59:22 +0200
parents 12f668401613
children fedc54356091 49f0749307a0
comparison
equal deleted inserted replaced
442:6865eb742883 443:072b5f3ed0c8
116 public int hashCode() { 116 public int hashCode() {
117 return path.hashCode(); 117 return path.hashCode();
118 } 118 }
119 119
120 public enum CompareResult { 120 public enum CompareResult {
121 Same, Unrelated, Nested, Parent, /* perhaps, also ImmediateParent, DirectChild? */ 121 Same, Unrelated, ImmediateChild, Nested, ImmediateParent, Parent /* +CommonParent ?*/
122 } 122 }
123 123
124 /* 124 /**
125 * a/file and a/dir ? 125 * @return one of {@link CompareResult} constants to indicate relations between the paths
126 */ 126 */
127 public CompareResult compareWith(Path another) { 127 public CompareResult compareWith(Path another) {
128 if (another == null) { 128 if (another == null) {
129 return CompareResult.Unrelated; // XXX perhaps, IAE? 129 return CompareResult.Unrelated; // XXX perhaps, IAE?
130 } 130 }
131 if (another == this || (another.length() == length() && equals(another))) { 131 if (another == this || (another.length() == length() && equals(another))) {
132 return CompareResult.Same; 132 return CompareResult.Same;
133 } 133 }
134 if (path.startsWith(another.path)) { 134 // one of the parties can't be parent in parent/nested, the other may be either file or folder
135 return CompareResult.Nested; 135 if (another.isDirectory() && path.startsWith(another.path)) {
136 } 136 return isOneSegmentDifference(path, another.path) ? CompareResult.ImmediateChild : CompareResult.Nested;
137 if (another.path.startsWith(path)) { 137 }
138 return CompareResult.Parent; 138 if (isDirectory() && another.path.startsWith(path)) {
139 return isOneSegmentDifference(another.path, path) ? CompareResult.ImmediateParent : CompareResult.Parent;
139 } 140 }
140 return CompareResult.Unrelated; 141 return CompareResult.Unrelated;
142 }
143
144 // true if p1 is only one segment larger than p2
145 private static boolean isOneSegmentDifference(String p1, String p2) {
146 assert p1.startsWith(p2);
147 String p1Tail= p1.substring(p2.length());
148 int slashLoc = p1Tail.indexOf('/');
149 return slashLoc == -1 || slashLoc == p1Tail.length() - 1;
141 } 150 }
142 151
143 public static Path create(CharSequence path) { 152 public static Path create(CharSequence path) {
144 if (path == null) { 153 if (path == null) {
145 throw new IllegalArgumentException(); 154 throw new IllegalArgumentException();