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