Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.api.CacheManager.java

Here you can find all the information about the file org.dgpf.search.api.CacheManager.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-03-30 09:45:08
 * Original Filename: org.dgpf.search.api.CacheManager.java
 * Version          : 2.0.0
 * Last modification: 2006-05-08
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.dgpf.search.api;

import java.util.List;

import org.sfc.collections.Collections;

/**
 * This class is used internally to manage the caches.
 *
 * @author Thomas Weise
 */

final class CacheManager
  {
/**
 * The internally used instance for cache management.
 */

  static  final CacheManager  INSTANCE  = new CacheManager();

/**
 * The list with the caches.
 */

  private final List<CacheInstance> m_list ;
  
/**
 * Instantiate the cache management. 
 */

  private CacheManager()
    {
    super();
    this.m_list = Collections.create_list(-1);
    }

  
/**
 * Obtain a cache instance to be used.
 * @param p_class         The class of the search context that wants to use
 *                        the cache.
 * @param p_definition    The underlying search definition.
 * @return  A simple cache instance.
 */

  synchronized final SearchCache get_cache(final Class             p_class,
                                         final SearchDefinition  p_definition)
    {
    final List<CacheInstance> l_l;
    final Thread              l_tg;
          CacheInstance       l_ci;
          int                 l_i;
    
    
    l_l  = this.m_list;
    l_tg = Thread.currentThread();
    
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_ci = l_l.get(l_i);
      if( (l_ci.m_class      == p_class) &&
          (l_ci.m_definition == p_definition) &&
          (l_ci.m_thread     == l_tg) )
        {
        l_ci.m_ref_count++;
        return l_ci.m_cache;
        }
      }
    
    l_ci = new CacheInstance(p_class, p_definition, l_tg);
    l_l.add(l_ci);
    
    return l_ci.m_cache;
    }
  
/**
 * Remove a cache.
 * @param p_cache The cache to be removed.
 */

  synchronized  final void  drop_cache  (final SearchCache p_cache)
    {
    final List<CacheInstance> l_l;
          CacheInstance       l_ci;
          int                 l_i;
    
    
    l_l  = this.m_list;
    
    for(l_i = (l_l.size()-1); l_i >= 0; l_i--)
      {
      l_ci = l_l.get(l_i);
      if( l_ci.m_cache == p_cache )
        {
        if( (--l_ci.m_ref_count) <= 0 )
          {
          l_l.remove(l_i);
          }
        return;
        }
      }
    }
  
/**
 * An cache instance.
 *
 * @author Thomas Weise
 */

  private static  final class CacheInstance
    {
/**
 * The cache used internally to prevent equal genotypes from being created
 * twice. 
 */

            final SearchCache         m_cache         ;
/**
 * The class of the search context used.
 */

            final Class             m_class         ;
/**
 * The search definition applied.
 */

            final SearchDefinition  m_definition    ;
/**
 * The thread that created the cache.
 */

            final Thread            m_thread        ;
/**
 * The reference counter.
 */

                  int               m_ref_count     ;
    
/**
 * Create a new cache instance.
 * @param p_class         The class of the search context using this cache.
 * @param p_definition    The search definition applied.
 * @param p_thread        The thread that created the cache.
 */

    CacheInstance (final  Class             p_class,
                   final  SearchDefinition  p_definition,
                   final  Thread            p_thread)
      {
      super();
      
      this.m_class        = p_class;
      this.m_definition   = p_definition;
      this.m_thread       = p_thread;
      this.m_cache        = new SearchCache(p_definition.get_cache_size());
      this.m_ref_count    = 1;
      }
    }
  }

File Information:

file name:CacheManager.java
package:org.dgpf.search.api
qualified name:org.dgpf.search.api.CacheManager.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/api/CacheManager.java
size:4.649 KB (4761 B)
uploaded: 2015-07-22 04:10:59 GMT+0000
last update: 2006-05-08 13:45:38 GMT+0000
last access: 2017-11-20 19:09:03 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-20 19:09:03 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo