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; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.commons.collections.CollectionUtils; |
| 23 | import org.apache.commons.lang.StringUtils; |
| 24 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 27 | import java.util.Collection; |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame^] | 28 | import java.util.Comparator; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 29 | import java.util.Iterator; |
| 30 | import java.util.List; |
| 31 | import java.util.Objects; |
| 32 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 34 | |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Unique key for nodes in the {@link DocumentTree}. |
| 37 | */ |
| 38 | public class DocumentPath implements Comparable<DocumentPath> { |
| 39 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 40 | /** Default path separator. */ |
| 41 | public static final String DEFAULT_SEPARATOR = "|"; |
| 42 | |
| 43 | /** Default path separator regex. */ |
| 44 | public static final String DEFAULT_SEPARATOR_RE = "\\|"; |
| 45 | |
| 46 | // TODO: Add means to set the path separator and separator ERE. |
| 47 | private static String pathSeparator = DEFAULT_SEPARATOR; |
| 48 | private static String pathSeparatorRE = DEFAULT_SEPARATOR_RE; |
| 49 | |
Jordan Halterman | cb1e02c | 2017-08-25 16:20:43 -0700 | [diff] [blame] | 50 | /** Root document tree path. */ |
| 51 | public static final DocumentPath ROOT = DocumentPath.from("root"); |
| 52 | |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 53 | private final List<String> pathElements = Lists.newArrayList(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Private utility constructor for internal generation of partial paths only. |
| 57 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 58 | * @param pathElements list of path elements |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 59 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 60 | private DocumentPath(List<String> pathElements) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 61 | checkNotNull(pathElements); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 62 | this.pathElements.addAll(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Thomas Vachuska | c3984c6 | 2016-09-07 17:51:45 -0700 | [diff] [blame] | 66 | * Constructs a new document path. |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 67 | * <p> |
| 68 | * New paths must contain at least one name and string names may NOT contain any period characters. |
| 69 | * If one field is {@code null} that field will be ignored. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 70 | * |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 71 | * @param nodeName the name of the last level of this path |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 72 | * @param parentPath the path representing the parent leading up to this |
| 73 | * node, in the case of the root this should be {@code null} |
| 74 | * @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] | 75 | */ |
| 76 | public DocumentPath(String nodeName, DocumentPath parentPath) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 77 | checkNotNull(nodeName, "Node name cannot be null"); |
| 78 | if (nodeName.contains(pathSeparator)) { |
Yuta HIGUCHI | 153d358 | 2017-09-01 16:59:52 -0700 | [diff] [blame] | 79 | throw new IllegalDocumentNameException("'" + pathSeparator + "'" + |
| 80 | " are not allowed in names."); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 81 | } |
| 82 | if (parentPath != null) { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 83 | pathElements.addAll(parentPath.pathElements()); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 84 | } |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 85 | pathElements.add(nodeName); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 86 | if (pathElements.isEmpty()) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 87 | throw new IllegalDocumentNameException("A document path must contain at" + |
| 88 | "least one non-null" + |
| 89 | "element."); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 94 | * Creates a new {@code DocumentPath} from a period delimited path string. |
| 95 | * |
| 96 | * @param path path string |
| 97 | * @return {@code DocumentPath} instance |
| 98 | */ |
| 99 | public static DocumentPath from(String path) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 100 | return new DocumentPath(Arrays.asList(path.split(pathSeparatorRE))); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 104 | * Creates a new {@code DocumentPath} from a list of path elements. |
| 105 | * |
| 106 | * @param elements path elements |
| 107 | * @return {@code DocumentPath} instance |
| 108 | */ |
| 109 | public static DocumentPath from(String... elements) { |
| 110 | return from(Arrays.asList(elements)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a new {@code DocumentPath} from a list of path elements. |
| 115 | * |
| 116 | * @param elements path elements |
| 117 | * @return {@code DocumentPath} instance |
| 118 | */ |
| 119 | public static DocumentPath from(List<String> elements) { |
| 120 | return new DocumentPath(elements); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates a new {@code DocumentPath} from a list of path elements. |
| 125 | * |
| 126 | * @param elements path elements |
| 127 | * @param child child element |
| 128 | * @return {@code DocumentPath} instance |
| 129 | */ |
| 130 | public static DocumentPath from(List<String> elements, String child) { |
| 131 | elements = new ArrayList<>(elements); |
| 132 | elements.add(child); |
| 133 | return from(elements); |
| 134 | } |
| 135 | |
| 136 | /** |
Sithara Punnassery | 589fac2 | 2016-10-03 11:51:53 -0700 | [diff] [blame] | 137 | * Returns the relative path to the given node. |
| 138 | * |
| 139 | * @return relative path to the given node. |
| 140 | */ |
| 141 | public DocumentPath childPath() { |
| 142 | if (pathElements.size() <= 1) { |
| 143 | return null; |
| 144 | } |
| 145 | return new DocumentPath(this.pathElements.subList(pathElements.size() - 1, pathElements.size())); |
| 146 | } |
| 147 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 148 | * Returns a path for the parent of this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 149 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 150 | * @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] | 151 | */ |
| 152 | public DocumentPath parent() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 153 | if (pathElements.size() <= 1) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 154 | return null; |
| 155 | } |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 156 | return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1)); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 160 | * Returns the list of path elements representing this path in correct |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 161 | * order. |
| 162 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 163 | * @return a list of elements that make up this path |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 164 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 165 | public List<String> pathElements() { |
| 166 | return ImmutableList.copyOf(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Returns if the specified path belongs to a direct ancestor of the node pointed at by this path. |
| 171 | * <p> |
| 172 | * Example: {@code root.a} is a direct ancestor of {@code r.a.b.c}; while {@code r.a.x} is not. |
| 173 | * |
| 174 | * @param other other path |
| 175 | * @return {@code true} is yes; {@code false} otherwise. |
| 176 | */ |
| 177 | public boolean isAncestorOf(DocumentPath other) { |
| 178 | return !other.equals(this) && other.toString().startsWith(toString()); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns if the specified path is belongs to a subtree rooted this path. |
| 183 | * <p> |
| 184 | * Example: {@code root.a.b} and {@code root.a.b.c.d.e} are descendants of {@code r.a.b}; |
| 185 | * while {@code r.a.x.c} is not. |
| 186 | * |
| 187 | * @param other other path |
| 188 | * @return {@code true} is yes; {@code false} otherwise. |
| 189 | */ |
| 190 | public boolean isDescendentOf(DocumentPath other) { |
| 191 | return other.equals(this) || other.isAncestorOf(this); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Returns the path that points to the least common ancestor of the specified |
| 196 | * collection of paths. |
| 197 | * @param paths collection of path |
| 198 | * @return path to least common ancestor |
| 199 | */ |
| 200 | public static DocumentPath leastCommonAncestor(Collection<DocumentPath> paths) { |
| 201 | if (CollectionUtils.isEmpty(paths)) { |
| 202 | return null; |
| 203 | } |
| 204 | return DocumentPath.from(StringUtils.getCommonPrefix(paths.stream() |
| 205 | .map(DocumentPath::toString) |
| 206 | .toArray(String[]::new))); |
| 207 | } |
| 208 | |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 209 | @Override |
| 210 | public int hashCode() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 211 | return Objects.hash(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean equals(Object obj) { |
| 216 | if (obj instanceof DocumentPath) { |
| 217 | DocumentPath that = (DocumentPath) obj; |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 218 | return this.pathElements.equals(that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public String toString() { |
| 225 | StringBuilder stringBuilder = new StringBuilder(); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 226 | Iterator<String> iter = pathElements.iterator(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 227 | while (iter.hasNext()) { |
| 228 | stringBuilder.append(iter.next()); |
| 229 | if (iter.hasNext()) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 230 | stringBuilder.append(pathSeparator); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | return stringBuilder.toString(); |
| 234 | } |
| 235 | |
| 236 | @Override |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 237 | public int compareTo(DocumentPath that) { |
Yuta HIGUCHI | 52e047f | 2017-09-11 16:43:21 -0700 | [diff] [blame^] | 238 | return Comparators.lexicographical(Comparator.<String>naturalOrder()) |
| 239 | .compare(this.pathElements, that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 240 | } |
| 241 | } |