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

org.jclouds.providers.Providers Maven / Gradle / Ivy

There is a newer version: 2.6.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.jclouds.providers;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find;
import static org.jclouds.reflect.Reflection2.typeToken;

import java.util.NoSuchElementException;
import java.util.ServiceLoader;

import org.jclouds.Context;
import org.jclouds.View;
import org.jclouds.apis.ApiMetadata;
import org.jclouds.osgi.ProviderRegistry;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;

/**
 * The Providers class provides static methods for accessing providers.
 */
public class Providers {

   public static enum IdFunction implements Function {
      INSTANCE;

      @Override
      public String apply(ProviderMetadata input) {
         return input.getId();
      }

   }

   public static Function idFunction() {
      return IdFunction.INSTANCE;
   }

   public static class ApiMetadataFunction implements Function {
      @Override
      public ApiMetadata apply(ProviderMetadata input) {
         return input.getApiMetadata();
      }

   }

   public static Function apiMetadataFunction() {
      return new ApiMetadataFunction();
   }

   /**
    * Returns the providers located on the classpath via {@link java.util.ServiceLoader}.
    * 
    * @return all available providers loaded from classpath via ServiceLoader
    */
   public static Iterable fromServiceLoader() {
      return ServiceLoader.load(ProviderMetadata.class);
   }

   /**
    * Returns all available providers.
    * 
    * @return all available providers
    */
   public static Iterable all() {
     return ImmutableSet.builder()
                        .addAll(fromServiceLoader())
                        .addAll(ProviderRegistry.fromRegistry()).build();
   }

   /**
    * Returns the first provider with the provided id
    * 
    * @param id
    *           the id of the provider to return
    * 
    * @return the provider with the given id
    * 
    * @throws NoSuchElementException
    *            whenever there are no providers with the provided id
    */
   public static ProviderMetadata withId(String id) throws NoSuchElementException {
      return find(all(), ProviderPredicates.id(id));
   }

   /**
    * Returns the providers that are of the provided viewableAs.
    * 
    * @param viewableAs
    *           the viewableAs to providers to return
    * 
    * @return the providers of the provided viewableAs
    */
   public static Iterable viewableAs(TypeToken viewableAs) {
      return filter(all(), ProviderPredicates.viewableAs(viewableAs));
   }

   public static Iterable viewableAs(Class viewableAs) {
      return filter(all(), ProviderPredicates.viewableAs(typeToken(viewableAs)));
   }

   /**
    * Returns the providers that are of the provided api.
    * 
    * @param api
    *           the api to providers to return
    * 
    * @return the providers of the provided api
    */
   public static Iterable apiMetadataAssignableFrom(TypeToken api) {
      Preconditions.checkNotNull(api, "api must be defined");
      return filter(all(), ProviderPredicates.apiMetadataAssignableFrom(api));
   }

   /**
    * Returns the providers that are of the provided context.
    * 
    * @param context
    *           the context to providers to return
    * 
    * @return the providers of the provided context
    */
   public static  Iterable contextAssignableFrom(
            TypeToken context) {
      Preconditions.checkNotNull(context, "context must be defined");
      return filter(all(), new ProviderPredicates.ContextAssignableFrom(context));
   }

   /**
    * Returns the providers that are bound to the same location as the given ISO 3166 code
    * regardless of viewableAs.
    * 
    * @param isoCode
    *           the ISO 3166 code to filter providers by
    * 
    * @return the providers bound by the given ISO 3166 code
    */
   public static Iterable boundedByIso3166Code(String iso3166Code) {
      return filter(all(), ProviderPredicates.boundedByIso3166Code(iso3166Code));
   }

   /**
    * Returns the providers that are bound to the same location as the given ISO 3166 code and of
    * the given viewableAs.
    * 
    * @param iso3166Code
    *           the ISO 3166 code to filter providers by
    * @param viewableAs
    *           the viewableAs to filter providers by
    * 
    * @return the providers bound by the given ISO 3166 code and of the proper viewableAs
    */
   public static Iterable boundedByIso3166Code(String iso3166Code,
            TypeToken viewableAs) {
      return filter(all(), Predicates.and(ProviderPredicates.boundedByIso3166Code(iso3166Code), ProviderPredicates
               .viewableAs(viewableAs)));
   }

   public static Iterable boundedByIso3166Code(String iso3166Code,
            Class viewableAs) {
      return boundedByIso3166Code(iso3166Code, typeToken(viewableAs));
   }

   /**
    * Returns the providers that have at least one common ISO 3166 code in common regardless of
    * viewableAs.
    * 
    * @param providerMetadata
    *           the provider metadata to use to filter providers by
    * 
    * @return the providers that share at least one common ISO 3166 code
    */
   public static Iterable collocatedWith(ProviderMetadata providerMetadata) {
      return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata));
   }

   /**
    * Returns the providers that have at least one common ISO 3166 code and are of the given
    * viewableAs.
    * 
    * @param providerMetadata
    *           the provider metadata to use to filter providers by
    * @param viewableAs
    *           the viewableAs to filter providers by
    * 
    * @return the providers that share at least one common ISO 3166 code and of the given
    *         viewableAs
    */
   public static Iterable collocatedWith(ProviderMetadata providerMetadata,
            TypeToken viewableAs) {
      return filter(all(), Predicates.and(ProviderPredicates.intersectingIso3166Code(providerMetadata),
               ProviderPredicates.viewableAs(viewableAs)));
   }

   public static Iterable collocatedWith(ProviderMetadata providerMetadata,
            Class viewableAs) {
      return collocatedWith(providerMetadata, typeToken(viewableAs));
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy