it.tidalwave.mobile.LazyLookup Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* 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 f8d2b204b34b 2010/05/30 16:01:47 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import org.openide.util.Lookup;
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
public synchronized T lookup (final @Nonnull Class clazz)
{
logger.fine("lookup(%s)", clazz);
T instance = (T)objectMap.get(clazz);
if (instance == null)
{
final Class extends T> implementationClass = (Class extends T>)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);
}
}
return instance;
}
@Override
public Result lookup(Template template)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy