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 | |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame^] | 19 | import com.google.inject.spi.SourceProviders; |
| 20 | import com.google.inject.util.Objects; |
| 21 | |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 22 | /** |
| 23 | * @author crazybob@google.com (Bob Lee) |
| 24 | */ |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 25 | class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> { |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 26 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 27 | private final Provider<? extends T> provider; |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame^] | 28 | private final Object source; |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 29 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 30 | public InternalFactoryToProviderAdapter(Provider<? extends T> provider) { |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame^] | 31 | this(provider, SourceProviders.UNKNOWN_SOURCE); |
| 32 | } |
| 33 | |
| 34 | public InternalFactoryToProviderAdapter( |
| 35 | Provider<? extends T> provider, Object source) { |
| 36 | this.provider = Objects.nonNull(provider, "provider"); |
| 37 | this.source = Objects.nonNull(source, "source"); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | public T get(InternalContext context) { |
kevinb9n | a312d7a | 2007-03-01 10:57:35 +0000 | [diff] [blame^] | 41 | T provided = provider.get(); |
| 42 | if (provided != null) { |
| 43 | return provided; |
| 44 | } |
| 45 | String message = String.format(ErrorMessages.NULL_PROVIDED, source); |
| 46 | throw new ProvisionException(context.getExternalContext(), |
| 47 | new NullPointerException(message)); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | public String toString() { |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 51 | return provider.toString(); |
crazyboblee | 7289ac1 | 2007-02-01 00:28:09 +0000 | [diff] [blame] | 52 | } |
| 53 | } |