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