• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
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 com.google.turbine.binder.lookup;
18 
19 import com.google.common.collect.ImmutableList;
20 import com.google.errorprone.annotations.Immutable;
21 import com.google.turbine.tree.Tree.Ident;
22 import java.util.NoSuchElementException;
23 
24 /**
25  * The key for a qualified name lookup; effectively an immutable iterator over parts of a qualified
26  * name.
27  */
28 @Immutable
29 public class LookupKey {
30   private final ImmutableList<Ident> simpleNames;
31 
LookupKey(ImmutableList<Ident> simpleNames)32   public LookupKey(ImmutableList<Ident> simpleNames) {
33     this.simpleNames = simpleNames;
34   }
35 
36   /** The first simple name in the qualified type name. */
first()37   public Ident first() {
38     return simpleNames.get(0);
39   }
40 
hasNext()41   boolean hasNext() {
42     return simpleNames.size() > 1;
43   }
44 
45   /**
46    * A {@link LookupKey} with the first simple name removed.
47    *
48    * <p>Used when resolving qualified top-level names, e.g. {@code java.util.HashMap.Entry} might be
49    * resolved in the following stages, ending with a resolved class and a {@link LookupKey} for any
50    * remaining nested type names (which may not be canonical).
51    *
52    * <ul>
53    *   <li>{@code ((PACKAGE java) (KEY util.HashMap.Entry))}
54    *   <li>{@code ((PACKAGE java.util) (KEY HashMap.Entry))}
55    *   <li>{@code ((CLASS java.util.HashMap) (KEY Entry))}
56    * </ul>
57    */
rest()58   public LookupKey rest() {
59     if (!hasNext()) {
60       throw new NoSuchElementException();
61     }
62     return new LookupKey(simpleNames.subList(1, simpleNames.size()));
63   }
64 
65   /** The simple names of the type. */
simpleNames()66   public ImmutableList<Ident> simpleNames() {
67     return simpleNames;
68   }
69 }
70