blob: 4e97cd4a0a2ce87a5fdfbcfeb29683df87ab4179 [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
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070019import java.util.Iterator;
20import java.util.List;
21import java.util.Objects;
22
Madan Jampani5bdebd52016-09-07 16:18:12 -070023import com.google.common.base.Preconditions;
24import com.google.common.collect.ImmutableList;
25import com.google.common.collect.Lists;
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070026
Madan Jampani5bdebd52016-09-07 16:18:12 -070027/**
28 * Unique key for nodes in the {@link DocumentTree}.
29 */
30public class DocumentPath implements Comparable<DocumentPath> {
31
32 private final List<String> pathElements = Lists.newArrayList();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070033
34 /**
35 * Private utility constructor for internal generation of partial paths only.
36 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070037 * @param pathElements list of path elements
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070038 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070039 private DocumentPath(List<String> pathElements) {
40 Preconditions.checkNotNull(pathElements);
41 this.pathElements.addAll(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070042 }
43
44 /**
Thomas Vachuskac3984c62016-09-07 17:51:45 -070045 * Constructs a new document path.
Madan Jampani5bdebd52016-09-07 16:18:12 -070046 * <p>
47 * New paths must contain at least one name and string names may NOT contain any period characters.
48 * If one field is {@code null} that field will be ignored.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070049 *
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070050 * @param nodeName the name of the last level of this path
Madan Jampani5bdebd52016-09-07 16:18:12 -070051 * @param parentPath the path representing the parent leading up to this
52 * node, in the case of the root this should be {@code null}
53 * @throws IllegalDocumentNameException if both parameters are null or name contains an illegal character ('.')
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070054 */
55 public DocumentPath(String nodeName, DocumentPath parentPath) {
56 if (nodeName.contains(".")) {
57 throw new IllegalDocumentNameException(
58 "Periods are not allowed in names.");
59 }
60 if (parentPath != null) {
Madan Jampani5bdebd52016-09-07 16:18:12 -070061 pathElements.addAll(parentPath.pathElements());
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070062 }
63 if (nodeName != null) {
Madan Jampani5bdebd52016-09-07 16:18:12 -070064 pathElements.add(nodeName);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070065 }
Madan Jampani5bdebd52016-09-07 16:18:12 -070066 if (pathElements.isEmpty()) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070067 throw new IllegalDocumentNameException("A document path must contain at" +
68 "least one non-null" +
69 "element.");
70 }
71 }
72
73 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070074 * Returns a path for the parent of this node.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070075 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070076 * @return parent node path. If this path is for the root, returns {@code null}.
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070077 */
78 public DocumentPath parent() {
Madan Jampani5bdebd52016-09-07 16:18:12 -070079 if (pathElements.size() <= 1) {
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070080 return null;
81 }
Madan Jampani5bdebd52016-09-07 16:18:12 -070082 return new DocumentPath(this.pathElements.subList(0, pathElements.size() - 1));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070083 }
84
85 /**
Madan Jampani5bdebd52016-09-07 16:18:12 -070086 * Returns the list of path elements representing this path in correct
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070087 * order.
88 *
Madan Jampani5bdebd52016-09-07 16:18:12 -070089 * @return a list of elements that make up this path
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070090 */
Madan Jampani5bdebd52016-09-07 16:18:12 -070091 public List<String> pathElements() {
92 return ImmutableList.copyOf(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070093 }
94
95 @Override
96 public int hashCode() {
Madan Jampani5bdebd52016-09-07 16:18:12 -070097 return Objects.hash(pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -070098 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (obj instanceof DocumentPath) {
103 DocumentPath that = (DocumentPath) obj;
Madan Jampani5bdebd52016-09-07 16:18:12 -0700104 return this.pathElements.equals(that.pathElements);
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700105 }
106 return false;
107 }
108
109 @Override
110 public String toString() {
111 StringBuilder stringBuilder = new StringBuilder();
Madan Jampani5bdebd52016-09-07 16:18:12 -0700112 Iterator<String> iter = pathElements.iterator();
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700113 while (iter.hasNext()) {
114 stringBuilder.append(iter.next());
115 if (iter.hasNext()) {
116 stringBuilder.append(".");
117 }
118 }
119 return stringBuilder.toString();
120 }
121
122 @Override
Madan Jampani5bdebd52016-09-07 16:18:12 -0700123 public int compareTo(DocumentPath that) {
124 int shorterLength = this.pathElements.size() > that.pathElements.size()
125 ? that.pathElements.size() : this.pathElements.size();
126 for (int i = 0; i < shorterLength; i++) {
127 if (this.pathElements.get(i).compareTo(that.pathElements.get(i)) != 0) {
128 return this.pathElements.get(i).compareTo(that.pathElements.get(i));
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700129 }
130 }
Madan Jampani5bdebd52016-09-07 16:18:12 -0700131
132 if (this.pathElements.size() > that.pathElements.size()) {
133 return 1;
134 } else if (that.pathElements.size() > this.pathElements.size()) {
135 return -1;
136 } else {
137 return 0;
138 }
Aaron Kruglikovb789b5f2016-08-31 14:47:05 -0700139 }
140}