|
Java example source code file (Modules.java)
The Modules.java Java example source code/** * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.inject.util; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.inject.AbstractModule; import com.google.inject.Binder; import com.google.inject.Binding; import com.google.inject.Key; import com.google.inject.Module; import com.google.inject.PrivateBinder; import com.google.inject.PrivateModule; import com.google.inject.Scope; import com.google.inject.internal.Errors; import com.google.inject.spi.DefaultBindingScopingVisitor; import com.google.inject.spi.DefaultElementVisitor; import com.google.inject.spi.Element; import com.google.inject.spi.ElementVisitor; import com.google.inject.spi.Elements; import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding; import com.google.inject.spi.PrivateElements; import com.google.inject.spi.ScopeBinding; import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * Static utility methods for creating and working with instances of {@link Module}. * * @author jessewilson@google.com (Jesse Wilson) * @since 2.0 */ public final class Modules { private Modules() {} public static final Module EMPTY_MODULE = new EmptyModule(); private static class EmptyModule implements Module { public void configure(Binder binder) {} } /** * Returns a builder that creates a module that overlays override modules over the given * modules. If a key is bound in both sets of modules, only the binding from the override modules * is kept. If a single {@link PrivateModule} is supplied or all elements are from * a single {@link PrivateBinder}, then this will overwrite the private bindings. * Otherwise, private bindings will not be overwritten unless they are exposed. * This can be used to replace the bindings of a production module with test bindings: * <pre> * Module functionalTestModule * = Modules.override(new ProductionModule()).with(new TestModule()); * </pre> * * <p>Prefer to write smaller modules that can be reused and tested without overrides. * * @param modules the modules whose bindings are open to be overridden */ public static OverriddenModuleBuilder override(Module... modules) { return new RealOverriddenModuleBuilder(Arrays.asList(modules)); } /** * Returns a builder that creates a module that overlays override modules over the given * modules. If a key is bound in both sets of modules, only the binding from the override modules * is kept. If a single {@link PrivateModule} is supplied or all elements are from * a single {@link PrivateBinder}, then this will overwrite the private bindings. * Otherwise, private bindings will not be overwritten unless they are exposed. * This can be used to replace the bindings of a production module with test bindings: * <pre> * Module functionalTestModule * = Modules.override(getProductionModules()).with(getTestModules()); * </pre> * * <p>Prefer to write smaller modules that can be reused and tested without overrides. * * @param modules the modules whose bindings are open to be overridden */ public static OverriddenModuleBuilder override(Iterable<? extends Module> modules) { return new RealOverriddenModuleBuilder(modules); } /** * Returns a new module that installs all of {@code modules}. */ public static Module combine(Module... modules) { return combine(ImmutableSet.copyOf(modules)); } /** * Returns a new module that installs all of {@code modules}. */ public static Module combine(Iterable<? extends Module> modules) { return new CombinedModule(modules); } private static class CombinedModule implements Module { final Set<Module> modulesSet; CombinedModule(Iterable<? extends Module> modules) { this.modulesSet = ImmutableSet.copyOf(modules); } public void configure(Binder binder) { binder = binder.skipSources(getClass()); for (Module module : modulesSet) { binder.install(module); } } } /** * See the EDSL example at {@link Modules#override(Module[]) override()}. */ public interface OverriddenModuleBuilder { /** * See the EDSL example at {@link Modules#override(Module[]) override()}. */ Module with(Module... overrides); /** * See the EDSL example at {@link Modules#override(Module[]) override()}. */ Module with(Iterable<? extends Module> overrides); } private static final class RealOverriddenModuleBuilder implements OverriddenModuleBuilder { private final ImmutableSet<Module> baseModules; private RealOverriddenModuleBuilder(Iterable<? extends Module> baseModules) { this.baseModules = ImmutableSet.copyOf(baseModules); } public Module with(Module... overrides) { return with(Arrays.asList(overrides)); } public Module with(Iterable<? extends Module> overrides) { return new OverrideModule(overrides, baseModules); } } static class OverrideModule extends AbstractModule { private final ImmutableSet<Module> overrides; private final ImmutableSet<Module> baseModules; OverrideModule(Iterable<? extends Module> overrides, ImmutableSet Other Java examples (source code examples)Here is a short list of links related to this Java Modules.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.