Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.commons.collections.CollectionUtils; |
| 22 | import org.apache.commons.lang.StringUtils; |
| 23 | |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 25 | import java.util.Arrays; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 26 | import java.util.Collection; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
| 29 | import java.util.Objects; |
| 30 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 31 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 32 | |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Unique key for nodes in the {@link DocumentTree}. |
| 35 | */ |
| 36 | public class DocumentPath implements Comparable<DocumentPath> { |
| 37 | |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 38 | /** 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 Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 48 | private final List<String> pathElements = Lists.newArrayList(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Private utility constructor for internal generation of partial paths only. |
| 52 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 53 | * @param pathElements list of path elements |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 54 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 55 | private DocumentPath(List<String> pathElements) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 56 | checkNotNull(pathElements); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 57 | this.pathElements.addAll(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Thomas Vachuska | c3984c6 | 2016-09-07 17:51:45 -0700 | [diff] [blame] | 61 | * Constructs a new document path. |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 62 | * <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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 65 | * |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 66 | * @param nodeName the name of the last level of this path |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 67 | * @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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 70 | */ |
| 71 | public DocumentPath(String nodeName, DocumentPath parentPath) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 72 | checkNotNull(nodeName, "Node name cannot be null"); |
| 73 | if (nodeName.contains(pathSeparator)) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 74 | throw new IllegalDocumentNameException( |
| 75 | "Periods are not allowed in names."); |
| 76 | } |
| 77 | if (parentPath != null) { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 78 | pathElements.addAll(parentPath.pathElements()); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 79 | } |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 80 | pathElements.add(nodeName); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 81 | if (pathElements.isEmpty()) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 82 | throw new IllegalDocumentNameException("A document path must contain at" + |
| 83 | "least one non-null" + |
| 84 | "element."); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 89 | * 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 Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 95 | return new DocumentPath(Arrays.asList(path.split(pathSeparatorRE))); |
Madan Jampani | ad5b8c7 | 2016-09-12 15:05:01 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Jordan Halterman | b0ac590 | 2017-07-30 12:31:01 -0700 | [diff] [blame] | 99 | * 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 Punnassery | 589fac2 | 2016-10-03 11:51:53 -0700 | [diff] [blame] | 132 | * 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 Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 143 | * Returns a path for the parent of this node. |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 144 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 145 | * @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] | 146 | */ |
| 147 | public DocumentPath parent() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 148 | if (pathElements.size() <= 1) { |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 149 | return null; |
| 150 | } |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 151 | return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1)); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 155 | * Returns the list of path elements representing this path in correct |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 156 | * order. |
| 157 | * |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 158 | * @return a list of elements that make up this path |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 159 | */ |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 160 | public List<String> pathElements() { |
| 161 | return ImmutableList.copyOf(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 164 | /** |
| 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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 204 | @Override |
| 205 | public int hashCode() { |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 206 | return Objects.hash(pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public boolean equals(Object obj) { |
| 211 | if (obj instanceof DocumentPath) { |
| 212 | DocumentPath that = (DocumentPath) obj; |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 213 | return this.pathElements.equals(that.pathElements); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public String toString() { |
| 220 | StringBuilder stringBuilder = new StringBuilder(); |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 221 | Iterator<String> iter = pathElements.iterator(); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 222 | while (iter.hasNext()) { |
| 223 | stringBuilder.append(iter.next()); |
| 224 | if (iter.hasNext()) { |
Thomas Vachuska | e2bd115 | 2017-03-23 13:42:32 -0700 | [diff] [blame] | 225 | stringBuilder.append(pathSeparator); |
Aaron Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | return stringBuilder.toString(); |
| 229 | } |
| 230 | |
| 231 | @Override |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 232 | 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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
Madan Jampani | 5bdebd5 | 2016-09-07 16:18:12 -0700 | [diff] [blame] | 240 | 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 Kruglikov | b789b5f | 2016-08-31 14:47:05 -0700 | [diff] [blame] | 247 | } |
| 248 | } |