io.github.pustike.inject.bind.MultiBinder Maven / Gradle / Ivy
Show all versions of pustike-inject Show documentation
/*
* Copyright (C) 2016-2017 the original author or authors.
*
* 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 io.github.pustike.inject.bind;
/**
* An API to bind multiple values separately, to later inject them as a complete collection.
* For ex.
* {@code
* Module snacksModule = binder -> {
* MultiBinder multiBinder = binder.multiBinder(Snack.class);
* multiBinder.addBinding().toInstance(new Twix());
* multiBinder.addBinding().toProvider(SnickersProvider.class);
* multiBinder.addBinding().to(Skittles.class);
* };
* }
*
* With this binding, a {@link java.util.List}{@code } can now be injected, as:
*
*
* class SnackMachine {
* {@literal @}Inject
* public SnackMachine(List<Snack> snacks) { ... }
* }
*
* If desired, {@link java.util.Collection}{@code >} can also be injected.
*
* Contributing multiBindings from different modules is supported. For example, it is okay for both {@code
* CandyModule} and {@code ChipsModule} to create their own {@code MultiBinder}, and to each contribute
* bindings to the list of snacks. When that list is injected, it will contain elements from both modules.
*
* The injected list is unmodifiable and elements can only be added to the list by configuring the multiBinder.
* Elements can not be removed from the list.
*
*
Annotations can be used to create different lists of the same element type. Each distinct annotation gets its
* own independent collection of elements.
* @param the type of the class specified in this multiBinder
*/
public interface MultiBinder {
/**
* Returns the {@link LinkedBindingBuilder} used to add a new element into the list.
*
* It is an error to call this method without also calling one of the {@code to} methods on the
* returned binding builder.
*
*
Scoping elements independently is supported. Use the {@code in} method to specify a binding
* scope.
* @return the linked binding builder
*/
LinkedBindingBuilder addBinding();
}