1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.persistence.utils;
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25
26
27
28 final class PathSection {
29 private static final Pattern pattern = Pattern
30 .compile("([^/\\[\\]]+)\\[(\\d+|-1)\\]");
31 private final String path;
32 private final int index;
33
34 private PathSection(String path, int index) {
35 this.path = path;
36 this.index = index;
37 }
38
39 public String getPath() {
40 return path;
41 }
42
43 public int getIndex() {
44 return index;
45 }
46
47 public static PathSection fromString(String pt) {
48 Matcher matcher = pattern.matcher(pt);
49 if (!matcher.matches()) {
50 throw new IllegalArgumentException(
51 String.format(
52 "Bad path section for nested bundle update: '%s'. "
53 + "Non-terminal paths should contain relation name and index.",
54 pt));
55 }
56 return new PathSection(matcher.group(1), Integer.parseInt(
57 matcher.group(2)));
58 }
59
60 @Override
61 public int hashCode() {
62 return index * 31 + path.hashCode();
63 }
64
65 @Override
66 public boolean equals(Object other) {
67 if (this == other)
68 return true;
69 else if (!(other instanceof PathSection))
70 return false;
71 else {
72 PathSection that = (PathSection)other;
73 return path.equals(that.path) && index == that.index;
74 }
75 }
76
77
78 public String toString() {
79 return path + "[" + index + "]";
80 }
81 }