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

ratpack.guice.internal.GuiceUtil Maven / Gradle / Ivy

Go to download

Integration with Google Guice for Ratpack applications - https://code.google.com/p/google-guice/

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * Copyright 2013 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 ratpack.guice.internal;

import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;
import com.google.inject.*;
import ratpack.func.Action;
import ratpack.func.Function;

import java.util.Map;

import static ratpack.util.ExceptionUtils.uncheck;

public abstract class GuiceUtil {

  private GuiceUtil() {
  }

  public static  void search(Injector injector, TypeToken type, Function, Boolean> transformer) {
    Map, Binding> allBindings = injector.getAllBindings();
    for (Map.Entry, Binding> keyBindingEntry : allBindings.entrySet()) {
      TypeLiteral bindingType = keyBindingEntry.getKey().getTypeLiteral();
      if (type.isAssignableFrom(toTypeToken(bindingType))) {
        @SuppressWarnings("unchecked") Provider provider = (Provider) keyBindingEntry.getValue().getProvider();
        try {
          if (!transformer.apply(provider)) {
            return;
          }
        } catch (Exception e) {
          throw uncheck(e);
        }
      }
    }
    Injector parent = injector.getParent();
    if (parent != null) {
      search(parent, type, transformer);
    }
  }

  public static  void eachOfType(Injector injector, TypeToken type, final Action action) {
    search(injector, type, new Function, Boolean>() {
      @Override
      public Boolean apply(Provider from) throws Exception {
        action.execute(from.get());
        return true;
      }
    });
  }

  public static  void eachProviderOfType(Injector injector, TypeToken type, final Action> action) {
    search(injector, type, new Function, Boolean>() {
      @Override
      public Boolean apply(Provider from) throws Exception {
        action.execute(from);
        return true;
      }
    });
  }

  public static  ImmutableList allOfType(Injector injector, TypeToken type) {
    final ImmutableList.Builder listBuilder = ImmutableList.builder();
    eachOfType(injector, type, new Action() {
      @Override
      public void execute(T thing) throws Exception {
        listBuilder.add(thing);
      }
    });
    return listBuilder.build();
  }

  public static  ImmutableList> allProvidersOfType(Injector injector, TypeToken type) {
    final ImmutableList.Builder> listBuilder = ImmutableList.builder();
    eachProviderOfType(injector, type, new Action>() {
      @Override
      public void execute(Provider thing) throws Exception {
        listBuilder.add(thing);
      }
    });
    return listBuilder.build();
  }

  public static  TypeToken toTypeToken(TypeLiteral type) {
    @SuppressWarnings("unchecked") TypeToken typeToken = (TypeToken) TypeToken.of(type.getType());
    return typeToken;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy