KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > InternalFactoryToProviderAdapter


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;
18
19 import com.google.inject.spi.SourceProviders;
20 import com.google.inject.util.Objects;
21
22 /**
23  * @author crazybob@google.com (Bob Lee)
24 */

25 class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> {
26
27   private final Provider<? extends T> provider;
28   private final Object JavaDoc source;
29
30   public InternalFactoryToProviderAdapter(Provider<? extends T> provider) {
31     this(provider, SourceProviders.UNKNOWN_SOURCE);
32   }
33
34   public InternalFactoryToProviderAdapter(
35       Provider<? extends T> provider, Object JavaDoc source) {
36     this.provider = Objects.nonNull(provider, "provider");
37     this.source = Objects.nonNull(source, "source");
38   }
39   
40   public T get(InternalContext context) {
41     T provided = provider.get();
42     if (provided != null) {
43       return provided;
44     }
45
46     // TODO(kevinb): gee, ya think we might want to remove this?
47
if (("I'm a bad hack".equals(
48         System.getProperty("guice.allow.nulls.bad.bad.bad")))) {
49       return provided;
50     }
51     String JavaDoc message = String.format(ErrorMessages.NULL_PROVIDED, source);
52     throw new ProvisionException(context.getExternalContext(),
53         new NullPointerException JavaDoc(message));
54   }
55
56   public String JavaDoc toString() {
57     return provider.toString();
58   }
59 }
60
Popular Tags