KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) 2007 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.Key.AnnotationStrategy;
20 import com.google.inject.binder.ConstantBindingBuilder;
21 import com.google.inject.binder.AnnotatedConstantBindingBuilder;
22 import com.google.inject.spi.SourceProviders;
23 import com.google.inject.util.Annotations;
24 import com.google.inject.util.StackTraceElements;
25 import com.google.inject.util.Objects;
26 import java.lang.annotation.Annotation JavaDoc;
27
28 /**
29    * Builds a constant binding.
30  */

31 class ConstantBindingBuilderImpl implements AnnotatedConstantBindingBuilder,
32     ConstantBindingBuilder {
33
34   BindingInfo<?> bindingInfo;
35   AnnotationStrategy annotationStrategy;
36   final Object JavaDoc source;
37   private BinderImpl binder;
38
39   ConstantBindingBuilderImpl(BinderImpl binder, Object JavaDoc source) {
40     this.binder = binder;
41     this.source = source;
42   }
43
44   public ConstantBindingBuilder annotatedWith(
45       Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
46     Objects.nonNull(annotationType, "annotation type");
47     validateAnnotation(annotationType);
48     annotationStrategy = Key.strategyFor(annotationType);
49     return this;
50   }
51
52   public ConstantBindingBuilder annotatedWith(Annotation JavaDoc annotation) {
53     Objects.nonNull(annotation, "annotation");
54     validateAnnotation(annotation.annotationType());
55     annotationStrategy = Key.strategyFor(annotation);
56     return this;
57   }
58
59   void validateAnnotation(Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
60     if (annotationStrategy != null) {
61       binder.addError(source, ErrorMessages.ANNOTATION_ALREADY_SPECIFIED);
62       return;
63     }
64
65     boolean retainedAtRuntime =
66         Annotations.isRetainedAtRuntime(annotationType);
67     boolean bindingAnnotation = Key.isBindingAnnotation(annotationType);
68
69     if (!retainedAtRuntime) {
70       binder.addError(StackTraceElements.forType(annotationType),
71           ErrorMessages.MISSING_RUNTIME_RETENTION, source);
72     }
73
74     if (!bindingAnnotation) {
75       binder.addError(StackTraceElements.forType(annotationType),
76           ErrorMessages.MISSING_BINDING_ANNOTATION, source);
77     }
78   }
79
80   boolean hasValue() {
81     return bindingInfo != null;
82   }
83
84   Object JavaDoc getSource() {
85     return source;
86   }
87
88   public void to(String JavaDoc value) {
89     to(String JavaDoc.class, value);
90   }
91
92   public void to(int value) {
93     to(int.class, value);
94   }
95
96   public void to(long value) {
97     to(long.class, value);
98   }
99
100   public void to(boolean value) {
101     to(boolean.class, value);
102   }
103
104   public void to(double value) {
105     to(double.class, value);
106   }
107
108   public void to(float value) {
109     to(float.class, value);
110   }
111
112   public void to(short value) {
113     to(short.class, value);
114   }
115
116   public void to(char value) {
117     to(char.class, value);
118   }
119
120   public void to(Class JavaDoc<?> value) {
121     to(Class JavaDoc.class, value);
122   }
123
124   public <E extends Enum JavaDoc<E>> void to(E value) {
125     to(value.getDeclaringClass(), value);
126   }
127
128   /**
129    * Maps a constant value to the given type and name.
130    */

131   <T> void to(final Class JavaDoc<T> type, final T value) {
132     if (this.bindingInfo != null) {
133       binder.addError(source, ErrorMessages.CONSTANT_VALUE_ALREADY_SET);
134     } else {
135       this.bindingInfo
136           = new BindingInfo<T>(type, value, annotationStrategy, source);
137     }
138   }
139
140   BindingImpl<?> createBinding(InjectorImpl injector) {
141     return bindingInfo.createBinding(injector);
142   }
143
144   private static class BindingInfo<T> {
145
146     final Class JavaDoc<T> type;
147     final T value;
148     final AnnotationStrategy annotationStrategy;
149     final Object JavaDoc source;
150
151     BindingInfo(Class JavaDoc<T> type, T value,
152         AnnotationStrategy annotationStrategy, Object JavaDoc source) {
153       this.type = type;
154       this.value = value;
155       this.annotationStrategy = annotationStrategy;
156       this.source = source;
157     }
158
159     BindingImpl<T> createBinding(InjectorImpl injector) {
160       Key<T> key = Key.get(type, annotationStrategy);
161       ConstantFactory<T> factory = new ConstantFactory<T>(value);
162       return BindingImpl.newInstance(injector, key, source, factory);
163     }
164   }
165 }
166
Popular Tags