crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 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 | */ |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 16 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.Provider; |
| 20 | import com.google.inject.ProvisionException; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 21 | import com.google.inject.spi.Dependency; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 22 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 23 | /** |
| 24 | * @author crazybob@google.com (Bob Lee) |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 25 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 26 | final class ProviderToInternalFactoryAdapter<T> implements Provider<T> { |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 27 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 28 | private final InjectorImpl injector; |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 29 | private final InternalFactory<? extends T> internalFactory; |
| 30 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 31 | public ProviderToInternalFactoryAdapter(InjectorImpl injector, |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 32 | InternalFactory<? extends T> internalFactory) { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 33 | this.injector = injector; |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 34 | this.internalFactory = internalFactory; |
| 35 | } |
| 36 | |
| 37 | public T get() { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 38 | final Errors errors = new Errors(); |
| 39 | try { |
| 40 | T t = injector.callInContext(new ContextualCallable<T>() { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 41 | public T call(InternalContext context) throws ErrorsException { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 42 | Dependency dependency = context.getDependency(); |
| 43 | return internalFactory.get(errors, context, dependency); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 44 | } |
| 45 | }); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 46 | errors.throwIfNewErrors(0); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 47 | return t; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 48 | } catch (ErrorsException e) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 49 | throw new ProvisionException(errors.merge(e.getErrors()).getMessages()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 50 | } |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 51 | } |
| 52 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 53 | @Override public String toString() { |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 54 | return internalFactory.toString(); |
| 55 | } |
| 56 | } |