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 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Lists; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Iterator; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| 29 | * A path class for identifying nodes within the {@code DocumentTree}. |
| 30 | * |
| 31 | * Note: indexing is ONE based so the root is the 1st level, to retrieve the |
| 32 | * root one should query level 1. |
| 33 | */ |
| 34 | public class DocumentPath implements Comparable { |
| 35 | |
| 36 | private final ArrayList<String> tokens = Lists.newArrayList(); |
| 37 | |
| 38 | /** |
| 39 | * Private utility constructor for internal generation of partial paths only. |
| 40 | * |
| 41 | * @param path |
| 42 | */ |
| 43 | private DocumentPath(List<String> path) { |
| 44 | Preconditions.checkNotNull(path); |
| 45 | this.tokens.addAll(path); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Constructor to generate new {@DocumentPath}, new paths must contain at |
| 50 | * least one name and string names may NOT contain any '.'s. If one field |
| 51 | * is null that field will be ignored. |
| 52 | * |
| 53 | * @throws IllegalDocumentNameException if both parameters are null or the string |
| 54 | * name contains an illegal character ('.') |
| 55 | * @param nodeName the name of the last level of this path |
| 56 | * @param parentPath the path representing the parents leading up to this |
| 57 | * node, in the case of the root this should be null |
| 58 | */ |
| 59 | public DocumentPath(String nodeName, DocumentPath parentPath) { |
| 60 | if (nodeName.contains(".")) { |
| 61 | throw new IllegalDocumentNameException( |
| 62 | "Periods are not allowed in names."); |
| 63 | } |
| 64 | if (parentPath != null) { |
| 65 | tokens.addAll(parentPath.path()); |
| 66 | } |
| 67 | if (nodeName != null) { |
| 68 | tokens.add(nodeName); |
| 69 | } |
| 70 | if (tokens.isEmpty()) { |
| 71 | throw new IllegalDocumentNameException("A document path must contain at" + |
| 72 | "least one non-null" + |
| 73 | "element."); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns a path from the root to the parent of this node, if this node is |
| 79 | * the root this call returns null. |
| 80 | * |
| 81 | * @return a {@code DocumentPath} representing a path to this paths parent, |
| 82 | * null if this a root path |
| 83 | */ |
| 84 | public DocumentPath parent() { |
| 85 | if (tokens.size() <= 1) { |
| 86 | return null; |
| 87 | } |
| 88 | return new DocumentPath(this.tokens.subList(0, tokens.size() - 1)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the complete list of tokens representing this path in correct |
| 93 | * order. |
| 94 | * |
| 95 | * @return a list of strings representing this path |
| 96 | */ |
| 97 | public List<String> path() { |
| 98 | return ImmutableList.copyOf(tokens); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int hashCode() { |
| 103 | return Objects.hash(tokens); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public boolean equals(Object obj) { |
| 108 | if (obj instanceof DocumentPath) { |
| 109 | DocumentPath that = (DocumentPath) obj; |
| 110 | return this.tokens.equals(that.tokens); |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | StringBuilder stringBuilder = new StringBuilder(); |
| 118 | Iterator<String> iter = tokens.iterator(); |
| 119 | while (iter.hasNext()) { |
| 120 | stringBuilder.append(iter.next()); |
| 121 | if (iter.hasNext()) { |
| 122 | stringBuilder.append("."); |
| 123 | } |
| 124 | } |
| 125 | return stringBuilder.toString(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public int compareTo(Object o) { |
| 130 | if (o instanceof DocumentPath) { |
| 131 | DocumentPath that = (DocumentPath) o; |
| 132 | int shorterLength = this.tokens.size() > that.tokens.size() ? |
| 133 | that.tokens.size() : this.tokens.size(); |
| 134 | for (int i = 0; i < shorterLength; i++) { |
| 135 | if (this.tokens.get(i).compareTo(that.tokens.get(i)) != 0) { |
| 136 | return this.tokens.get(i).compareTo(that.tokens.get(i)); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (this.tokens.size() > that.tokens.size()) { |
| 141 | return 1; |
| 142 | } else if (that.tokens.size() > this.tokens.size()) { |
| 143 | return -1; |
| 144 | } else { |
| 145 | return 0; |
| 146 | } |
| 147 | } |
| 148 | throw new IllegalArgumentException("Compare can only compare objects" + |
| 149 | "of the same type."); |
| 150 | } |
| 151 | } |