KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > inject > name > Names


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.name;
18
19 import com.google.inject.Binder;
20 import com.google.inject.Key;
21 import com.google.inject.spi.SourceProviders;
22 import com.google.inject.spi.SourceProvider;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 /**
27  * Utility methods for use with {@code @}{@link Named}.
28  *
29  * @author crazybob@google.com (Bob Lee)
30  */

31 public class Names {
32
33   private Names() {}
34
35   static {
36     SourceProviders.skip(Names.class);
37   }
38
39   /**
40    * Creates a {@link Named} annotation with {@code name} as the value.
41    */

42   public static Named named(String JavaDoc name) {
43     return new NamedImpl(name);
44   }
45
46   /**
47    * Creates a constant binding to {@code @Named(key)} for each property.
48    */

49   public static void bindProperties(final Binder binder,
50       final Map JavaDoc<String JavaDoc, String JavaDoc> properties) {
51     SourceProviders.withDefault(
52         new SimpleSourceProvider(SourceProviders.defaultSource()),
53         new Runnable JavaDoc() {
54           public void run() {
55             for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : properties.entrySet()) {
56               String JavaDoc key = entry.getKey();
57               String JavaDoc value = entry.getValue();
58               binder.bind(Key.get(String JavaDoc.class, new NamedImpl(key))).toInstance(value);
59             }
60           }
61         });
62   }
63
64   /**
65    * Creates a constant binding to {@code @Named(key)} for each property.
66    */

67   public static void bindProperties(final Binder binder,
68       final Properties JavaDoc properties) {
69     SourceProviders.withDefault(
70         new SimpleSourceProvider(SourceProviders.defaultSource()),
71         new Runnable JavaDoc() {
72           public void run() {
73             for (Map.Entry JavaDoc<Object JavaDoc, Object JavaDoc> entry : properties.entrySet()) {
74               String JavaDoc key = (String JavaDoc) entry.getKey();
75               String JavaDoc value = (String JavaDoc) entry.getValue();
76               binder.bind(Key.get(String JavaDoc.class, new NamedImpl(key))).toInstance(value);
77             }
78           }
79         });
80   }
81
82   static class SimpleSourceProvider implements SourceProvider {
83
84     final Object JavaDoc source;
85
86     SimpleSourceProvider(Object JavaDoc source) {
87       this.source = source;
88     }
89
90     public Object JavaDoc source() {
91       return source;
92     }
93   }
94 }
95
Popular Tags