📄 containerbuilder.java
字号:
return factory(type, Container.DEFAULT_NAME, type); } /** * Convenience method. Equivalent to {@code factory(type, name, type)}. * * @see #factory(Class, String, Class) */ public <T> ContainerBuilder factory(Class<T> type, String name) { return factory(type, name, type); } /** * Convenience method. Equivalent to {@code factory(type, * Container.DEFAULT_NAME, implementation, scope)}. * * @see #factory(Class, String, Class, Scope) */ public <T> ContainerBuilder factory(Class<T> type, Class<? extends T> implementation, Scope scope) { return factory(type, Container.DEFAULT_NAME, implementation, scope); } /** * Convenience method. Equivalent to {@code factory(type, * Container.DEFAULT_NAME, type, scope)}. * * @see #factory(Class, String, Class, Scope) */ public <T> ContainerBuilder factory(Class<T> type, Scope scope) { return factory(type, Container.DEFAULT_NAME, type, scope); } /** * Convenience method. Equivalent to {@code factory(type, name, type, * scope)}. * * @see #factory(Class, String, Class, Scope) */ public <T> ContainerBuilder factory(Class<T> type, String name, Scope scope) { return factory(type, name, type, scope); } /** * Convenience method. Equivalent to {@code alias(type, Container.DEFAULT_NAME, * type)}. * * @see #alias(Class, String, String) */ public <T> ContainerBuilder alias(Class<T> type, String alias) { return alias(type, Container.DEFAULT_NAME, alias); } /** * Maps an existing factory to a new name. * * @param type of dependency * @param name of dependency * @param alias of to the dependency * @return this builder */ public <T> ContainerBuilder alias(Class<T> type, String name, String alias) { return alias(Key.newInstance(type, name), Key.newInstance(type, alias)); } /** * Maps an existing dependency. All methods in this class ultimately funnel through * here. */ private <T> ContainerBuilder alias(final Key<T> key, final Key<T> aliasKey) { ensureNotCreated(); checkKey(aliasKey); final InternalFactory<? extends T> scopedFactory = (InternalFactory<? extends T>)factories.get(key); if (scopedFactory == null) { throw new DependencyException( "Dependency mapping for " + key + " doesn't exists."); } factories.put(aliasKey, scopedFactory); return this; } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, String value) { return constant(String.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, int value) { return constant(int.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, long value) { return constant(long.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, boolean value) { return constant(boolean.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, double value) { return constant(double.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, float value) { return constant(float.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, short value) { return constant(short.class, name, value); } /** * Maps a constant value to the given name. */ public ContainerBuilder constant(String name, char value) { return constant(char.class, name, value); } /** * Maps a class to the given name. */ public ContainerBuilder constant(String name, Class value) { return constant(Class.class, name, value); } /** * Maps an enum to the given name. */ public <E extends Enum<E>> ContainerBuilder constant(String name, E value) { return constant(value.getDeclaringClass(), name, value); } /** * Maps a constant value to the given type and name. */ private <T> ContainerBuilder constant(final Class<T> type, final String name, final T value) { InternalFactory<T> factory = new InternalFactory<T>() { public T create(InternalContext ignored) { return value; } public String toString() { return new LinkedHashMap<String, Object>() { { put("type", type); put("name", name); put("value", value); } }.toString(); } }; return factory(Key.newInstance(type, name), factory, Scope.DEFAULT); } /** * Upon creation, the {@link Container} will inject static fields and methods * into the given classes. * * @param types for which static members will be injected */ public ContainerBuilder injectStatics(Class<?>... types) { staticInjections.addAll(Arrays.asList(types)); return this; } /** * Returns true if this builder contains a mapping for the given type and * name. */ public boolean contains(Class<?> type, String name) { return factories.containsKey(Key.newInstance(type, name)); } /** * Convenience method. Equivalent to {@code contains(type, * Container.DEFAULT_NAME)}. */ public boolean contains(Class<?> type) { return contains(type, Container.DEFAULT_NAME); } /** * Creates a {@link Container} instance. Injects static members for classes * which were registered using {@link #injectStatics(Class...)}. * * @param loadSingletons If true, the container will load all singletons * now. If false, the container will lazily load singletons. Eager loading * is appropriate for production use while lazy loading can speed * development. * @throws IllegalStateException if called more than once */ public Container create(boolean loadSingletons) { ensureNotCreated(); created = true; final ContainerImpl container = new ContainerImpl( new HashMap<Key<?>, InternalFactory<?>>(factories)); if (loadSingletons) { container.callInContext(new ContainerImpl.ContextualCallable<Void>() { public Void call(InternalContext context) { for (InternalFactory<?> factory : singletonFactories) { factory.create(context); } return null; } }); } container.injectStatics(staticInjections); return container; } /** * Currently we only support creating one Container instance per builder. * If we want to support creating more than one container per builder, * we should move to a "factory factory" model where we create a factory * instance per Container. Right now, one factory instance would be * shared across all the containers, singletons synchronize on the * container when lazy loading, etc. */ private void ensureNotCreated() { if (created) { throw new IllegalStateException("Container already created."); } } /** * Implemented by classes which participate in building a container. */ public interface Command { /** * Contributes factories to the given builder. * * @param builder */ void build(ContainerBuilder builder); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -