Home | History | Annotate | Download | only in internal
      1 /**
      2  * Copyright (C) 2006 Google Inc.
      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.inject.internal;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.inject.Provider;
     22 import com.google.inject.spi.Dependency;
     23 
     24 /**
     25  * @author crazybob (at) google.com (Bob Lee)
     26 */
     27 final class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> {
     28 
     29   private final Provider<? extends T> provider;
     30   private final Object source;
     31 
     32   public InternalFactoryToProviderAdapter(Provider<? extends T> provider, Object source) {
     33     this.provider = checkNotNull(provider, "provider");
     34     this.source = checkNotNull(source, "source");
     35   }
     36 
     37   public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked)
     38       throws ErrorsException {
     39     // TODO(sameb): Does this need to push state into the context?
     40     try {
     41       return errors.checkForNull(provider.get(), source, dependency);
     42     } catch (RuntimeException userException) {
     43       throw errors.withSource(source).errorInProvider(userException).toException();
     44     }
     45   }
     46 
     47   @Override public String toString() {
     48     return provider.toString();
     49   }
     50 }
     51