blob: 8c969aa0b861a4964e471d0261eeee5dd15be935 [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 HIGUCHIb2a20d12018-02-14 10:25:55 -0800175 return other != null &&
176 this.pathElements.size() < other.pathElements.size() &&
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700177 this.pathElements.equals(other.pathElements.subList(0, this.pathElements.size()));
Madan Jampani98094222016-09-15 21:12:46 -0700178 }
179
180 /**
181 * Returns if the specified path is belongs to a subtree rooted this path.
182 * <p>
183 * Example: {@code root.a.b} and {@code root.a.b.c.d.e} are descendants of {@code r.a.b};
184 * while {@code r.a.x.c} is not.
185 *
186 * @param other other path
187 * @return {@code true} is yes; {@code false} otherwise.
188 */
189 public boolean isDescendentOf(DocumentPath other) {
Yuta HIGUCHIb2a20d12018-02-14 10:25:55 -0800190 return other != null &&
191 (other.equals(this) || other.isAncestorOf(this));
Madan Jampani98094222016-09-15 21:12:46 -0700192 }
193
194 /**
195 * Returns the path that points to the least common ancestor of the specified
196 * collection of paths.
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700197 *
Madan Jampani98094222016-09-15 21:12:46 -0700198 * @param paths collection of path
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700199 * @return path to least common ancestor or null if there is nothing in common
Madan Jampani98094222016-09-15 21:12:46 -0700200 */
201 public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) {
202 if (CollectionUtils.isEmpty(paths)) {
203 return null;
204 }
Yuta HIGUCHI796a78d2017-11-03 14:09:59 -0700205 DocumentPath first = paths.iterator().next();
206
207 int maxComps = paths.stream()
208 .map(DocumentPath::pathElements)
209 .mapToInt(List::size)
210 .min()
211 .orElse(-1); // paths.size() will never be 0 here
212
213 for (int i = 0; i < maxComps; ++i) {
214 final int fi = i;
215 String comp = first.pathElements().get(i);
216 boolean isAllCommon = paths.stream()
217 .map(DocumentPath::pathElements)
218 .map(l -> l.get(fi))
219 .allMatch(c -> comp.equals(c));
220 if (!isAllCommon) {
221 return (i == 0) ? null :
222 DocumentPath.from(first.pathElements.subList(0, i));
223 }
224 }
225 return DocumentPath.from(first.pathElements.subList(0, maxComps));
Madan Jampani98094222016-09-15 21:12:46 -0700226 }
227
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700228 @Override
229 public int hashCode() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700230 return Objects.hash(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700231 }
232
233 @Override
234 public boolean equals(Object obj) {
235 if (obj instanceof DocumentPath) {
236 DocumentPath that = (DocumentPath) obj;
Madan Jampani5bdebd52016-09-07 16:18:12 -0700237 return this.pathElements.equals(that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700238 }
239 return false;
240 }
241
242 @Override
243 public String toString() {
244 StringBuilder stringBuilder = new StringBuilder();
Madan Jampani5bdebd52016-09-07 16:18:12 -0700245 Iterator<String> iter = pathElements.iterator();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700246 while (iter.hasNext()) {
247 stringBuilder.append(iter.next());
248 if (iter.hasNext()) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -0700249 stringBuilder.append(pathSeparator);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700250 }
251 }
252 return stringBuilder.toString();
253 }
254
255 @Override
Madan Jampani5bdebd52016-09-07 16:18:12 -0700256 public int compareTo(DocumentPath that) {
Yuta HIGUCHI52e047f2017-09-11 16:43:21 -0700257 return Comparators.lexicographical(Comparator.<String>naturalOrder())
258 .compare(this.pathElements, that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700259 }
260}