blob: 69e5c55a4bc867f338a97a822d5cf1d93c071fff [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
crazyboblee7289ac12007-02-01 00:28:09 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Provider;
limpbizkit53664a72009-02-21 00:25:27 +000020import static com.google.inject.internal.Preconditions.checkNotNull;
limpbizkita98bc7a2008-08-29 16:52:44 +000021import com.google.inject.spi.Dependency;
kevinb9na312d7a2007-03-01 10:57:35 +000022
crazyboblee7289ac12007-02-01 00:28:09 +000023/**
24 * @author crazybob@google.com (Bob Lee)
25*/
limpbizkit5ae41eb2009-06-06 17:51:27 +000026final class InternalFactoryToProviderAdapter<T> implements InternalFactory<T> {
crazyboblee7289ac12007-02-01 00:28:09 +000027
limpbizkit5fb9d922008-10-14 23:35:56 +000028 private final Initializable<Provider<? extends T>> initializable;
kevinb9na312d7a2007-03-01 10:57:35 +000029 private final Object source;
crazyboblee7289ac12007-02-01 00:28:09 +000030
limpbizkit5fb9d922008-10-14 23:35:56 +000031 public InternalFactoryToProviderAdapter(Initializable<Provider<? extends T>> initializable) {
32 this(initializable, SourceProvider.UNKNOWN_SOURCE);
kevinb9na312d7a2007-03-01 10:57:35 +000033 }
34
35 public InternalFactoryToProviderAdapter(
limpbizkit5fb9d922008-10-14 23:35:56 +000036 Initializable<Provider<? extends T>> initializable, Object source) {
37 this.initializable = checkNotNull(initializable, "provider");
kevinb9n1601ae52008-06-03 22:21:04 +000038 this.source = checkNotNull(source, "source");
crazyboblee7289ac12007-02-01 00:28:09 +000039 }
limpbizkit51515b52008-03-11 03:46:25 +000040
limpbizkita98bc7a2008-08-29 16:52:44 +000041 public T get(Errors errors, InternalContext context, Dependency<?> dependency)
limpbizkit163c48a2008-06-16 02:58:08 +000042 throws ErrorsException {
limpbizkite4647a62008-05-25 18:03:35 +000043 try {
limpbizkit5fb9d922008-10-14 23:35:56 +000044 return errors.checkForNull(initializable.get(errors).get(), source, dependency);
limpbizkit9dc32d42008-06-15 11:29:10 +000045 } catch (RuntimeException userException) {
limpbizkit490833f2008-11-02 00:12:39 +000046 throw errors.withSource(source).errorInProvider(userException).toException();
limpbizkite4647a62008-05-25 18:03:35 +000047 }
crazyboblee7289ac12007-02-01 00:28:09 +000048 }
49
limpbizkit9dc32d42008-06-15 11:29:10 +000050 @Override public String toString() {
limpbizkit5fb9d922008-10-14 23:35:56 +000051 return initializable.toString();
crazyboblee7289ac12007-02-01 00:28:09 +000052 }
crazyboblee7289ac12007-02-01 00:28:09 +000053}