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