1 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 ; 24 import java.util.Properties ; 25 26 31 public class Names { 32 33 private Names() {} 34 35 static { 36 SourceProviders.skip(Names.class); 37 } 38 39 42 public static Named named(String name) { 43 return new NamedImpl(name); 44 } 45 46 49 public static void bindProperties(final Binder binder, 50 final Map <String , String > properties) { 51 SourceProviders.withDefault( 52 new SimpleSourceProvider(SourceProviders.defaultSource()), 53 new Runnable () { 54 public void run() { 55 for (Map.Entry <String , String > entry : properties.entrySet()) { 56 String key = entry.getKey(); 57 String value = entry.getValue(); 58 binder.bind(Key.get(String .class, new NamedImpl(key))).toInstance(value); 59 } 60 } 61 }); 62 } 63 64 67 public static void bindProperties(final Binder binder, 68 final Properties properties) { 69 SourceProviders.withDefault( 70 new SimpleSourceProvider(SourceProviders.defaultSource()), 71 new Runnable () { 72 public void run() { 73 for (Map.Entry <Object , Object > entry : properties.entrySet()) { 74 String key = (String ) entry.getKey(); 75 String value = (String ) entry.getValue(); 76 binder.bind(Key.get(String .class, new NamedImpl(key))).toInstance(value); 77 } 78 } 79 }); 80 } 81 82 static class SimpleSourceProvider implements SourceProvider { 83 84 final Object source; 85 86 SimpleSourceProvider(Object source) { 87 this.source = source; 88 } 89 90 public Object source() { 91 return source; 92 } 93 } 94 } 95 | Popular Tags |