• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import static com.google.common.base.Preconditions.checkNotNull;
18 import static com.google.common.base.Preconditions.checkState;
19 
20 import com.google.common.annotations.GwtCompatible;
21 
22 import javax.annotation.Nullable;
23 
24 /**
25  * A path to a node in a binary search tree, originating at the root.
26  *
27  * @author Louis Wasserman
28  * @param <N> The type of nodes in this binary search tree.
29  * @param <P> This path type, and the path type of all suffix paths.
30  */
31 @GwtCompatible
32 abstract class BstPath<N extends BstNode<?, N>, P extends BstPath<N, P>> {
33   private final N tip;
34   @Nullable
35   private final P prefix;
36 
BstPath(N tip, @Nullable P prefix)37   BstPath(N tip, @Nullable P prefix) {
38     this.tip = checkNotNull(tip);
39     this.prefix = prefix;
40   }
41 
42   /**
43    * Return the end of this {@code BstPath}, the deepest node in the path.
44    */
getTip()45   public final N getTip() {
46     return tip;
47   }
48 
49   /**
50    * Returns {@code true} if this path has a prefix.
51    */
hasPrefix()52   public final boolean hasPrefix() {
53     return prefix != null;
54   }
55 
56   /**
57    * Returns the prefix of this path, which reaches to the parent of the end of this path. Returns
58    * {@code null} if this path has no prefix.
59    */
60   @Nullable
prefixOrNull()61   public final P prefixOrNull() {
62     return prefix;
63   }
64 
65   /**
66    * Returns the prefix of this path, which reaches to the parent of the end of this path.
67    *
68    * @throws IllegalStateException if this path has no prefix.
69    */
getPrefix()70   public final P getPrefix() {
71     checkState(hasPrefix());
72     return prefix;
73   }
74 }
75