All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.instaclustr.operations.OperationBindings Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.instaclustr.operations;

import com.google.inject.Binder;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.util.Types;

public final class OperationBindings {
    private OperationBindings() {
    }

    /**
     * Install the various bindings and AssistedInject {@link OperationFactory}s for
     * a {@link Operation} and its associated {@link OperationRequest} type.
     * 

* This allows for automatic JSON serialization/deserialization (via {@link MapBackedTypeIdResolver}), * and creation of Operations from their Requests. * @param binder binder to use * @param typeId id of type * @param requestClass request class * @param operationClass operation class * @param type of request * @param type of operation */ public static > void installOperationBindings(final Binder binder, final String typeId, final Class requestClass, final Class operationClass) { @SuppressWarnings("unchecked") final TypeLiteral> operationFactoryType = (TypeLiteral>) TypeLiteral.get( Types.newParameterizedType(OperationFactory.class, requestClass) ); final TypeLiteral> operationRequestClassType = new TypeLiteral>() { }; final TypeLiteral> operationClassType = new TypeLiteral>() { }; // get Guice to create the AssistedInject OperationFactory implementation for the Operation class // to allow creation Operations from their OperationRequests binder.install(new FactoryModuleBuilder() .implement(Operation.class, operationClass) .build(operationFactoryType)); // add an entry to the Map, OperationFactory> for the factory created above MapBinder.newMapBinder(binder, operationRequestClassType, TypeLiteral.get(OperationFactory.class)) .addBinding(requestClass).to(operationFactoryType); // add an entry to the Map> for the typeId. // this allows OperationRequest.TypeIdResolver to lookup the Class for a given typeId (and vice versa) MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), operationRequestClassType) .addBinding(typeId).toInstance(requestClass); // add an entry to the Map> for the typeId. // this allows Operation.TypeIdResolver to lookup the Class for a given typeId (and vice versa) MapBinder.newMapBinder(binder, TypeLiteral.get(String.class), operationClassType) .addBinding(typeId).toInstance(operationClass); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy