/
ZPath.java
282 lines (258 loc) · 8.76 KB
/
ZPath.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.curator.x.async.modeled;
import org.apache.curator.x.async.modeled.details.ZPathImpl;
import java.util.Arrays;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import static org.apache.curator.utils.ZKPaths.PATH_SEPARATOR;
/**
* Abstracts a ZooKeeper ZNode path
*/
public interface ZPath extends Resolvable
{
String PARAMETER_OPENING_DELIMITER = "{";
String PARAMETER_CLOSING_DELIMITER = "}";
/**
* The root path: "/"
*/
ZPath root = ZPathImpl.root;
/**
* Returns the special node name that can be used for replacements at runtime
* via {@link #resolved(Object...)} when passed via the various <code>from()</code> methods
*/
static String parameter()
{
return parameter("id");
}
/**
* Same as {@link #parameter()} but allows you to specify an alternate code/name. This name
* has no effect and is only for debugging purposes. When <code>toString()</code> is called
* on ZPaths, this code shows.
*/
static String parameter(String name)
{
return PATH_SEPARATOR + PARAMETER_OPENING_DELIMITER + name + PARAMETER_CLOSING_DELIMITER;
}
/**
* Take a string path and return a ZPath
*
* @param fullPath the path to parse
* @return ZPath
* @throws IllegalArgumentException if the path is invalid
*/
static ZPath parse(String fullPath)
{
return ZPathImpl.parse(fullPath, s -> s);
}
/**
* Take a string path and return a ZPath. Each part of the path
* that is <code>{XXXX}</code> is replaced with {@link #parameter()}.
* E.g. <code>parseWithIds("/one/two/{first}/three/{second}")</code> is the equivalent
* of calling <code>ZPath.from("one", "two", parameter(), "three", parameter())</code>
*
* @param fullPath the path to parse
* @return ZPath
* @throws IllegalArgumentException if the path is invalid
*/
static ZPath parseWithIds(String fullPath)
{
return ZPathImpl.parse(fullPath, s -> isId(s) ? (PATH_SEPARATOR + s) : s); // TODO
}
/**
* Return true if the given string conforms to the "{XXXX}" ID pattern
*
* @param s string to check
* @return true/false
*/
static boolean isId(String s)
{
return s.startsWith("{") && s.endsWith("}");
}
/**
* Take a ZNode string path and return a ZPath
*
* @param fullPath the path to parse
* @param nameFilter each part of the path is passed through this filter
* @return ZPath
* @throws IllegalArgumentException if the path is invalid
*/
static ZPath parse(String fullPath, UnaryOperator<String> nameFilter)
{
return ZPathImpl.parse(fullPath, nameFilter);
}
/**
* Convert individual path names into a ZPath. E.g.
* <code>ZPath.from("my", "full", "path")</code>. Any/all of the names can be passed as
* {@link #parameter()} so that the path can be resolved later using
* of the <code>resolved()</code> methods.
*
* @param names path names
* @return ZPath
* @throws IllegalArgumentException if any of the names is invalid
*/
static ZPath from(String... names)
{
return ZPathImpl.from(names);
}
/**
* Convert individual path names into a ZPath. Any/all of the names can be passed as
* {@link #parameter()} so that the path can be resolved later using
* of the <code>resolved()</code> methods.
*
* @param names path names
* @return ZPath
* @throws IllegalArgumentException if any of the names is invalid
*/
static ZPath from(List<String> names)
{
return ZPathImpl.from(names);
}
/**
* Convert individual path names into a ZPath starting at the given base. E.g.
* if base is "/home/base" <code>ZPath.from(base, "my", "full", "path")</code>
* would be "/home/base/my/full/path". Any/all of the names can be passed as
* {@link #parameter()} so that the path can be resolved later using
* of the <code>resolved()</code> methods.
*
* @param base base/starting path
* @param names path names
* @return ZPath
* @throws IllegalArgumentException if any of the names is invalid
*/
static ZPath from(ZPath base, String... names)
{
return ZPathImpl.from(base, names);
}
/**
* Convert individual path names into a ZPath starting at the given base. Any/all of the names can be passed as
* {@link #parameter()} so that the path can be resolved later using
* of the <code>resolved()</code> methods.
*
* @param base base/starting path
* @param names path names
* @return ZPath
* @throws IllegalArgumentException if any of the names is invalid
*/
static ZPath from(ZPath base, List<String> names)
{
return ZPathImpl.from(base, names);
}
/**
* <p>
* When creating paths, any node in the path can be set to {@link #parameter()}.
* At runtime, the ZPath can be "resolved" by replacing these nodes with values.
* </p>
*
* <p>
* The replacement is the <code>toString()</code> value of the parameter object or,
* if the object implements {@link org.apache.curator.x.async.modeled.NodeName},
* the value of <code>nodeName()</code>.
* </p>
*
* @param parameters list of replacements. Must have be the same length as the number of
* parameter nodes in the path
* @return new resolved ZPath
*/
@Override
default ZPath resolved(Object... parameters)
{
return resolved(Arrays.asList(parameters));
}
/**
* <p>
* When creating paths, any node in the path can be set to {@link #parameter()}.
* At runtime, the ZPath can be "resolved" by replacing these nodes with values.
* </p>
*
* <p>
* The replacement is the <code>toString()</code> value of the parameter object or,
* if the object implements {@link org.apache.curator.x.async.modeled.NodeName},
* the value of <code>nodeName()</code>.
* </p>
*
* @param parameters list of replacements. Must have be the same length as the number of
* parameter nodes in the path
* @return new resolved ZPath
*/
@Override
ZPath resolved(List<Object> parameters);
/**
* <p>
* Return a ZPath that represents a child ZNode of this ZPath. e.g.
* <code>ZPath.from("a", "b").at("c")</code> represents the path "/a/b/c"
* </p>
*
* <p>
* The replacement is the <code>toString()</code> value of child or,
* if it implements {@link org.apache.curator.x.async.modeled.NodeName},
* the value of <code>nodeName()</code>.
* </p>
*
* @param child child node name
* @return ZPath
*/
ZPath child(Object child);
/**
* Return this ZPath's parent
*
* @return parent ZPath
* @throws java.util.NoSuchElementException if this is the root ZPath
*/
ZPath parent();
/**
* Return true/false if this is the root ZPath
*
* @return true false
*/
boolean isRoot();
/**
* Return true if this path is fully resolved (i.e. has no unresolved parameters)
*
* @return true/false
*/
boolean isResolved();
/**
* Return true if this path starts with the given path. i.e.
* <code>ZPath.from("/one/two/three").startsWith(ZPath.from("/one/two"))</code> returns true
*
* @param path base path
* @return true/false
*/
boolean startsWith(ZPath path);
/**
* The string full path that this ZPath represents
*
* @return full path
*/
String fullPath();
/**
* The node name at this ZPath
*
* @return name
*/
String nodeName();
/**
* Return a regex Pattern useful for using in {@link org.apache.curator.framework.schema.Schema}
*
* @return pattern for this path
*/
Pattern toSchemaPathPattern();
}