Logo Search packages:      
Sourcecode: cdk version File versions  Download package

ChemFile.java

/*
 *  $RCSfile$
 *  $Author: egonw $
 *  $Date: 2007-02-16 14:03:31 +0100 (Fri, 16 Feb 2007) $
 *  $Revision: 7958 $
 *
 *  Copyright (C) 1997-2007  Christoph Steinbeck <steinbeck@users.sf.net>
 *
 *  Contact: cdk-devel@lists.sourceforge.net
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but 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.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.openscience.cdk;

import java.io.Serializable;

import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
import org.openscience.cdk.interfaces.IChemObjectListener;
import org.openscience.cdk.interfaces.IChemSequence;

/**
 *  A Object containing a number of ChemSequences. This is supposed to be the
 *  top level container, which can contain all the concepts stored in a chemical
 *  document
 *
 *@author        steinbeck
 *@cdk.module    data
 */
00042 public class ChemFile extends ChemObject implements Serializable, Cloneable,
            IChemFile, IChemObjectListener
{

      /**
     * Determines if a de-serialized object is compatible with this class.
     *
     * This value must only be changed if and only if the new version
     * of this class is imcompatible with the old version. See Sun docs
     * for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
     * /serialization/spec/version.doc.html>details</a>.
       */
00054       private static final long serialVersionUID = 1926781734333430132L;

      /**
       *  Array of ChemSquences.
       */
00059       protected IChemSequence[] chemSequences;

      /**
       *  Number of ChemSequences contained by this container.
       */
00064       protected int chemSequenceCount;

      /**
       *  Amount by which the chemsequence array grows when elements are added and
       *  the array is not large enough for that.
       */
00070       protected int growArraySize = 4;


      /**
       *  Constructs an empty ChemFile.
       */
00076       public ChemFile()
      {
            chemSequenceCount = 0;
            chemSequences = new ChemSequence[growArraySize];
      }


      /**
       *  Adds a ChemSequence to this container.
       *
       *@param  chemSequence  The chemSequence to be added to this container
       *@see                  #chemSequences
       */
00089       public void addChemSequence(IChemSequence chemSequence)
      {
            chemSequence.addListener(this);
            if (chemSequenceCount + 1 >= chemSequences.length)
            {
                  growChemSequenceArray();
            }
            chemSequences[chemSequenceCount] = chemSequence;
            chemSequenceCount++;
            notifyChanged();
      }

      /**
       *  Removes a ChemSequence from this container.
       *
       *@param  chemSequence  The chemSequence to be added to this container
       *@see                  #chemSequences
       */
00107       public void removeChemSequence(int pos)
      {
            chemSequences[pos].removeListener(this);
            for (int i = pos; i < chemSequenceCount - 1; i++) {
                  chemSequences[i] = chemSequences[i + 1];
            }
            chemSequences[chemSequenceCount - 1] = null;
            chemSequenceCount--;
            notifyChanged();
      }

      /**
       *  Returns the Iterator to ChemSequences of this container.
       *
       *@return    The Iterator to ChemSequences of this container
       *@see       #addChemSequence
       */
00124       public java.util.Iterator chemSequences()
      {
            return new ChemSequenceIterator();
      }

      /**
     * The inner Iterator class.
     *
     */
00133     private class ChemSequenceIterator implements java.util.Iterator {

        private int pointer = 0;
      
        public boolean hasNext() {
            if (pointer < chemSequenceCount) return true;
          return false;
        }

        public Object next() {
            return chemSequences[pointer++];
        }

        public void remove() {
            removeChemSequence(--pointer);
        }
      
    }
      
      /**
       *  Returns the ChemSequence at position <code>number</code> in the container.
       *
       *@param  number  The position of the ChemSequence to be returned.
       *@return         The ChemSequence at position <code>number</code>.
       *@see            #addChemSequence
       */
00159       public IChemSequence getChemSequence(int number)
      {
            return (ChemSequence)chemSequences[number];
      }


      /**
       *  Grows the ChemSequence array by a given size.
       *
       *@see    #growArraySize
       */
00170       protected void growChemSequenceArray()
      {
            growArraySize = chemSequences.length;
            IChemSequence[] newchemSequences = new ChemSequence[chemSequences.length + growArraySize];
            System.arraycopy(chemSequences, 0, newchemSequences, 0, chemSequences.length);
            chemSequences = newchemSequences;
      }


      /**
       *  Returns the number of ChemSequences in this Container.
       *
       *@return    The number of ChemSequences in this Container
       */
00184       public int getChemSequenceCount()
      {
            return this.chemSequenceCount;
      }


      /**
       * Returns a String representation of this class. It implements
         * RFC #9.
       *
       *@return    String representation of the Object
       */
00196       public String toString()
      {
            StringBuffer buffer = new StringBuffer();
            buffer.append("ChemFile(#S=");
            java.util.Iterator seqs = chemSequences();
            buffer.append(chemSequenceCount);
            buffer.append(", ");
            while (seqs.hasNext())
            {
                  IChemSequence sequence = (IChemSequence)seqs.next();
                  buffer.append(sequence.toString());
            }
            buffer.append(')');
            return buffer.toString();
      }


      /**
       *  Allows for getting an clone of this object.
       *
       *@return    a clone of this object
       */
00218       public Object clone() throws CloneNotSupportedException
      {
            ChemFile clone = (ChemFile) super.clone();
            // clone the chemModels
            clone.chemSequenceCount = getChemSequenceCount();
            clone.chemSequences = new ChemSequence[clone.chemSequenceCount];
            for (int f = 0; f < clone.chemSequenceCount; f++)
            {
                  clone.chemSequences[f] = (ChemSequence)((ChemSequence)chemSequences[f]).clone();
            }
            return clone;
      }


      /**
       *  Called by objects to which this object has
       *  registered as a listener.
       *
       *@param  event  A change event pointing to the source of the change
       */
00238       public void stateChanged(IChemObjectChangeEvent event)
      {
            notifyChanged(event);
      }
}


Generated by  Doxygen 1.6.0   Back to index