blob: 79b06f7b3ee2b8fb6c780d26974dc2b7fccb4fc4 [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;
20import com.google.inject.ProvisionException;
limpbizkita98bc7a2008-08-29 16:52:44 +000021import com.google.inject.spi.Dependency;
limpbizkit9dc32d42008-06-15 11:29:10 +000022
crazyboblee7289ac12007-02-01 00:28:09 +000023/**
24 * @author crazybob@google.com (Bob Lee)
kevinb9na99dca72007-02-11 04:48:57 +000025 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000026final class ProviderToInternalFactoryAdapter<T> implements Provider<T> {
crazyboblee7289ac12007-02-01 00:28:09 +000027
kevinb9na2915a92007-02-28 06:20:30 +000028 private final InjectorImpl injector;
crazyboblee7289ac12007-02-01 00:28:09 +000029 private final InternalFactory<? extends T> internalFactory;
30
kevinb9na2915a92007-02-28 06:20:30 +000031 public ProviderToInternalFactoryAdapter(InjectorImpl injector,
crazyboblee7289ac12007-02-01 00:28:09 +000032 InternalFactory<? extends T> internalFactory) {
kevinb9na2915a92007-02-28 06:20:30 +000033 this.injector = injector;
crazyboblee7289ac12007-02-01 00:28:09 +000034 this.internalFactory = internalFactory;
35 }
36
37 public T get() {
limpbizkit9dc32d42008-06-15 11:29:10 +000038 final Errors errors = new Errors();
39 try {
40 T t = injector.callInContext(new ContextualCallable<T>() {
limpbizkit163c48a2008-06-16 02:58:08 +000041 public T call(InternalContext context) throws ErrorsException {
limpbizkita98bc7a2008-08-29 16:52:44 +000042 Dependency dependency = context.getDependency();
43 return internalFactory.get(errors, context, dependency);
limpbizkit9dc32d42008-06-15 11:29:10 +000044 }
45 });
limpbizkit72d11dd2008-11-02 07:59:13 +000046 errors.throwIfNewErrors(0);
limpbizkit9dc32d42008-06-15 11:29:10 +000047 return t;
limpbizkit163c48a2008-06-16 02:58:08 +000048 } catch (ErrorsException e) {
limpbizkit490833f2008-11-02 00:12:39 +000049 throw new ProvisionException(errors.merge(e.getErrors()).getMessages());
limpbizkit9dc32d42008-06-15 11:29:10 +000050 }
crazyboblee7289ac12007-02-01 00:28:09 +000051 }
52
limpbizkit9dc32d42008-06-15 11:29:10 +000053 @Override public String toString() {
crazyboblee7289ac12007-02-01 00:28:09 +000054 return internalFactory.toString();
55 }
56}