Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Comparators; |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 21 | import org.apache.commons.collections.CollectionUtils; |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 23 | import java.util.Arrays; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 24 | import java.util.Collection; |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 25 | import java.util.Comparator; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.Objects; |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 30 | |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Unique key for nodes in the {@link DocumentTree}. |
| 33 | */ |
| 34 | public class DocumentPath implements Comparable<DocumentPath> { |
| 35 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 36 | /** 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 Halterman | cb1e02c | 2017-08-25 16:20:43 -0700 | [diff] [blame] | 46 | /** Root document tree path. */ |
| 47 | public static final DocumentPath ROOT = DocumentPath.from("root"); |
| 48 | |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 49 | private final List<String> pathElements; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Private utility constructor for internal generation of partial paths only. |
| 53 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 54 | * @param pathElements list of path elements |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 55 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 56 | private DocumentPath(List<String> pathElements) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 57 | checkNotNull(pathElements); |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 58 | this.pathElements = ImmutableList.copyOf(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Thomas Vachuska | c3984c6 | 2016-09-07 17:51:45 -0700 | [diff] [blame] | 62 | * Constructs a new document path. |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 63 | * <p> |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 64 | * New paths must contain at least one name and string names MUST NOT contain |
| 65 | * any path separator characters. |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 66 | * If one field is {@code null} that field will be ignored. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 67 | * |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 68 | * @param nodeName the name of the last level of this path |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 69 | * @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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 72 | */ |
| 73 | public DocumentPath(String nodeName, DocumentPath parentPath) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 74 | checkNotNull(nodeName, "Node name cannot be null"); |
| 75 | if (nodeName.contains(pathSeparator)) { |
Yuta HIGUCHI | 153d358 | 2017-09-01 16:59:52 -0700 | [diff] [blame] | 76 | throw new IllegalDocumentNameException("'" + pathSeparator + "'" + |
| 77 | " are not allowed in names."); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 78 | } |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 79 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 80 | if (parentPath != null) { |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 81 | pathElements = ImmutableList.<String>builder() |
| 82 | .addAll(parentPath.pathElements()) |
| 83 | .add(nodeName) |
| 84 | .build(); |
| 85 | } else { |
| 86 | pathElements = ImmutableList.of(nodeName); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 91 | * 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 Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 97 | return new DocumentPath(Arrays.asList(path.split(pathSeparatorRE))); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 101 | * 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) { |
Yuta HIGUCHI | 2d1b590 | 2018-02-15 14:49:23 -0800 | [diff] [blame] | 128 | List<String> concat = new ArrayList<>(elements.size() + 1); |
| 129 | concat.addAll(elements); |
| 130 | concat.add(child); |
| 131 | return from(concat); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Creates a new {@code DocumentPath} from a list of path elements. |
| 136 | * |
| 137 | * @param elements path elements |
| 138 | * @param childElms child element |
| 139 | * @return {@code DocumentPath} instance |
| 140 | */ |
| 141 | public static DocumentPath from(List<String> elements, String... childElms) { |
| 142 | List<String> concat = new ArrayList<>(elements.size() + childElms.length); |
| 143 | concat.addAll(elements); |
| 144 | concat.addAll(Arrays.asList(childElms)); |
| 145 | return from(concat); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Creates a new DocumentPath element appending {@code childElm} to |
| 150 | * this path. |
| 151 | * |
| 152 | * @param childElms to append |
| 153 | * @return this + childElm |
| 154 | */ |
| 155 | public DocumentPath append(List<String> childElms) { |
| 156 | List<String> concat = new ArrayList<>(pathElements.size() + childElms.size()); |
| 157 | concat.addAll(pathElements); |
| 158 | concat.addAll(childElms); |
| 159 | return from(concat); |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
Sithara Punnassery | 589fac2 | 2016-10-03 11:51:53 -0700 | [diff] [blame] | 163 | * Returns the relative path to the given node. |
| 164 | * |
| 165 | * @return relative path to the given node. |
| 166 | */ |
| 167 | public DocumentPath childPath() { |
| 168 | if (pathElements.size() <= 1) { |
| 169 | return null; |
| 170 | } |
| 171 | return new DocumentPath(this.pathElements.subList(pathElements.size() - 1, pathElements.size())); |
| 172 | } |
| 173 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 174 | * Returns a path for the parent of this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 175 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 176 | * @return parent node path. If this path is for the root, returns {@code null}. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 177 | */ |
| 178 | public DocumentPath parent() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 179 | if (pathElements.size() <= 1) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 180 | return null; |
| 181 | } |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 182 | return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1)); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 186 | * Returns the list of path elements representing this path in correct |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 187 | * order. |
| 188 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 189 | * @return a list of elements that make up this path |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 190 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 191 | public List<String> pathElements() { |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 192 | return pathElements; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Returns if the specified path belongs to a direct ancestor of the node pointed at by this path. |
| 197 | * <p> |
| 198 | * Example: {@code root.a} is a direct ancestor of {@code r.a.b.c}; while {@code r.a.x} is not. |
| 199 | * |
| 200 | * @param other other path |
| 201 | * @return {@code true} is yes; {@code false} otherwise. |
| 202 | */ |
| 203 | public boolean isAncestorOf(DocumentPath other) { |
Yuta HIGUCHI | b2a20d1 | 2018-02-14 10:25:55 -0800 | [diff] [blame] | 204 | return other != null && |
| 205 | this.pathElements.size() < other.pathElements.size() && |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 206 | this.pathElements.equals(other.pathElements.subList(0, this.pathElements.size())); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Returns if the specified path is belongs to a subtree rooted this path. |
| 211 | * <p> |
| 212 | * Example: {@code root.a.b} and {@code root.a.b.c.d.e} are descendants of {@code r.a.b}; |
| 213 | * while {@code r.a.x.c} is not. |
| 214 | * |
| 215 | * @param other other path |
| 216 | * @return {@code true} is yes; {@code false} otherwise. |
| 217 | */ |
| 218 | public boolean isDescendentOf(DocumentPath other) { |
Yuta HIGUCHI | b2a20d1 | 2018-02-14 10:25:55 -0800 | [diff] [blame] | 219 | return other != null && |
| 220 | (other.equals(this) || other.isAncestorOf(this)); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns the path that points to the least common ancestor of the specified |
| 225 | * collection of paths. |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 226 | * |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 227 | * @param paths collection of path |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 228 | * @return path to least common ancestor or null if there is nothing in common |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 229 | */ |
| 230 | public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) { |
| 231 | if (CollectionUtils.isEmpty(paths)) { |
| 232 | return null; |
| 233 | } |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 234 | DocumentPath first = paths.iterator().next(); |
| 235 | |
| 236 | int maxComps = paths.stream() |
| 237 | .map(DocumentPath::pathElements) |
| 238 | .mapToInt(List::size) |
| 239 | .min() |
| 240 | .orElse(-1); // paths.size() will never be 0 here |
| 241 | |
| 242 | for (int i = 0; i < maxComps; ++i) { |
| 243 | final int fi = i; |
| 244 | String comp = first.pathElements().get(i); |
| 245 | boolean isAllCommon = paths.stream() |
| 246 | .map(DocumentPath::pathElements) |
| 247 | .map(l -> l.get(fi)) |
| 248 | .allMatch(c -> comp.equals(c)); |
| 249 | if (!isAllCommon) { |
| 250 | return (i == 0) ? null : |
| 251 | DocumentPath.from(first.pathElements.subList(0, i)); |
| 252 | } |
| 253 | } |
| 254 | return DocumentPath.from(first.pathElements.subList(0, maxComps)); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 257 | @Override |
| 258 | public int hashCode() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 259 | return Objects.hash(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public boolean equals(Object obj) { |
| 264 | if (obj instanceof DocumentPath) { |
| 265 | DocumentPath that = (DocumentPath) obj; |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 266 | return this.pathElements.equals(that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public String toString() { |
| 273 | StringBuilder stringBuilder = new StringBuilder(); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 274 | Iterator<String> iter = pathElements.iterator(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 275 | while (iter.hasNext()) { |
| 276 | stringBuilder.append(iter.next()); |
| 277 | if (iter.hasNext()) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 278 | stringBuilder.append(pathSeparator); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | return stringBuilder.toString(); |
| 282 | } |
| 283 | |
| 284 | @Override |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 285 | public int compareTo(DocumentPath that) { |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 286 | return Comparators.lexicographical(Comparator.<String>naturalOrder()) |
| 287 | .compare(this.pathElements, that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 288 | } |
| 289 | } |