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

it.tidalwave.metadata.MetadataCachedLookup Maven / Gradle / Ivy

/*******************************************************************************
 *
 * blueMarine - open source photo workflow
 * =======================================
 *
 * Copyright (C) 2003-2009 by Fabrizio Giudici
 * Project home page: http://bluemarine.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. 
 *
 *******************************************************************************
 *
 * $Id: MetadataCachedLookup.java,v d60681e7fc1d 2009/09/17 23:59:23 fabrizio $
 *
 ******************************************************************************/
package it.tidalwave.metadata;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Collections;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;

/*******************************************************************************
 *
 * 
 * FIXME: move this to Metadata!
 * 
 * @author  Fabrizio Giudici
 * @version $Id: MetadataCachedLookup.java,v d60681e7fc1d 2009/09/17 23:59:23 fabrizio $
 *
 ******************************************************************************/
public class MetadataCachedLookup extends Lookup
  {
    protected static final int LIVE_REFERENCE_COUNT = 20;
    
    /** This queue prevents that a number of the latest retrieved metadata are GCed. */
    protected static final Queue referenceKeeper = new ArrayBlockingQueue(LIVE_REFERENCE_COUNT);

    @Nonnull
    protected final DataObject dataObject;

    @CheckForNull
    protected WeakReference metadataRef;

    public MetadataCachedLookup (@Nonnull final DataObject dataObject)
      {
        this.dataObject = dataObject;
      }
    
    @Override
    @CheckForNull
    public synchronized  T lookup (@Nonnull final Class clazz)
      {
        if (Metadata.class.equals(clazz))
          {
            Metadata metadata = null;
            
            if (metadataRef != null)
              {
                metadata = metadataRef.get();
              }

            if (metadata == null)
              {
                metadata = MetadataFactory.Locator.findMetadataFactory().findOrCreateMetadata(dataObject);
                
                if (referenceKeeper.size() == LIVE_REFERENCE_COUNT)
                  {
                    referenceKeeper.poll();
                  }
                
                referenceKeeper.add(metadata);
                metadataRef = new WeakReference(metadata);
              }

            return (T)metadata;
          }  
        
        return null;
      }

    @Override
    @Nonnull
    public  Result lookup (@Nonnull final Template template)
      {
        final T metadata = lookup(template.getType());
        
        return new Lookup.Result() 
          {
            @Override
            public void addLookupListener (final LookupListener listener) 
              {
              }  

            @Override
            public void removeLookupListener (final LookupListener listener) 
              {
              }

            @Override
            public Collection allInstances() 
              {
                return (metadata != null) ? Collections.singletonList(metadata) : Collections.emptyList();
              }
          };
      }

    @Override
    @Nonnull
    public String toString() 
      {
        return "MetadataCachedLookup";
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy