blob: 94e6de80190f40513ef6b134ed325792fc8571e7 [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
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 */
crazyboblee7289ac12007-02-01 00:28:09 +000016
17package com.google.inject;
18
kevinb9na312d7a2007-03-01 10:57:35 +000019import com.google.inject.spi.SourceProviders;
20import com.google.inject.util.Objects;
21
crazyboblee7289ac12007-02-01 00:28:09 +000022/**
23 * @author crazybob@google.com (Bob Lee)
24*/
crazybobleebd9544e2007-02-25 20:32:11 +000025class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> {
crazyboblee7289ac12007-02-01 00:28:09 +000026
crazybobleebd9544e2007-02-25 20:32:11 +000027 private final Provider<? extends T> provider;
kevinb9na312d7a2007-03-01 10:57:35 +000028 private final Object source;
crazyboblee7289ac12007-02-01 00:28:09 +000029
crazybobleebd9544e2007-02-25 20:32:11 +000030 public InternalFactoryToProviderAdapter(Provider<? extends T> provider) {
kevinb9na312d7a2007-03-01 10:57:35 +000031 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");
crazyboblee7289ac12007-02-01 00:28:09 +000038 }
39
40 public T get(InternalContext context) {
kevinb9na312d7a2007-03-01 10:57:35 +000041 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));
crazyboblee7289ac12007-02-01 00:28:09 +000048 }
49
50 public String toString() {
crazybobleebd9544e2007-02-25 20:32:11 +000051 return provider.toString();
crazyboblee7289ac12007-02-01 00:28:09 +000052 }
53}