blob: a5e1eaaa24d831842c4bfb400143d2eac5e5a80d [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
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import org.apache.commons.collections.CollectionUtils;
22import org.apache.commons.lang.StringUtils;
23
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070024import java.util.ArrayList;
Madan Jampaniad5b8c72016-09-12 15:05:01 -070025import java.util.Arrays;
Madan Jampani98094222016-09-15 21:12:46 -070026import java.util.Collection;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070027import java.util.Iterator;
28import java.util.List;
29import java.util.Objects;
30
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070031import static com.google.common.base.Preconditions.checkNotNull;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070032
Madan Jampani5bdebd52016-09-07 16:18:12 -070033/**
34 * Unique key for nodes in the {@link DocumentTree}.
35 */
36public class DocumentPath implements Comparable<DocumentPath> {
37
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070038 /** Default path separator. */
39 public static final String DEFAULT_SEPARATOR = "|";
40
41 /** Default path separator regex. */
42 public static final String DEFAULT_SEPARATOR_RE = "\\|";
43
44 // TODO: Add means to set the path separator and separator ERE.
45 private static String pathSeparator = DEFAULT_SEPARATOR;
46 private static String pathSeparatorRE = DEFAULT_SEPARATOR_RE;
47
Madan Jampani5bdebd52016-09-07 16:18:12 -070048 private final List<String> pathElements = Lists.newArrayList();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070049
50 /**
51 * Private utility constructor for internal generation of partial paths only.
52 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070053 * @param pathElements list of path elements
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070054 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070055 private DocumentPath(List<String> pathElements) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070056 checkNotNull(pathElements);
Madan Jampani5bdebd52016-09-07 16:18:12 -070057 this.pathElements.addAll(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070058 }
59
60 /**
Thomas Vachuskac3984c62016-09-07 17:51:45 -070061 * Constructs a new document path.
Madan Jampani5bdebd52016-09-07 16:18:12 -070062 * <p>
63 * New paths must contain at least one name and string names may NOT contain any period characters.
64 * If one field is {@code null} that field will be ignored.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070065 *
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070066 * @param nodeName the name of the last level of this path
Madan Jampani5bdebd52016-09-07 16:18:12 -070067 * @param parentPath the path representing the parent leading up to this
68 * node, in the case of the root this should be {@code null}
69 * @throws IllegalDocumentNameException if both parameters are null or name contains an illegal character ('.')
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070070 */
71 public DocumentPath(String nodeName, DocumentPath parentPath) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070072 checkNotNull(nodeName, "Node name cannot be null");
73 if (nodeName.contains(pathSeparator)) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070074 throw new IllegalDocumentNameException(
75 "Periods are not allowed in names.");
76 }
77 if (parentPath != null) {
Madan Jampani5bdebd52016-09-07 16:18:12 -070078 pathElements.addAll(parentPath.pathElements());
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070079 }
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070080 pathElements.add(nodeName);
Madan Jampani5bdebd52016-09-07 16:18:12 -070081 if (pathElements.isEmpty()) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070082 throw new IllegalDocumentNameException("A document path must contain at" +
83 "least one non-null" +
84 "element.");
85 }
86 }
87
88 /**
Madan Jampaniad5b8c72016-09-12 15:05:01 -070089 * Creates a new {@code DocumentPath} from a period delimited path string.
90 *
91 * @param path path string
92 * @return {@code DocumentPath} instance
93 */
94 public static DocumentPath from(String path) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -070095 return new DocumentPath(Arrays.asList(path.split(pathSeparatorRE)));
Madan Jampaniad5b8c72016-09-12 15:05:01 -070096 }
97
98 /**
Jordan Haltermanb0ac5902017-07-30 12:31:01 -070099 * Creates a new {@code DocumentPath} from a list of path elements.
100 *
101 * @param elements path elements
102 * @return {@code DocumentPath} instance
103 */
104 public static DocumentPath from(String... elements) {
105 return from(Arrays.asList(elements));
106 }
107
108 /**
109 * Creates a new {@code DocumentPath} from a list of path elements.
110 *
111 * @param elements path elements
112 * @return {@code DocumentPath} instance
113 */
114 public static DocumentPath from(List<String> elements) {
115 return new DocumentPath(elements);
116 }
117
118 /**
119 * Creates a new {@code DocumentPath} from a list of path elements.
120 *
121 * @param elements path elements
122 * @param child child element
123 * @return {@code DocumentPath} instance
124 */
125 public static DocumentPath from(List<String> elements, String child) {
126 elements = new ArrayList<>(elements);
127 elements.add(child);
128 return from(elements);
129 }
130
131 /**
Sithara Punnassery589fac22016-10-03 11:51:53 -0700132 * Returns the relative path to the given node.
133 *
134 * @return relative path to the given node.
135 */
136 public DocumentPath childPath() {
137 if (pathElements.size() <= 1) {
138 return null;
139 }
140 return new DocumentPath(this.pathElements.subList(pathElements.size() - 1, pathElements.size()));
141 }
142 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700143 * Returns a path for the parent of this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700144 *
Madan Jampani5bdebd52016-09-07 16:18:12 -0700145 * @return parent node path. If this path is for the root, returns {@code null}.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700146 */
147 public DocumentPath parent() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700148 if (pathElements.size() <= 1) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700149 return null;
150 }
Madan Jampani5bdebd52016-09-07 16:18:12 -0700151 return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700152 }
153
154 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -0700155 * Returns the list of path elements representing this path in correct
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700156 * order.
157 *
Madan Jampani5bdebd52016-09-07 16:18:12 -0700158 * @return a list of elements that make up this path
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700159 */
Madan Jampani5bdebd52016-09-07 16:18:12 -0700160 public List<String> pathElements() {
161 return ImmutableList.copyOf(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700162 }
163
Madan Jampani98094222016-09-15 21:12:46 -0700164 /**
165 * Returns if the specified path belongs to a direct ancestor of the node pointed at by this path.
166 * <p>
167 * Example: {@code root.a} is a direct ancestor of {@code r.a.b.c}; while {@code r.a.x} is not.
168 *
169 * @param other other path
170 * @return {@code true} is yes; {@code false} otherwise.
171 */
172 public boolean isAncestorOf(DocumentPath other) {
173 return !other.equals(this) && other.toString().startsWith(toString());
174 }
175
176 /**
177 * Returns if the specified path is belongs to a subtree rooted this path.
178 * <p>
179 * Example: {@code root.a.b} and {@code root.a.b.c.d.e} are descendants of {@code r.a.b};
180 * while {@code r.a.x.c} is not.
181 *
182 * @param other other path
183 * @return {@code true} is yes; {@code false} otherwise.
184 */
185 public boolean isDescendentOf(DocumentPath other) {
186 return other.equals(this) || other.isAncestorOf(this);
187 }
188
189 /**
190 * Returns the path that points to the least common ancestor of the specified
191 * collection of paths.
192 * @param paths collection of path
193 * @return path to least common ancestor
194 */
195 public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) {
196 if (CollectionUtils.isEmpty(paths)) {
197 return null;
198 }
199 return DocumentPath.from(StringUtils.getCommonPrefix(paths.stream()
200 .map(DocumentPath::toString)
201 .toArray(String[]::new)));
202 }
203
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700204 @Override
205 public int hashCode() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700206 return Objects.hash(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700207 }
208
209 @Override
210 public boolean equals(Object obj) {
211 if (obj instanceof DocumentPath) {
212 DocumentPath that = (DocumentPath) obj;
Madan Jampani5bdebd52016-09-07 16:18:12 -0700213 return this.pathElements.equals(that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700214 }
215 return false;
216 }
217
218 @Override
219 public String toString() {
220 StringBuilder stringBuilder = new StringBuilder();
Madan Jampani5bdebd52016-09-07 16:18:12 -0700221 Iterator<String> iter = pathElements.iterator();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700222 while (iter.hasNext()) {
223 stringBuilder.append(iter.next());
224 if (iter.hasNext()) {
Thomas Vachuskae2bd1152017-03-23 13:42:32 -0700225 stringBuilder.append(pathSeparator);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700226 }
227 }
228 return stringBuilder.toString();
229 }
230
231 @Override
Madan Jampani5bdebd52016-09-07 16:18:12 -0700232 public int compareTo(DocumentPath that) {
233 int shorterLength = this.pathElements.size() > that.pathElements.size()
234 ? that.pathElements.size() : this.pathElements.size();
235 for (int i = 0; i < shorterLength; i++) {
236 if (this.pathElements.get(i).compareTo(that.pathElements.get(i)) != 0) {
237 return this.pathElements.get(i).compareTo(that.pathElements.get(i));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700238 }
239 }
Madan Jampani5bdebd52016-09-07 16:18:12 -0700240 if (this.pathElements.size() > that.pathElements.size()) {
241 return 1;
242 } else if (that.pathElements.size() > this.pathElements.size()) {
243 return -1;
244 } else {
245 return 0;
246 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700247 }
248}