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

org.ocpsoft.rewrite.util.Instances Maven / Gradle / Ivy

/*
 * Copyright 2013 Lincoln Baxter, III
 *
 * 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 org.ocpsoft.rewrite.util;

import java.util.Collections;
import java.util.List;

import org.ocpsoft.common.pattern.WeightedComparator;
import org.ocpsoft.common.services.ServiceLoader;
import org.ocpsoft.common.util.Iterators;
import org.ocpsoft.logging.Logger;
import org.ocpsoft.rewrite.spi.InstanceProvider;

/**
 * Utility class for looking up instances of Java types. See also {@link InstanceProvider}.
 * 
 * @author Lincoln Baxter, III
 */
public final class Instances
{
   private static Logger log = Logger.getLogger(Instances.class);

   private static List instanceProviders;

   private Instances()
   {}

   /**
    * Lookup an instance of the given {@link Class} type.
    */
   @SuppressWarnings("unchecked")
   public static  T lookup(Class type)
   {
      if (instanceProviders == null) {
         instanceProviders = Iterators.asList(ServiceLoader.load(InstanceProvider.class));
         Collections.sort(instanceProviders, new WeightedComparator());
         ServiceLogger.logLoadedServices(log, InstanceProvider.class, instanceProviders);
      }

      T result = null;
      for (InstanceProvider p : instanceProviders) {
         result = (T) p.getInstance(type);
         if (result != null)
         {
            break;
         }
      }
      return result;
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy