blob: 1c43f4da19ee37dd98931adcdcec97f9b28e5757 [file] [log] [blame]
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -07001/*
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
17package org.onosproject.store.service;
18
Madan Jampaniad5b8c72016-09-12 15:05:01 -070019import java.util.Arrays;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070020import java.util.Iterator;
21import java.util.List;
22import java.util.Objects;
23
Madan Jampani5bdebd52016-09-07 16:18:12 -070024import com.google.common.base.Preconditions;
25import com.google.common.collect.ImmutableList;
26import com.google.common.collect.Lists;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070027
Madan Jampani5bdebd52016-09-07 16:18:12 -070028/**
29 * Unique key for nodes in the {@link DocumentTree}.
30 */
31public class DocumentPath implements Comparable<DocumentPath> {
32
33 private final List<String> pathElements = Lists.newArrayList();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070034
35 /**
36 * Private utility constructor for internal generation of partial paths only.
37 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070038 * @param pathElements list of path elements
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070039 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070040 private DocumentPath(List<String> pathElements) {
41 Preconditions.checkNotNull(pathElements);
42 this.pathElements.addAll(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070043 }
44
45 /**
Thomas Vachuskac3984c62016-09-07 17:51:45 -070046 * Constructs a new document path.
Madan Jampani5bdebd52016-09-07 16:18:12 -070047 * <p>
48 * New paths must contain at least one name and string names may NOT contain any period characters.
49 * If one field is {@code null} that field will be ignored.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070050 *
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070051 * @param nodeName the name of the last level of this path
Madan Jampani5bdebd52016-09-07 16:18:12 -070052 * @param parentPath the path representing the parent leading up to this
53 * node, in the case of the root this should be {@code null}
54 * @throws IllegalDocumentNameException if both parameters are null or name contains an illegal character ('.')
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070055 */
56 public DocumentPath(String nodeName, DocumentPath parentPath) {
57 if (nodeName.contains(".")) {
58 throw new IllegalDocumentNameException(
59 "Periods are not allowed in names.");
60 }
61 if (parentPath != null) {
Madan Jampani5bdebd52016-09-07 16:18:12 -070062 pathElements.addAll(parentPath.pathElements());
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070063 }
64 if (nodeName != null) {
Madan Jampani5bdebd52016-09-07 16:18:12 -070065 pathElements.add(nodeName);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070066 }
Madan Jampani5bdebd52016-09-07 16:18:12 -070067 if (pathElements.isEmpty()) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070068 throw new IllegalDocumentNameException("A document path must contain at" +
69 "least one non-null" +
70 "element.");
71 }
72 }
73
74 /**
Madan Jampaniad5b8c72016-09-12 15:05:01 -070075 * Creates a new {@code DocumentPath} from a period delimited path string.
76 *
77 * @param path path string
78 * @return {@code DocumentPath} instance
79 */
80 public static DocumentPath from(String path) {
81 return new DocumentPath(Arrays.asList(path.split("\\.")));
82 }
83
84 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070085 * Returns a path for the parent of this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070086 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070087 * @return parent node path. If this path is for the root, returns {@code null}.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070088 */
89 public DocumentPath parent() {
Madan Jampani5bdebd52016-09-07 16:18:12 -070090 if (pathElements.size() <= 1) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070091 return null;
92 }
Madan Jampani5bdebd52016-09-07 16:18:12 -070093 return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070094 }
95
96 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070097 * Returns the list of path elements representing this path in correct
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070098 * order.
99 *
Madan Jampani5bdebd52016-09-07 16:18:12 -0700100 * @return a list of elements that make up this path
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700101 */
Madan Jampani5bdebd52016-09-07 16:18:12 -0700102 public List<String> pathElements() {
103 return ImmutableList.copyOf(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700104 }
105
106 @Override
107 public int hashCode() {
Madan Jampani5bdebd52016-09-07 16:18:12 -0700108 return Objects.hash(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (obj instanceof DocumentPath) {
114 DocumentPath that = (DocumentPath) obj;
Madan Jampani5bdebd52016-09-07 16:18:12 -0700115 return this.pathElements.equals(that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700116 }
117 return false;
118 }
119
120 @Override
121 public String toString() {
122 StringBuilder stringBuilder = new StringBuilder();
Madan Jampani5bdebd52016-09-07 16:18:12 -0700123 Iterator<String> iter = pathElements.iterator();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700124 while (iter.hasNext()) {
125 stringBuilder.append(iter.next());
126 if (iter.hasNext()) {
127 stringBuilder.append(".");
128 }
129 }
130 return stringBuilder.toString();
131 }
132
133 @Override
Madan Jampani5bdebd52016-09-07 16:18:12 -0700134 public int compareTo(DocumentPath that) {
135 int shorterLength = this.pathElements.size() > that.pathElements.size()
136 ? that.pathElements.size() : this.pathElements.size();
137 for (int i = 0; i < shorterLength; i++) {
138 if (this.pathElements.get(i).compareTo(that.pathElements.get(i)) != 0) {
139 return this.pathElements.get(i).compareTo(that.pathElements.get(i));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700140 }
141 }
Madan Jampani5bdebd52016-09-07 16:18:12 -0700142
143 if (this.pathElements.size() > that.pathElements.size()) {
144 return 1;
145 } else if (that.pathElements.size() > this.pathElements.size()) {
146 return -1;
147 } else {
148 return 0;
149 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700150 }
151}