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) { |
| 128 | elements = new ArrayList<>(elements); |
| 129 | elements.add(child); |
| 130 | return from(elements); |
| 131 | } |
| 132 | |
| 133 | /** |
Sithara Punnassery | 589fac2 | 2016-10-03 11:51:53 -0700 | [diff] [blame] | 134 | * 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 Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 145 | * Returns a path for the parent of this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 146 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 147 | * @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] | 148 | */ |
| 149 | public DocumentPath parent() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 150 | if (pathElements.size() <= 1) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 151 | return null; |
| 152 | } |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 153 | return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1)); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 157 | * Returns the list of path elements representing this path in correct |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 158 | * order. |
| 159 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 160 | * @return a list of elements that make up this path |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 161 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 162 | public List<String> pathElements() { |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 163 | return pathElements; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 166 | /** |
| 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 HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 175 | return this.pathElements.size() < other.pathElements.size() && |
| 176 | this.pathElements.equals(other.pathElements.subList(0, this.pathElements.size())); |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 177 | } |
| 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 HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 195 | * |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 196 | * @param paths collection of path |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 197 | * @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] | 198 | */ |
| 199 | public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) { |
| 200 | if (CollectionUtils.isEmpty(paths)) { |
| 201 | return null; |
| 202 | } |
Yuta HIGUCHI | 796a78d | 2017-11-03 14:09:59 -0700 | [diff] [blame] | 203 | 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 Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 226 | @Override |
| 227 | public int hashCode() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 228 | return Objects.hash(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public boolean equals(Object obj) { |
| 233 | if (obj instanceof DocumentPath) { |
| 234 | DocumentPath that = (DocumentPath) obj; |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 235 | return this.pathElements.equals(that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public String toString() { |
| 242 | StringBuilder stringBuilder = new StringBuilder(); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 243 | Iterator<String> iter = pathElements.iterator(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 244 | while (iter.hasNext()) { |
| 245 | stringBuilder.append(iter.next()); |
| 246 | if (iter.hasNext()) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 247 | stringBuilder.append(pathSeparator); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | return stringBuilder.toString(); |
| 251 | } |
| 252 | |
| 253 | @Override |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 254 | public int compareTo(DocumentPath that) { |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame] | 255 | return Comparators.lexicographical(Comparator.<String>naturalOrder()) |
| 256 | .compare(this.pathElements, that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 257 | } |
| 258 | } |