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.9
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: LazyLookup.java,v 0fd53171c67d 2010/06/19 00:28:30 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.util;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;
import it.tidalwave.util.logging.Logger;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class LazyLookup extends Lookup
  {
    private static final String CLASS = LazyLookup.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final Map, Class> classMap = new HashMap, Class>();
    private final Map, Object> objectMap = new HashMap, Object>();

    public void register (final @Nonnull Class interfaceClass, final @Nonnull Class implementationClass)
      {
        classMap.put(interfaceClass, implementationClass);
      }

    @Override @CheckForNull
    public synchronized  T lookup (final @Nonnull Class clazz)
      {
        logger.fine("lookup(%s)", 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)
      {
        logger.fine("lookup(%s)", template);
        final Class clazz = template.getType();

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

            @Override
            public void removeLookupListener(LookupListener l)
              {
              }

            @Override @Nonnull
            public Collection allInstances()
              {
                final List results = new ArrayList();
                T instance = (T)objectMap.get(clazz);

                if (instance == null)
                  {
                    final Class implementationClass = (Class)classMap.get(clazz);

                    if (implementationClass != null)
                      {
                        try
                          {
                            instance = implementationClass.newInstance();
                          }
                        catch (InstantiationException e)
                          {
                            throw new RuntimeException(e);
                          }
                        catch (IllegalAccessException e)
                          {
                            throw new RuntimeException(e);
                          }

                        objectMap.put(clazz, instance);
                      }
                  }

                if (instance != null)
                  {
                    results.add(instance);
                  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy