blob: ed5de9f762e8dc1ed6d737cc312786f9dc0f46a5 [file] [log] [blame]
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.store.service;
18
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -070019import com.google.common.collect.Comparators;
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070020import com.google.common.collect.ImmutableList;
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070021import org.apache.commons.collections.CollectionUtils;
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070022import java.util.ArrayList;
Madan Jampaniad5b8c72016-09-12 15:05:01 -070023import java.util.Arrays;
Madan Jampani98094222016-09-15 21:12:46 -070024import java.util.Collection;
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -070025import java.util.Comparator;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070026import java.util.Iterator;
27import java.util.List;
28import java.util.Objects;
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070029import static com.google.common.base.Preconditions.checkNotNull;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070030
Madan Jampani5bdebd52016-09-07 16:18:12 -070031/**
32 * Unique key for nodes in the {@link DocumentTree}.
33 */
34public class DocumentPath implements Comparable<DocumentPath> {
35
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070036 /** Default path separator. */
37 public static final String DEFAULT_SEPARATOR = "|";
38
39 /** Default path separator regex. */
40 public static final String DEFAULT_SEPARATOR_RE = "\\|";
41
42 // TODO: Add means to set the path separator and separator ERE.
43 private static String pathSeparator = DEFAULT_SEPARATOR;
44 private static String pathSeparatorRE = DEFAULT_SEPARATOR_RE;
45
Jordan Haltermancb1e02c2017-08-25 16:20:43 -070046 /** Root document tree path. */
47 public static final DocumentPath ROOT = DocumentPath.from("root");
48
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -070049 private final List<String> pathElements;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070050
51 /**
52 * Private utility constructor for internal generation of partial paths only.
53 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070054 * @param pathElements list of path elements
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070055 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070056 private DocumentPath(List<String> pathElements) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070057 checkNotNull(pathElements);
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -070058 this.pathElements = ImmutableList.copyOf(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070059 }
60
61 /**
Thomas Vachuskac3984c62016-09-07 17:51:45 -070062 * Constructs a new document path.
Madan Jampani5bdebd52016-09-07 16:18:12 -070063 * <p>
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -070064 * New paths must contain at least one name and string names MUST NOT contain
65 * any path separator characters.
Madan Jampani5bdebd52016-09-07 16:18:12 -070066 * If one field is {@code null} that field will be ignored.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070067 *
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070068 * @param nodeName the name of the last level of this path
Madan Jampani5bdebd52016-09-07 16:18:12 -070069 * @param parentPath the path representing the parent leading up to this
70 * node, in the case of the root this should be {@code null}
71 * @throws IllegalDocumentNameException if both parameters are null or name contains an illegal character ('.')
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070072 */
73 public DocumentPath(String nodeName, DocumentPath parentPath) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070074 checkNotNull(nodeName, "Node name cannot be null");
75 if (nodeName.contains(pathSeparator)) {
Yuta HIGUCHI153d3582017-09-01 16:59:52 -070076 throw new IllegalDocumentNameException("'" + pathSeparator + "'" +
77 " are not allowed in names.");
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070078 }
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -070079
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070080 if (parentPath != null) {
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -070081 pathElements = ImmutableList.<String>builder()
82 .addAll(parentPath.pathElements())
83 .add(nodeName)
84 .build();
85 } else {
86 pathElements = ImmutableList.of(nodeName);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070087 }
88 }
89
90 /**
Madan Jampaniad5b8c72016-09-12 15:05:01 -070091 * Creates a new {@code DocumentPath} from a period delimited path string.
92 *
93 * @param path path string
94 * @return {@code DocumentPath} instance
95 */
96 public static DocumentPath from(String path) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070097 return new DocumentPath(Arrays.asList(path.split(pathSeparatorRE)));
Madan Jampaniad5b8c72016-09-12 15:05:01 -070098 }
99
100 /**
Jordan Haltermanb0ac5902017-07-30 12:31:01 -0700101 * Creates a new {@code DocumentPath} from a list of path elements.
102 *
103 * @param elements path elements
104 * @return {@code DocumentPath} instance
105 */
106 public static DocumentPath from(String... elements) {
107 return from(Arrays.asList(elements));
108 }
109
110 /**
111 * Creates a new {@code DocumentPath} from a list of path elements.
112 *
113 * @param elements path elements
114 * @return {@code DocumentPath} instance
115 */
116 public static DocumentPath from(List<String> elements) {
117 return new DocumentPath(elements);
118 }
119
120 /**
121 * Creates a new {@code DocumentPath} from a list of path elements.
122 *
123 * @param elements path elements
124 * @param child child element
125 * @return {@code DocumentPath} instance
126 */
127 public static DocumentPath from(List<String> elements, String child) {
128 elements = new ArrayList<>(elements);
129 elements.add(child);
130 return from(elements);
131 }
132
133 /**
Sithara Punnassery589fac22016-10-03 11:51:53 -0700134 * Returns the relative path to the given node.
135 *
136 * @return relative path to the given node.
137 */
138 public DocumentPath childPath() {
139 if (pathElements.size() <= 1) {
140 return null;
141 }
142 return new DocumentPath(this.pathElements.subList(pathElements.size() - 1, pathElements.size()));
143 }
144 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700145 * Returns a path for the parent of this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700146 *
Madan Jampani5bdebd52016-09-07 16:18:12 -0700147 * @return parent node path. If this path is for the root, returns {@code null}.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700148 */
149 public DocumentPath parent() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700150 if (pathElements.size() <= 1) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700151 return null;
152 }
Madan Jampani5bdebd52016-09-07 16:18:12 -0700153 return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700154 }
155
156 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700157 * Returns the list of path elements representing this path in correct
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700158 * order.
159 *
Madan Jampani5bdebd52016-09-07 16:18:12 -0700160 * @return a list of elements that make up this path
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700161 */
Madan Jampani5bdebd52016-09-07 16:18:12 -0700162 public List<String> pathElements() {
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700163 return pathElements;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700164 }
165
Madan Jampani98094222016-09-15 21:12:46 -0700166 /**
167 * Returns if the specified path belongs to a direct ancestor of the node pointed at by this path.
168 * <p>
169 * Example: {@code root.a} is a direct ancestor of {@code r.a.b.c}; while {@code r.a.x} is not.
170 *
171 * @param other other path
172 * @return {@code true} is yes; {@code false} otherwise.
173 */
174 public boolean isAncestorOf(DocumentPath other) {
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700175 return this.pathElements.size() < other.pathElements.size() &&
176 this.pathElements.equals(other.pathElements.subList(0, this.pathElements.size()));
Madan Jampani98094222016-09-15 21:12:46 -0700177 }
178
179 /**
180 * Returns if the specified path is belongs to a subtree rooted this path.
181 * <p>
182 * Example: {@code root.a.b} and {@code root.a.b.c.d.e} are descendants of {@code r.a.b};
183 * while {@code r.a.x.c} is not.
184 *
185 * @param other other path
186 * @return {@code true} is yes; {@code false} otherwise.
187 */
188 public boolean isDescendentOf(DocumentPath other) {
189 return other.equals(this) || other.isAncestorOf(this);
190 }
191
192 /**
193 * Returns the path that points to the least common ancestor of the specified
194 * collection of paths.
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700195 *
Madan Jampani98094222016-09-15 21:12:46 -0700196 * @param paths collection of path
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700197 * @return path to least common ancestor or null if there is nothing in common
Madan Jampani98094222016-09-15 21:12:46 -0700198 */
199 public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) {
200 if (CollectionUtils.isEmpty(paths)) {
201 return null;
202 }
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700203 DocumentPath first = paths.iterator().next();
204
205 int maxComps = paths.stream()
206 .map(DocumentPath::pathElements)
207 .mapToInt(List::size)
208 .min()
209 .orElse(-1); // paths.size() will never be 0 here
210
211 for (int i = 0; i < maxComps; ++i) {
212 final int fi = i;
213 String comp = first.pathElements().get(i);
214 boolean isAllCommon = paths.stream()
215 .map(DocumentPath::pathElements)
216 .map(l -> l.get(fi))
217 .allMatch(c -> comp.equals(c));
218 if (!isAllCommon) {
219 return (i == 0) ? null :
220 DocumentPath.from(first.pathElements.subList(0, i));
221 }
222 }
223 return DocumentPath.from(first.pathElements.subList(0, maxComps));
Madan Jampani98094222016-09-15 21:12:46 -0700224 }
225
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700226 @Override
227 public int hashCode() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700228 return Objects.hash(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700229 }
230
231 @Override
232 public boolean equals(Object obj) {
233 if (obj instanceof DocumentPath) {
234 DocumentPath that = (DocumentPath) obj;
Madan Jampani5bdebd52016-09-07 16:18:12 -0700235 return this.pathElements.equals(that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700236 }
237 return false;
238 }
239
240 @Override
241 public String toString() {
242 StringBuilder stringBuilder = new StringBuilder();
Madan Jampani5bdebd52016-09-07 16:18:12 -0700243 Iterator<String> iter = pathElements.iterator();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700244 while (iter.hasNext()) {
245 stringBuilder.append(iter.next());
246 if (iter.hasNext()) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -0700247 stringBuilder.append(pathSeparator);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700248 }
249 }
250 return stringBuilder.toString();
251 }
252
253 @Override
Madan Jampani5bdebd52016-09-07 16:18:12 -0700254 public int compareTo(DocumentPath that) {
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -0700255 return Comparators.lexicographical(Comparator.<String>naturalOrder())
256 .compare(this.pathElements, that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700257 }
258}