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

it.tidalwave.mobile.util.LazyLookup Maven / Gradle / Ivy

There is a newer version: 1.0.16
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Core - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it
 * SCM: https://kenai.com/hg/bluebill~core-src
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.util;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class LazyLookup extends Lookup
  {
    private static final Logger log = LoggerFactory.getLogger(LazyLookup.class);

    private final Map, List>> classMap = new HashMap, List>>();
    private final Map, List> instanceMap = new HashMap, List>();

    public void register (final @Nonnull Class interfaceClass, final @Nonnull Class implementationClass)
      {
        List> classes = classMap.get(interfaceClass);
        
        if (classes == null)
          {
            classes = new ArrayList>();
            classMap.put(interfaceClass, classes);
          }
        
        classes.add(implementationClass);        
      }

    @Override @CheckForNull
    public synchronized  T lookup (final @Nonnull Class clazz)
      {
        log.trace("lookup({})", clazz);
        final Collection instances = lookup(new Template(clazz)).allInstances();
        return instances.isEmpty() ? null : instances.iterator().next();
      }

    @Override @CheckForNull
    public  Result lookup (final @Nonnull Template template)
      {
        log.trace("lookup({})", template);
        final Class type = template.getType();

        return new Result()
          {
            @Override
            public void addLookupListener(LookupListener l)
              {
              }

            @Override
            public void removeLookupListener(LookupListener l)
              {
              }

            @Override @Nonnull
            public Collection allInstances()
              {
                List instances = (List)instanceMap.get(type);

                if (instances == null)
                  {
                    instances = new ArrayList();
                    instanceMap.put(type, instances);
                    final List temp = classMap.get(type); // FIXME: workaround for cast
                    List> implementationClasses = (List>)temp;

                    if (implementationClasses != null)
                      {
                        for (Class implementationClass : implementationClasses) 
                          {
                            try
                              {
                                final T instance = implementationClass.newInstance();
                                instances.add(instance);
                              }
                            catch (InstantiationException e)
                              {
                                throw new RuntimeException(e);
                              }
                            catch (IllegalAccessException e)
                              {
                                throw new RuntimeException(e);
                              }
                          }
                      }
                  }

//                logger.finest(">>>> returning %s", results);
                return Collections.unmodifiableList(instances);
              }
          };
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy