📄 atomcontainer.java
字号:
/* $RCSfile$ * $Author: egonw $ * $Date: 2007-10-14 21:44:41 +0200 (Sun, 14 Oct 2007) $ * $Revision: 9060 $ * * Copyright (C) 1997-2007 Christoph Steinbeck * * 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 org.openscience.cdk.interfaces.*;import java.io.Serializable;import java.util.*;/** * Base class for all chemical objects that maintain a list of Atoms and * ElectronContainers. <p> * * Looping over all Bonds in the AtomContainer is typically done like: <pre> * Iterator iter = atomContainer.bonds(); * while (iter.hasNext()) { * IBond aBond = (IBond) iter.next(); * } * * </pre> * * @cdk.module data * * @author steinbeck * @cdk.created 2000-10-02 */public class AtomContainer extends ChemObject implements IAtomContainer, IChemObjectListener, Serializable, Cloneable { /** * 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>. */ private static final long serialVersionUID = 5678100348445919254L; /** * Number of atoms contained by this object. */ protected int atomCount; /** * Number of bonds contained by this object. */ protected int bondCount; /** * Number of lone pairs contained by this object. */ protected int lonePairCount; /** * Number of single electrons contained by this object. */ protected int singleElectronCount; /** * Amount by which the bond and arom arrays grow when elements are added and * the arrays are not large enough for that. */ protected int growArraySize = 10; /** * Internal array of atoms. */ protected IAtom[] atoms; /** * Internal array of bonds. */ protected IBond[] bonds; /** * Internal array of lone pairs. */ protected ILonePair[] lonePairs; /** * Internal array of single electrons. */ protected ISingleElectron[] singleElectrons; /** * Internal list of atom parities. */ protected Hashtable atomParities; /** * Constructs an empty AtomContainer. */ public AtomContainer() { this(10, 10, 0, 0); } /** * Constructs an AtomContainer with a copy of the atoms and electronContainers * of another AtomContainer (A shallow copy, i.e., with the same objects as in * the original AtomContainer). * * @param container An AtomContainer to copy the atoms and electronContainers from */ public AtomContainer(IAtomContainer container) { this.atomCount = container.getAtomCount(); this.bondCount = container.getBondCount(); this.lonePairCount = container.getLonePairCount(); this.singleElectronCount = container.getSingleElectronCount(); this.atoms = new IAtom[this.atomCount]; this.bonds = new IBond[this.bondCount]; this.lonePairs = new ILonePair[this.lonePairCount]; this.singleElectrons = new ISingleElectron[this.singleElectronCount]; atomParities = new Hashtable(atomCount/2); for (int f = 0; f < container.getAtomCount(); f++) { atoms[f] = container.getAtom(f); container.getAtom(f).addListener(this); } for (int f = 0; f < this.bondCount; f++) { bonds[f] = container.getBond(f); container.getBond(f).addListener(this); } for (int f = 0; f < this.lonePairCount; f++) { lonePairs[f] = container.getLonePair(f); container.getLonePair(f).addListener(this); } for (int f = 0; f < this.singleElectronCount; f++) { singleElectrons[f] = container.getSingleElectron(f); container.getSingleElectron(f).addListener(this); } } /** * Constructs an empty AtomContainer that will contain a certain number of * atoms and electronContainers. It will set the starting array lengths to the * defined values, but will not create any Atom or ElectronContainer's. * *@param atomCount Number of atoms to be in this container *@param bondCount Number of bonds to be in this container *@param lpCount Number of lone pairs to be in this container *@param seCount Number of single electrons to be in this container * */ public AtomContainer(int atomCount, int bondCount, int lpCount, int seCount) { this.atomCount = 0; this.bondCount = 0; this.lonePairCount = 0; this.singleElectronCount = 0; atoms = new IAtom[atomCount]; bonds = new IBond[bondCount]; lonePairs = new ILonePair[lpCount]; singleElectrons = new ISingleElectron[seCount]; atomParities = new Hashtable(atomCount/2); } /** * Adds an AtomParity to this container. If a parity is already given for the * affected Atom, it is overwritten. * * @param parity The new AtomParity for this container * @see #getAtomParity */ public void addAtomParity(IAtomParity parity) { atomParities.put(parity.getAtom(), parity); } /** * Returns the atom parity for the given Atom. If no parity is associated * with the given Atom, it returns null. * * @param atom Atom for which the parity must be returned * @return The AtomParity for the given Atom, or null if that Atom does * not have an associated AtomParity * @see #addAtomParity */ public IAtomParity getAtomParity(IAtom atom) { return (AtomParity)atomParities.get(atom); } /** * Sets the array of atoms of this AtomContainer. * *@param atoms The array of atoms to be assigned to this AtomContainer *@see #getAtom */ public void setAtoms(IAtom[] atoms) { this.atoms = atoms; for (int f = 0; f < atoms.length; f++) { atoms[f].addListener(this); } this.atomCount = atoms.length; notifyChanged(); } /** * Sets the array of bonds of this AtomContainer. * * @param bonds The array of bonds to be assigned to * this AtomContainer * @see #getBond */ public void setBonds(IBond[] bonds) { this.bonds = bonds; for (int i = 0; i < bonds.length; ++i) { bonds[i].addListener(this); } this.bondCount = bonds.length; } /** * Sets the array of electronContainers of this AtomContainer. * *@param electronContainers The array of electronContainers to be assigned to * this AtomContainer *@see #getElectronContainers */// public void setElectronContainers(IElectronContainer[] electronContainers)// {// this.electronContainers = electronContainers;// for (int f = 0; f < electronContainers.length; f++)// {// electronContainers[f].addListener(this); // }// setElectronContainerCount(electronContainers.length);// notifyChanged();// } /** * Set the atom at position <code>number</code> in [0,..]. * *@param number The position of the atom to be set. *@param atom The atom to be stored at position <code>number</code> *@see #getAtom(int) */ public void setAtom(int number, IAtom atom) { atom.addListener(this); atoms[number] = atom; notifyChanged(); } /** * Get the atom at position <code>number</code> in [0,..]. * *@param number The position of the atom to be retrieved. *@return The atomAt value * @see #setAtom(int, org.openscience.cdk.interfaces.IAtom) * @see #setAtoms(org.openscience.cdk.interfaces.IAtom[]) * */ public IAtom getAtom(int number) { return atoms[number]; } /** * Get the bond at position <code>number</code> in [0,..]. * *@param number The position of the bond to be retrieved. *@return The bondAt value */ public IBond getBond(int number) { return bonds[number]; } /** * Get the lone pair at position <code>number</code> in [0,..]. * *@param number The position of the LonePair to be retrieved. *@return The lone pair number */ public ILonePair getLonePair(int number) { return lonePairs[number]; } /** * Get the single electron at position <code>number</code> in [0,..]. * *@param number The position of the SingleElectron to be retrieved. *@return The single electron number */ public ISingleElectron getSingleElectron(int number) { return singleElectrons[number]; } /** * Sets the ElectronContainer at position <code>number</code> in [0,..]. * * @param number The position of the ElectronContainer to be set. * @param electronContainer The ElectronContainer to be stored at position <code>number</code> * @see #getElectronContainer(int) */// public void setElectronContainer(int number, IElectronContainer electronContainer)// {// electronContainer.addListener(this);// electronContainers[number] = electronContainer;// notifyChanged();// } /** * Sets the number of electronContainers in this container. * * @param electronContainerCount The number of electronContainers in this * container * @see #getElectronContainerCount */// public void setElectronContainerCount(int electronContainerCount)// {// this.electronContainerCount = electronContainerCount;// notifyChanged();// } /** * Sets the number of atoms in this container. * *@param atomCount The number of atoms in this container *@see #getAtomCount */// public void setAtomCount(int atomCount)// {// this.atomCount = atomCount;// notifyChanged();// } /** * Returns an Iterator for looping over all atoms in this container. * *@return An Iterator with the atoms in this container */ public java.util.Iterator atoms() { return new AtomIterator(); } /** * The inner AtomIterator class. * */ private class AtomIterator implements java.util.Iterator { private int pointer = 0; public boolean hasNext() { return pointer < atomCount; } public Object next() { return atoms[pointer++]; } public void remove() { removeAtom(--pointer); } } /** * Returns an Iterator for looping over all bonds in this container. * *@return An Iterator with the bonds in this container */ public java.util.Iterator bonds() { return new BondIterator(); } /** * The inner BondIterator class. * */ private class BondIterator implements java.util.Iterator { private int pointer = 0; public boolean hasNext() { return pointer < bondCount; } public Object next() { return bonds[pointer++]; } public void remove() { removeBond(--pointer); } } /** * Returns an Iterator for looping over all lone pairs in this container. * *@return An Iterator with the lone pairs in this container */ public Iterator lonePairs() { return new LonePairIterator(); } /** * The inner LonePairIterator class. * */ private class LonePairIterator implements java.util.Iterator { private int pointer = 0; public boolean hasNext() { return pointer < lonePairCount; } public Object next() { return lonePairs[pointer++]; } public void remove() { removeLonePair(--pointer); } } /** * Returns an Iterator for looping over all single electrons in this container. * *@return An Iterator with the single electrons in this container */ public Iterator singleElectrons() { return new SingleElectronIterator(); } /** * The inner SingleElectronIterator class. * */ private class SingleElectronIterator implements java.util.Iterator { private int pointer = 0; public boolean hasNext() { return pointer < singleElectronCount; } public Object next() { return singleElectrons[pointer++]; } public void remove() { removeSingleElectron(--pointer); } } /** * Returns an Iterator for looping over all electron containers in this container. * *@return An Iterator with the electron containers in this container */ public Iterator electronContainers() { return new ElectronContainerIterator(); } /** * The inner ElectronContainerIterator class. * */ private class ElectronContainerIterator implements java.util.Iterator { private int pointer = 0; public boolean hasNext() { return pointer < (bondCount + lonePairCount + singleElectronCount); } public Object next() { if (pointer < bondCount) return bonds[pointer++]; else if (pointer < bondCount+lonePairCount) return lonePairs[(pointer++)-bondCount]; else if (pointer < bondCount+lonePairCount+singleElectronCount) return singleElectrons[(pointer++)-bondCount-lonePairCount]; return null; } public void remove() { if (pointer <= bondCount) removeBond(--pointer); else if (pointer <= bondCount+lonePairCount) removeLonePair((--pointer)-bondCount); else if (pointer <= bondCount+lonePairCount+singleElectronCount) removeSingleElectron((--pointer)-bondCount-lonePairCount); } } /** * Returns the atom at position 0 in the container. * *@return The atom at position 0 . */ public IAtom getFirstAtom() { return (Atom)atoms[0]; } /** * Returns the atom at the last position in the container. * *@return The atom at the last position */ public IAtom getLastAtom() { return getAtomCount() > 0 ? (Atom)atoms[getAtomCount() - 1] : null; } /** * Returns the position of a given atom in the atoms array. It returns -1 if * the atom does not exist. * *@param atom The atom to be sought *@return The Position of the atom in the atoms array in [0,..]. */ public int getAtomNumber(IAtom atom) { for (int f = 0; f < atomCount; f++) { if (atoms[f] == atom) return f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -