Logo
Distributed Genetic Programming Framework
print print

File org.sfc.io.ReferenceCountedReader.java

Here you can find all the information about the file org.sfc.io.ReferenceCountedReader.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-06-22 16:25:35
 * Original Filename: org.sfc.io.ReferenceCountedReader.java
 * Version          : 1.0.2
 * Last modification: 2006-03-07
 *                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.sfc.io;

import java.io.IOException;
import java.io.Reader;

import org.sfc.scoped.AlreadyDisposedException;
import org.sfc.scoped.IReferenceCounted;
import org.sfc.utils.ICloneable;

/**
 * This class is a wrapper for <code>Reader</code>s that makes them
 * reference counted.
 * @author Thomas Weise 
 */

public abstract class ReferenceCountedReader  extends     Reader
                                              implements  IReferenceCounted,
                                                          ICloneable
  { 
/**
 * The internal reference counter.
 */

  private   int     m_ref_count ;
    
/**
 * <code>true</code> only as long as this object hasn't been disposed.
 */

  private   boolean m_living    ;  
    
/**
 * Create a reference counted <code>Reader</code>.
 * 
 */

  protected ReferenceCountedReader  ()
    {
    super();
    this.m_ref_count = 1;
    this.m_living    = true;
    }
    
/**
 * Increase the reference counter of this object by one.
 * You must call <code>add_ref</code> before you assign a variable to point
 * on this object (except when calling the constructor and storing the
 * result to exactly one variable).
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  add_ref () throws AlreadyDisposedException
    {
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_ref_count++;
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    }
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  release ()  throws  AlreadyDisposedException
    {
    try
      {
      this.do_release();
      }
    catch(IOException l_ioe)
      {
      //
      }
    }
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws  IOException If the underlying was closed and caused an
 *                      IOException.
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  private final void  do_release () throws IOException,
                                           AlreadyDisposedException
    {
    synchronized(this)
      {
      if(this.m_living)
        {        
        if( (--this.m_ref_count) <= 0)
          {
          this.m_living = false;
          this.dispose();
          }
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    }
  
  
/**
 * Dispose this reader.
 * @throws  IOException If the underlying was closed and caused an
 *                      IOException.
 */

  protected void  dispose() throws IOException
    {
    //
    }
  
/**
 * Returns a new instance of the io object.
 * @return  Another port to the io object.
 */

    @Override
  public  final Object  clone ()
    {
    this.add_ref();
    return this;
    }

/**
 * Close the stream. This method supersets the normal <code>close()</code>-
 * method and delegates to <code>release()</code>, so you can easily pass
 * objects of this class to methods that use normal semantics.
 * @throws  IOException  if an I/O error occurs.
 */

    @Override
  public final void close() throws IOException
    {
    do_release();
    }
    
     
/**
 * This method ensures the <code>close()</code> is called if it was not
 * yet called due to a programming mistake.
 * @throws  Throwable Maybe thrown by the super method.
 */

    @Override
  protected final void  finalize  () throws Throwable
    {
    
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_living = false;
        this.dispose();
        }
      this.m_ref_count = 0;
      }
      
    super.finalize();
    }
  }

File Information:

file name:ReferenceCountedReader.java
package:org.sfc.io
qualified name:org.sfc.io.ReferenceCountedReader.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/io/ReferenceCountedReader.java
size:5.325 KB (5453 B)
uploaded: 2015-07-22 04:11:12 GMT+0000
last update: 2006-03-07 04:20:28 GMT+0000
last access: 2018-01-23 15:35:52 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 2018-01-23 15:35:52 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo