3
* $Date: 2007-10-14 21:44:41 +0200 (Sun, 14 Oct 2007) $
6
* Copyright (C) 1997-2007 Christoph Steinbeck
8
* Contact: cdk-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24
package org.openscience.cdk;
26
import org.openscience.cdk.interfaces.*;
28
import java.io.Serializable;
32
* Base class for all chemical objects that maintain a list of Atoms and
33
* ElectronContainers. <p>
35
* Looping over all Bonds in the AtomContainer is typically done like: <pre>
36
* Iterator iter = atomContainer.bonds();
37
* while (iter.hasNext()) {
38
* IBond aBond = (IBond) iter.next();
46
* @cdk.created 2000-10-02
48
public class AtomContainer extends ChemObject
49
implements IAtomContainer, IChemObjectListener, Serializable, Cloneable {
52
* Determines if a de-serialized object is compatible with this class.
54
* This value must only be changed if and only if the new version
55
* of this class is imcompatible with the old version. See Sun docs
56
* for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
57
* /serialization/spec/version.doc.html>details</a>.
59
private static final long serialVersionUID = 5678100348445919254L;
62
* Number of atoms contained by this object.
64
protected int atomCount;
67
* Number of bonds contained by this object.
69
protected int bondCount;
72
* Number of lone pairs contained by this object.
74
protected int lonePairCount;
77
* Number of single electrons contained by this object.
79
protected int singleElectronCount;
82
* Amount by which the bond and arom arrays grow when elements are added and
83
* the arrays are not large enough for that.
85
protected int growArraySize = 10;
88
* Internal array of atoms.
90
protected IAtom[] atoms;
93
* Internal array of bonds.
95
protected IBond[] bonds;
98
* Internal array of lone pairs.
100
protected ILonePair[] lonePairs;
103
* Internal array of single electrons.
105
protected ISingleElectron[] singleElectrons;
108
* Internal list of atom parities.
110
protected Hashtable atomParities;
114
* Constructs an empty AtomContainer.
116
public AtomContainer() {
122
* Constructs an AtomContainer with a copy of the atoms and electronContainers
123
* of another AtomContainer (A shallow copy, i.e., with the same objects as in
124
* the original AtomContainer).
126
* @param container An AtomContainer to copy the atoms and electronContainers from
128
public AtomContainer(IAtomContainer container)
130
this.atomCount = container.getAtomCount();
131
this.bondCount = container.getBondCount();
132
this.lonePairCount = container.getLonePairCount();
133
this.singleElectronCount = container.getSingleElectronCount();
134
this.atoms = new IAtom[this.atomCount];
135
this.bonds = new IBond[this.bondCount];
136
this.lonePairs = new ILonePair[this.lonePairCount];
137
this.singleElectrons = new ISingleElectron[this.singleElectronCount];
139
atomParities = new Hashtable(atomCount/2);
141
for (int f = 0; f < container.getAtomCount(); f++) {
142
atoms[f] = container.getAtom(f);
143
container.getAtom(f).addListener(this);
145
for (int f = 0; f < this.bondCount; f++) {
146
bonds[f] = container.getBond(f);
147
container.getBond(f).addListener(this);
149
for (int f = 0; f < this.lonePairCount; f++) {
150
lonePairs[f] = container.getLonePair(f);
151
container.getLonePair(f).addListener(this);
153
for (int f = 0; f < this.singleElectronCount; f++) {
154
singleElectrons[f] = container.getSingleElectron(f);
155
container.getSingleElectron(f).addListener(this);
161
* Constructs an empty AtomContainer that will contain a certain number of
162
* atoms and electronContainers. It will set the starting array lengths to the
163
* defined values, but will not create any Atom or ElectronContainer's.
165
*@param atomCount Number of atoms to be in this container
166
*@param bondCount Number of bonds to be in this container
167
*@param lpCount Number of lone pairs to be in this container
168
*@param seCount Number of single electrons to be in this container
171
public AtomContainer(int atomCount, int bondCount, int lpCount, int seCount)
175
this.lonePairCount = 0;
176
this.singleElectronCount = 0;
177
atoms = new IAtom[atomCount];
178
bonds = new IBond[bondCount];
179
lonePairs = new ILonePair[lpCount];
180
singleElectrons = new ISingleElectron[seCount];
181
atomParities = new Hashtable(atomCount/2);
185
* Adds an AtomParity to this container. If a parity is already given for the
186
* affected Atom, it is overwritten.
188
* @param parity The new AtomParity for this container
189
* @see #getAtomParity
191
public void addAtomParity(IAtomParity parity) {
192
atomParities.put(parity.getAtom(), parity);
196
* Returns the atom parity for the given Atom. If no parity is associated
197
* with the given Atom, it returns null.
199
* @param atom Atom for which the parity must be returned
200
* @return The AtomParity for the given Atom, or null if that Atom does
201
* not have an associated AtomParity
202
* @see #addAtomParity
204
public IAtomParity getAtomParity(IAtom atom) {
205
return (AtomParity)atomParities.get(atom);
209
* Sets the array of atoms of this AtomContainer.
211
*@param atoms The array of atoms to be assigned to this AtomContainer
214
public void setAtoms(IAtom[] atoms)
217
for (int f = 0; f < atoms.length; f++)
219
atoms[f].addListener(this);
221
this.atomCount = atoms.length;
226
* Sets the array of bonds of this AtomContainer.
228
* @param bonds The array of bonds to be assigned to
232
public void setBonds(IBond[] bonds)
235
for (int i = 0; i < bonds.length; ++i) {
236
bonds[i].addListener(this);
238
this.bondCount = bonds.length;
242
* Sets the array of electronContainers of this AtomContainer.
244
*@param electronContainers The array of electronContainers to be assigned to
246
*@see #getElectronContainers
248
// public void setElectronContainers(IElectronContainer[] electronContainers)
250
// this.electronContainers = electronContainers;
251
// for (int f = 0; f < electronContainers.length; f++)
253
// electronContainers[f].addListener(this);
255
// setElectronContainerCount(electronContainers.length);
261
* Set the atom at position <code>number</code> in [0,..].
263
*@param number The position of the atom to be set.
264
*@param atom The atom to be stored at position <code>number</code>
267
public void setAtom(int number, IAtom atom)
269
atom.addListener(this);
270
atoms[number] = atom;
276
* Get the atom at position <code>number</code> in [0,..].
278
*@param number The position of the atom to be retrieved.
279
*@return The atomAt value
280
* @see #setAtom(int, org.openscience.cdk.interfaces.IAtom)
281
* @see #setAtoms(org.openscience.cdk.interfaces.IAtom[])
284
public IAtom getAtom(int number)
286
return atoms[number];
291
* Get the bond at position <code>number</code> in [0,..].
293
*@param number The position of the bond to be retrieved.
294
*@return The bondAt value
296
public IBond getBond(int number)
298
return bonds[number];
302
* Get the lone pair at position <code>number</code> in [0,..].
304
*@param number The position of the LonePair to be retrieved.
305
*@return The lone pair number
307
public ILonePair getLonePair(int number)
309
return lonePairs[number];
313
* Get the single electron at position <code>number</code> in [0,..].
315
*@param number The position of the SingleElectron to be retrieved.
316
*@return The single electron number
318
public ISingleElectron getSingleElectron(int number)
320
return singleElectrons[number];
324
* Sets the ElectronContainer at position <code>number</code> in [0,..].
326
* @param number The position of the ElectronContainer to be set.
327
* @param electronContainer The ElectronContainer to be stored at position <code>number</code>
328
* @see #getElectronContainer(int)
330
// public void setElectronContainer(int number, IElectronContainer electronContainer)
332
// electronContainer.addListener(this);
333
// electronContainers[number] = electronContainer;
339
* Sets the number of electronContainers in this container.
341
* @param electronContainerCount The number of electronContainers in this
343
* @see #getElectronContainerCount
345
// public void setElectronContainerCount(int electronContainerCount)
347
// this.electronContainerCount = electronContainerCount;
353
* Sets the number of atoms in this container.
355
*@param atomCount The number of atoms in this container
358
// public void setAtomCount(int atomCount)
360
// this.atomCount = atomCount;
366
* Returns an Iterator for looping over all atoms in this container.
368
*@return An Iterator with the atoms in this container
370
public java.util.Iterator atoms()
372
return new AtomIterator();
376
* The inner AtomIterator class.
379
private class AtomIterator implements java.util.Iterator {
381
private int pointer = 0;
383
public boolean hasNext() {
384
return pointer < atomCount;
387
public Object next() {
388
return atoms[pointer++];
391
public void remove() {
392
removeAtom(--pointer);
398
* Returns an Iterator for looping over all bonds in this container.
400
*@return An Iterator with the bonds in this container
402
public java.util.Iterator bonds()
404
return new BondIterator();
408
* The inner BondIterator class.
411
private class BondIterator implements java.util.Iterator {
413
private int pointer = 0;
415
public boolean hasNext() {
416
return pointer < bondCount;
419
public Object next() {
420
return bonds[pointer++];
423
public void remove() {
424
removeBond(--pointer);
430
* Returns an Iterator for looping over all lone pairs in this container.
432
*@return An Iterator with the lone pairs in this container
434
public Iterator lonePairs()
436
return new LonePairIterator();
440
* The inner LonePairIterator class.
443
private class LonePairIterator implements java.util.Iterator {
445
private int pointer = 0;
447
public boolean hasNext() {
448
return pointer < lonePairCount;
451
public Object next() {
452
return lonePairs[pointer++];
455
public void remove() {
456
removeLonePair(--pointer);
462
* Returns an Iterator for looping over all single electrons in this container.
464
*@return An Iterator with the single electrons in this container
466
public Iterator singleElectrons()
468
return new SingleElectronIterator();
472
* The inner SingleElectronIterator class.
475
private class SingleElectronIterator implements java.util.Iterator {
477
private int pointer = 0;
479
public boolean hasNext() {
480
return pointer < singleElectronCount;
483
public Object next() {
484
return singleElectrons[pointer++];
487
public void remove() {
488
removeSingleElectron(--pointer);
494
* Returns an Iterator for looping over all electron containers in this container.
496
*@return An Iterator with the electron containers in this container
498
public Iterator electronContainers()
500
return new ElectronContainerIterator();
504
* The inner ElectronContainerIterator class.
507
private class ElectronContainerIterator implements java.util.Iterator {
509
private int pointer = 0;
511
public boolean hasNext() {
512
return pointer < (bondCount + lonePairCount + singleElectronCount);
515
public Object next() {
516
if (pointer < bondCount) return bonds[pointer++];
517
else if (pointer < bondCount+lonePairCount) return lonePairs[(pointer++)-bondCount];
518
else if (pointer < bondCount+lonePairCount+singleElectronCount) return singleElectrons[(pointer++)-bondCount-lonePairCount];
522
public void remove() {
523
if (pointer <= bondCount) removeBond(--pointer);
524
else if (pointer <= bondCount+lonePairCount) removeLonePair((--pointer)-bondCount);
525
else if (pointer <= bondCount+lonePairCount+singleElectronCount) removeSingleElectron((--pointer)-bondCount-lonePairCount);
531
* Returns the atom at position 0 in the container.
533
*@return The atom at position 0 .
535
public IAtom getFirstAtom()
537
return (Atom)atoms[0];
542
* Returns the atom at the last position in the container.
544
*@return The atom at the last position
546
public IAtom getLastAtom()
548
return getAtomCount() > 0 ? (Atom)atoms[getAtomCount() - 1] : null;
553
* Returns the position of a given atom in the atoms array. It returns -1 if
554
* the atom does not exist.
556
*@param atom The atom to be sought
557
*@return The Position of the atom in the atoms array in [0,..].
559
public int getAtomNumber(IAtom atom)
561
for (int f = 0; f < atomCount; f++)
563
if (atoms[f] == atom) return f;
570
* Returns the position of the bond between two given atoms in the
571
* electronContainers array. It returns -1 if the bond does not exist.
573
*@param atom1 The first atom
574
*@param atom2 The second atom
575
*@return The Position of the bond between a1 and a2 in the
576
* electronContainers array.
578
public int getBondNumber(IAtom atom1, IAtom atom2)
580
return (getBondNumber(getBond(atom1, atom2)));
585
* Returns the position of a given bond in the electronContainers array. It
586
* returns -1 if the bond does not exist.
588
*@param bond The bond to be sought
589
*@return The Position of the bond in the electronContainers array in [0,..].
591
public int getBondNumber(IBond bond)
593
for (int f = 0; f < bondCount; f++)
595
if (bonds[f] == bond) return f;
601
* Returns the position of a given lone pair in the lone pair array.
602
* It returns -1 if the lone pair does not exist.
604
*@param lonePair The lone pair to be sought
605
*@return The Position of the lone pair in the array..
607
public int getLonePairNumber(ILonePair lonePair)
609
for (int f = 0; f < lonePairCount; f++)
611
if (lonePairs[f] == lonePair) return f;
617
* Returns the position of a given single electron in the single electron array.
618
* It returns -1 if the single electron does not exist.
620
*@param atom The single electron to be sought
621
*@return The Position of the single electron in the array.
623
public int getSingleElectronNumber(ISingleElectron singleElectron)
625
for (int f = 0; f < singleElectronCount; f++)
627
if (singleElectrons[f] == singleElectron) return f;
633
* Returns the ElectronContainer at position <code>number</code> in the
636
* @param number The position of the ElectronContainer to be returned.
637
* @return The ElectronContainer at position <code>number</code>.
639
public IElectronContainer getElectronContainer(int number)
641
if (number < this.bondCount) return bonds[number];
642
number -= this.bondCount;
643
if (number < this.lonePairCount) return lonePairs[number];
644
number -= this.lonePairCount;
645
if (number < this.singleElectronCount) return singleElectrons[number];
651
* Returns the bond that connectes the two given atoms.
653
* @param atom1 The first atom
654
* @param atom2 The second atom
655
* @return The bond that connectes the two atoms
657
public IBond getBond(IAtom atom1, IAtom atom2)
659
for (int i = 0; i < getBondCount(); i++)
661
if (bonds[i].contains(atom1) &&
662
bonds[i].getConnectedAtom(atom1) == atom2) {
670
* Returns the number of Atoms in this Container.
672
*@return The number of Atoms in this Container
674
public int getAtomCount()
676
return this.atomCount;
680
* Returns the number of Bonds in this Container.
682
*@return The number of Bonds in this Container
684
public int getBondCount()
686
return this.bondCount;
690
* Returns the number of LonePairs in this Container.
692
*@return The number of LonePairs in this Container
694
public int getLonePairCount()
696
return this.lonePairCount;
700
* Returns the number of the single electrons in this container,
702
*@return The number of SingleElectron objects of this AtomContainer
704
public int getSingleElectronCount()
706
return this.singleElectronCount;
710
* Returns the number of ElectronContainers in this Container.
712
* @return The number of ElectronContainers in this Container
714
public int getElectronContainerCount()
716
return this.bondCount + this.lonePairCount + this.singleElectronCount;
720
* Returns an ArrayList of all atoms connected to the given atom.
722
*@param atom The atom the bond partners are searched of.
723
*@return The ArrayList with the connected atoms
725
public List getConnectedAtomsList(IAtom atom)
727
List atomsList = new ArrayList();
728
for (int i = 0; i < bondCount; i++)
730
if (bonds[i].contains(atom)) atomsList.add(bonds[i].getConnectedAtom(atom));
736
* Returns an ArrayList of all Bonds connected to the given atom.
738
*@param atom The atom the connected bonds are searched of
739
*@return The ArrayList with connected atoms
741
public List getConnectedBondsList(IAtom atom)
743
List bondsList = new ArrayList();
744
for (int i = 0; i < bondCount; i++)
746
if (bonds[i].contains(atom)) bondsList.add(bonds[i]);
752
* Returns the array of lone pairs connected to an atom.
754
* @param atom The atom for which to get lone pairs
755
* @return The array of LonePairs of this AtomContainer
756
* @see #getElectronContainer
757
* @see #electronContainers()
760
public List getConnectedLonePairsList(IAtom atom) {
761
List lps = new ArrayList();
762
for (int i = 0; i < lonePairCount; i++) {
763
if (lonePairs[i].contains(atom)) lps.add(lonePairs[i]);
769
* Returns an array of all SingleElectron connected to the given atom.
771
*@param atom The atom on which the single electron is located
772
*@return The array of SingleElectron of this AtomContainer
774
public List getConnectedSingleElectronsList(IAtom atom)
776
List lps = new ArrayList();
777
for (int i = 0; i < singleElectronCount; i++)
779
if (singleElectrons[i].contains(atom)) lps.add(singleElectrons[i]);
785
* Returns an ArrayList of all electronContainers connected to the given atom.
787
*@param atom The atom the connected electronContainers are searched of
788
*@return The ArrayList with the connected atoms
790
public List getConnectedElectronContainersList(IAtom atom)
792
List lps = new ArrayList();
793
for (int i = 0; i < bondCount; i++)
795
if (bonds[i].contains(atom)) lps.add(bonds[i]);
797
for (int i = 0; i < lonePairCount; i++)
799
if (lonePairs[i].contains(atom)) lps.add(lonePairs[i]);
801
for (int i = 0; i < singleElectronCount; i++)
803
if (singleElectrons[i].contains(atom)) lps.add(singleElectrons[i]);
809
* Returns the number of atoms connected to the given atom.
811
*@param atom The atom the number of bond partners are searched of.
812
*@return The the size of connected atoms
814
public int getConnectedAtomsCount(IAtom atom)
817
for (int i = 0; i < bondCount; i++)
819
if (bonds[i].contains(atom)) ++count;
825
* Returns the number of Bonds for a given Atom.
827
*@param atom The atom
828
*@return The number of Bonds for this atom
830
public int getConnectedBondsCount(IAtom atom)
832
return getConnectedAtomsCount(atom);
836
* Returns the number of connected atoms (degree) to the given atom.
838
*@param atomNumber The atomnumber the degree is searched for
839
*@return The number of connected atoms (degree)
841
public int getConnectedBondsCount(int atomNumber)
843
return getConnectedAtomsCount(atoms[atomNumber]);
847
* Returns the number of LonePairs for a given Atom.
849
*@param atom The atom
850
*@return The number of LonePairs for this atom
852
public int getConnectedLonePairsCount(IAtom atom)
855
for (int i = 0; i < lonePairCount; i++)
857
if (lonePairs[i].contains(atom)) ++count;
863
* Returns the sum of the SingleElectron for a given Atom.
865
*@param atom The atom on which the single electron is located
866
*@return The array of SingleElectron of this AtomContainer
868
public int getConnectedSingleElectronsCount(IAtom atom)
871
for (int i = 0; i < singleElectronCount; i++)
873
if (singleElectrons[i].contains(atom)) ++count;
880
* Returns the sum of the bond orders for a given Atom.
882
* @param atom The atom
883
* @return The number of bondorders for this atom
885
public double getBondOrderSum(IAtom atom)
888
for (int i = 0; i < bondCount; i++)
890
if (bonds[i].contains(atom)) count += bonds[i].getOrder();
896
* Returns the maximum bond order that this atom currently has in the context
897
* of this AtomContainer.
899
* @param atom The atom
900
* @return The maximum bond order that this atom currently has
902
public double getMaximumBondOrder(IAtom atom) {
904
for (int i = 0; i < bondCount; i++)
906
if (bonds[i].contains(atom) && bonds[i].getOrder() > max)
908
max = bonds[i].getOrder();
916
* Returns the minimum bond order that this atom currently has in the context
917
* of this AtomContainer.
919
*@param atom The atom
920
*@return The minimim bond order that this atom currently has
922
public double getMinimumBondOrder(IAtom atom)
925
for (int i = 0; i < bondCount; i++)
927
if (bonds[i].contains(atom) && bonds[i].getOrder() < min)
929
min = bonds[i].getOrder();
936
* Adds all atoms and electronContainers of a given atomcontainer to this
939
*@param atomContainer The atomcontainer to be added
941
public void add(IAtomContainer atomContainer)
943
for (int f = 0; f < atomContainer.getAtomCount(); f++)
945
if (!contains(atomContainer.getAtom(f)))
947
addAtom(atomContainer.getAtom(f));
950
for (int f = 0; f < atomContainer.getBondCount(); f++)
952
if (!contains(atomContainer.getBond(f)))
954
addBond(atomContainer.getBond(f));
957
for (int f = 0; f < atomContainer.getLonePairCount(); f++)
959
if (!contains(atomContainer.getLonePair(f)))
961
addLonePair(atomContainer.getLonePair(f));
964
for (int f = 0; f < atomContainer.getSingleElectronCount(); f++)
966
if (!contains(atomContainer.getSingleElectron(f)))
968
addSingleElectron(atomContainer.getSingleElectron(f));
975
* Adds the <code>ElectronContainer</code>s found in atomContainer to this
978
*@param atomContainer AtomContainer with the new ElectronContainers
980
// public void addElectronContainers(IAtomContainer atomContainer)
988
* Adds an atom to this container.
990
*@param atom The atom to be added to this container
992
public void addAtom(IAtom atom)
999
if (atomCount + 1 >= atoms.length)
1003
atom.addListener(this);
1004
atoms[atomCount] = atom;
1011
* Adds a Bond to this AtomContainer.
1013
*@param bond The bond to added to this container
1015
public void addBond(IBond bond)
1017
if (bondCount >= bonds.length) growBondArray();
1018
bonds[bondCount] = bond;
1024
* Adds a lone pair to this AtomContainer.
1026
*@param lonePair The LonePair to added to this container
1028
public void addLonePair(ILonePair lonePair)
1030
if (lonePairCount >= lonePairs.length) growLonePairArray();
1031
lonePairs[lonePairCount] = lonePair;
1037
* Adds a single electron to this AtomContainer.
1039
*@param singleElectron The SingleElectron to added to this container
1041
public void addSingleElectron(ISingleElectron singleElectron)
1043
if (singleElectronCount >= singleElectrons.length) growSingleElectronArray();
1044
singleElectrons[singleElectronCount] = singleElectron;
1045
++singleElectronCount;
1050
* Adds a ElectronContainer to this AtomContainer.
1052
*@param electronContainer The ElectronContainer to added to this container
1054
public void addElectronContainer(IElectronContainer electronContainer)
1056
if (electronContainer instanceof IBond) this.addBond((IBond)electronContainer);
1057
if (electronContainer instanceof ILonePair) this.addLonePair((ILonePair)electronContainer);
1058
if (electronContainer instanceof ISingleElectron) this.addSingleElectron((ISingleElectron)electronContainer);
1063
* Removes all atoms and electronContainers of a given atomcontainer from this
1066
*@param atomContainer The atomcontainer to be removed
1068
public void remove(IAtomContainer atomContainer)
1070
for (int f = 0; f < atomContainer.getAtomCount(); f++)
1072
removeAtom(atomContainer.getAtom(f));
1074
for (int f = 0; f < atomContainer.getBondCount(); f++)
1076
removeBond(atomContainer.getBond(f));
1078
for (int f = 0; f < atomContainer.getLonePairCount(); f++)
1080
removeLonePair(atomContainer.getLonePair(f));
1082
for (int f = 0; f < atomContainer.getSingleElectronCount(); f++)
1084
removeSingleElectron(atomContainer.getSingleElectron(f));
1089
* Removes the atom at the given position from the AtomContainer. Note that
1090
* the electronContainers are unaffected: you also have to take care of
1091
* removing all electronContainers to this atom from the container manually.
1093
*@param position The position of the atom to be removed.
1095
public void removeAtom(int position)
1097
atoms[position].removeListener(this);
1098
for (int i = position; i < atomCount - 1; i++)
1100
atoms[i] = atoms[i + 1];
1102
atoms[atomCount - 1] = null;
1108
* Removes the given atom from the AtomContainer. Note that the
1109
* electronContainers are unaffected: you also have to take care of removeing
1110
* all electronContainers to this atom from the container.
1112
*@param atom The atom to be removed
1114
public void removeAtom(IAtom atom)
1116
int position = getAtomNumber(atom);
1119
removeAtom(position);
1124
* Removes the bond at the given position from the AtomContainer.
1126
*@param position The position of the bond to be removed.
1128
public IBond removeBond(int position)
1130
IBond bond = bonds[position];
1131
bond.removeListener(this);
1132
for (int i = position; i < bondCount - 1; i++)
1134
bonds[i] = bonds[i + 1];
1136
bonds[bondCount - 1] = null;
1143
* Removes the bond that connects the two given atoms.
1145
* @param atom1 The first atom
1146
* @param atom2 The second atom
1147
* @return The bond that connectes the two atoms
1149
public IBond removeBond(IAtom atom1, IAtom atom2)
1151
int pos = getBondNumber(atom1, atom2);
1161
* Removes the bond from this container.
1163
* @param bond The bond to be removed.
1165
public void removeBond(IBond bond)
1167
int pos = getBondNumber(bond);
1168
if (pos != -1) removeBond(pos);
1172
* Removes the lone pair at the given position from the AtomContainer.
1174
*@param position The position of the LonePair to be removed.
1176
public ILonePair removeLonePair(int position)
1178
ILonePair lp = lonePairs[position];
1179
lp.removeListener(this);
1180
for (int i = position; i < lonePairCount - 1; i++)
1182
lonePairs[i] = lonePairs[i + 1];
1184
lonePairs[lonePairCount - 1] = null;
1191
* Removes the lone pair from the AtomContainer.
1193
*@param lonePair The LonePair to be removed.
1195
public void removeLonePair(ILonePair lonePair)
1197
int pos = getLonePairNumber(lonePair);
1198
if (pos != -1) removeLonePair(pos);
1202
* Removes the single electron at the given position from the AtomContainer.
1204
*@param position The position of the SingleElectron to be removed.
1206
public ISingleElectron removeSingleElectron(int position)
1208
ISingleElectron se = singleElectrons[position];
1209
se.removeListener(this);
1210
for (int i = position; i < singleElectronCount - 1; i++)
1212
singleElectrons[i] = singleElectrons[i + 1];
1214
singleElectrons[singleElectronCount - 1] = null;
1215
singleElectronCount--;
1221
* Removes the single electron from the AtomContainer.
1223
*@param singleElectron The SingleElectron to be removed.
1225
public void removeSingleElectron(ISingleElectron singleElectron)
1227
int pos = getSingleElectronNumber(singleElectron);
1228
if (pos != -1) removeSingleElectron(pos);
1232
* Removes the bond at the given position from this container.
1234
* @param number The position of the bond in the electronContainers array
1235
* @return Bond that was removed
1237
public IElectronContainer removeElectronContainer(int number)
1239
if (number < this.bondCount) return removeBond(number);
1240
number -= this.bondCount;
1241
if (number < this.lonePairCount) return removeLonePair(number);
1242
number -= this.lonePairCount;
1243
if (number < this.singleElectronCount) return removeSingleElectron(number);
1249
* Removes this ElectronContainer from this container.
1251
* @param electronContainer The electronContainer to be removed
1253
public void removeElectronContainer(IElectronContainer electronContainer) {
1254
if (electronContainer instanceof IBond) removeBond((IBond) electronContainer);
1255
else if (electronContainer instanceof ILonePair) removeLonePair((ILonePair) electronContainer);
1257
if (electronContainer instanceof ISingleElectron) removeSingleElectron((ISingleElectron) electronContainer);
1261
* Removes the given atom and all connected electronContainers from the
1264
*@param atom The atom to be removed
1266
public void removeAtomAndConnectedElectronContainers(IAtom atom)
1268
int position = getAtomNumber(atom);
1271
for (int i = 0; i < bondCount; i++)
1273
if (bonds[i].contains(atom)) {
1278
for (int i = 0; i < lonePairCount; i++)
1280
if (lonePairs[i].contains(atom)) {
1285
for (int i = 0; i < singleElectronCount; i++)
1287
if (singleElectrons[i].contains(atom)) {
1288
removeSingleElectron(i);
1292
removeAtom(position);
1298
* Removes all atoms and bond from this container.
1300
public void removeAllElements() {
1301
removeAllElectronContainers();
1302
for (int f = 0; f < getAtomCount(); f++) {
1303
getAtom(f).removeListener(this);
1305
atoms = new IAtom[growArraySize];
1312
* Removes electronContainers from this container.
1314
public void removeAllElectronContainers()
1317
for (int f = 0; f < getLonePairCount(); f++) {
1318
getLonePair(f).removeListener(this);
1320
for (int f = 0; f < getSingleElectronCount(); f++) {
1321
getSingleElectron(f).removeListener(this);
1323
lonePairs = new ILonePair[growArraySize];
1324
singleElectrons = new ISingleElectron[growArraySize];
1326
singleElectronCount = 0;
1331
* Removes all Bonds from this container.
1333
public void removeAllBonds() {
1334
for (int f = 0; f < getBondCount(); f++) {
1335
getBond(f).removeListener(this);
1337
bonds = new IBond[growArraySize];
1343
* Adds a bond to this container.
1345
*@param atom1 Id of the first atom of the Bond in [0,..]
1346
*@param atom2 Id of the second atom of the Bond in [0,..]
1347
*@param order Bondorder
1348
*@param stereo Stereochemical orientation
1350
public void addBond(int atom1, int atom2, double order, int stereo)
1352
IBond bond = getBuilder().newBond(getAtom(atom1), getAtom(atom2), order, stereo);
1359
if (bondCount >= bonds.length)
1364
/* no notifyChanged() here because addBond(bond) does
1370
* Adds a bond to this container.
1372
*@param atom1 Id of the first atom of the Bond in [0,..]
1373
*@param atom2 Id of the second atom of the Bond in [0,..]
1374
*@param order Bondorder
1376
public void addBond(int atom1, int atom2, double order)
1378
IBond bond = getBuilder().newBond(getAtom(atom1), getAtom(atom2), order);
1380
if (bondCount >= bonds.length)
1385
/* no notifyChanged() here because addBond(bond) does
1391
* Adds a LonePair to this Atom.
1393
*@param atomID The atom number to which the LonePair is added in [0,..]
1395
public void addLonePair(int atomID)
1397
ILonePair lonePair = getBuilder().newLonePair(atoms[atomID]);
1398
lonePair.addListener(this);
1399
addLonePair(lonePair);
1400
/* no notifyChanged() here because addElectronContainer() does
1405
* Adds a LonePair to this Atom.
1407
*@param atomID The atom number to which the LonePair is added in [0,..]
1409
public void addSingleElectron(int atomID)
1411
ISingleElectron singleElectron = getBuilder().newSingleElectron(atoms[atomID]);
1412
singleElectron.addListener(this);
1413
addSingleElectron(singleElectron);
1414
/* no notifyChanged() here because addSingleElectron() does
1419
* True, if the AtomContainer contains the given atom object.
1421
*@param atom the atom this AtomContainer is searched for
1422
*@return True, if the AtomContainer contains the given atom object
1424
public boolean contains(IAtom atom)
1426
for (int i = 0; i < getAtomCount(); i++)
1428
if (atom == atoms[i]) return true;
1434
* True, if the AtomContainer contains the given bond object.
1436
*@param bond the bond this AtomContainer is searched for
1437
*@return True, if the AtomContainer contains the given bond object
1439
public boolean contains(IBond bond)
1441
for (int i = 0; i < getBondCount(); i++)
1443
if (bond == bonds[i]) return true;
1449
* True, if the AtomContainer contains the given LonePair object.
1451
*@param lonePair the LonePair this AtomContainer is searched for
1452
*@return True, if the AtomContainer contains the given LonePair object
1454
public boolean contains(ILonePair lonePair)
1456
for (int i = 0; i < getLonePairCount(); i++)
1458
if (lonePair == lonePairs[i]) return true;
1464
* True, if the AtomContainer contains the given SingleElectron object.
1466
*@param singleElectron the LonePair this AtomContainer is searched for
1467
*@return True, if the AtomContainer contains the given LonePair object
1469
public boolean contains(ISingleElectron singleElectron)
1471
for (int i = 0; i < getSingleElectronCount(); i++)
1473
if (singleElectron == singleElectrons[i]) return true;
1479
* True, if the AtomContainer contains the given ElectronContainer object.
1481
*@param electronContainer ElectronContainer that is searched for
1482
*@return True, if the AtomContainer contains the given bond object
1484
public boolean contains(IElectronContainer electronContainer)
1486
if (electronContainer instanceof IBond) return contains((IBond)electronContainer);
1487
if (electronContainer instanceof ILonePair) return contains((ILonePair)electronContainer);
1488
if (electronContainer instanceof ISingleElectron) return contains((SingleElectron)electronContainer);
1493
* Returns a one line string representation of this Container. This method is
1496
*@return The string representation of this Container
1498
public String toString()
1500
StringBuffer stringContent = new StringBuffer(64);
1501
stringContent.append("AtomContainer(");
1502
stringContent.append(this.hashCode());
1503
stringContent.append(", #A:").append(getAtomCount());
1504
stringContent.append(", #B:").append(getBondCount()).append(", ");
1505
stringContent.append(", #LP:").append(getLonePairCount()).append(", ");
1506
stringContent.append(", #SE:").append(getSingleElectronCount()).append(", ");
1507
for (int i = 0; i < getAtomCount(); i++)
1509
stringContent.append(getAtom(i).toString()).append(", ");
1511
for (int i = 0; i < getBondCount(); i++)
1513
stringContent.append(getBond(i).toString()).append(", ");
1515
for (int i = 0; i < getLonePairCount(); i++)
1517
stringContent.append(getLonePair(i).toString()).append(", ");
1519
for (int i = 0; i < getSingleElectronCount(); i++)
1521
stringContent.append(getSingleElectron(i).toString()).append(", ");
1524
stringContent.append(", AP:[#").append(atomParities.size()).append(", ");
1525
Enumeration parities = atomParities.elements();
1526
while (parities.hasMoreElements()) {
1527
stringContent.append(parities.nextElement().toString());
1528
if (parities.hasMoreElements()) stringContent.append(", ");
1530
stringContent.append("])");
1531
return stringContent.toString();
1536
* Clones this AtomContainer object and its content.
1538
* @return The cloned object
1541
public Object clone() throws CloneNotSupportedException {
1543
IAtomContainer clone = (IAtomContainer) super.clone();
1544
// start from scratch
1545
clone.removeAllElements();
1547
for (int f = 0; f < getAtomCount(); f++) {
1548
clone.addAtom((Atom) getAtom(f).clone());
1553
for (int i = 0; i < getBondCount(); ++i) {
1555
newBond = (IBond)bond.clone();
1556
newAtoms = new IAtom[bond.getAtomCount()];
1557
for (int j = 0; j < bond.getAtomCount(); ++j) {
1558
newAtoms[j] = clone.getAtom(getAtomNumber(bond.getAtom(j)));
1560
newBond.setAtoms(newAtoms);
1561
clone.addBond(newBond);
1565
for (int i = 0; i < getLonePairCount(); ++i) {
1566
lp = getLonePair(i);
1567
newLp = (ILonePair)lp.clone();
1568
newLp.setAtom(clone.getAtom(getAtomNumber(lp.getAtom())));
1569
clone.addLonePair(newLp);
1572
ISingleElectron newSe;
1573
for (int i = 0; i < getSingleElectronCount(); ++i) {
1574
se = getSingleElectron(i);
1575
newSe = (ISingleElectron)se.clone();
1576
newSe.setAtom(clone.getAtom(getAtomNumber(se.getAtom())));
1577
clone.addSingleElectron(newSe);
1579
// for (int f = 0; f < getElectronContainerCount(); f++) {
1580
// electronContainer = this.getElectronContainer(f);
1581
// newEC = getBuilder().newElectronContainer();
1582
// if (electronContainer instanceof IBond) {
1583
// IBond bond = (IBond) electronContainer;
1584
// newEC = (IElectronContainer)bond.clone();
1585
// newAtoms = new IAtom[bond.getAtomCount()];
1586
// for (int g = 0; g < bond.getAtomCount(); g++) {
1587
// newAtoms[g] = clone.getAtom(getAtomNumber(bond.getAtom(g)));
1589
// ((IBond) newEC).setAtoms(newAtoms);
1590
// } else if (electronContainer instanceof ILonePair) {
1591
// IAtom atom = ((ILonePair) electronContainer).getAtom();
1592
// newEC = (ILonePair)electronContainer.clone();
1593
// ((ILonePair) newEC).setAtom(clone.getAtom(getAtomNumber(atom)));
1594
// } else if (electronContainer instanceof ISingleElectron) {
1595
// IAtom atom = ((ISingleElectron) electronContainer).getAtom();
1596
// newEC = (ISingleElectron)electronContainer.clone();
1597
// ((ISingleElectron) newEC).setAtom(clone.getAtom(getAtomNumber(atom)));
1599
// //logger.debug("Expecting EC, got: " + electronContainer.getClass().getName());
1600
// newEC = (IElectronContainer) electronContainer.clone();
1602
// clone.addElectronContainer(newEC);
1608
* Grows the ElectronContainer array by a given size.
1610
*@see #growArraySize
1612
// protected void growElectronContainerArray()
1614
// growArraySize = (electronContainers.length < growArraySize) ? growArraySize : electronContainers.length;
1615
// IElectronContainer[] newelectronContainers = new IElectronContainer[electronContainers.length + growArraySize];
1616
// System.arraycopy(electronContainers, 0, newelectronContainers, 0, electronContainers.length);
1617
// electronContainers = newelectronContainers;
1622
* Grows the atom array by a given size.
1624
*@see #growArraySize
1626
protected void growAtomArray()
1628
growArraySize = (atoms.length < growArraySize) ? growArraySize : atoms.length;
1629
IAtom[] newatoms = new IAtom[atoms.length + growArraySize];
1630
System.arraycopy(atoms, 0, newatoms, 0, atoms.length);
1635
* Grows the bond array by a given size.
1637
*@see #growArraySize
1639
protected void growBondArray()
1641
growArraySize = (bonds.length < growArraySize) ? growArraySize : bonds.length;
1642
IBond[] newBonds = new IBond[bonds.length + growArraySize];
1643
System.arraycopy(bonds, 0, newBonds, 0, bonds.length);
1648
* Grows the lone pair array by a given size.
1650
*@see #growArraySize
1652
protected void growLonePairArray()
1654
growArraySize = (lonePairs.length < growArraySize) ? growArraySize : lonePairs.length;
1655
ILonePair[] newLonePairs = new ILonePair[lonePairs.length + growArraySize];
1656
System.arraycopy(lonePairs, 0, newLonePairs, 0, lonePairs.length);
1657
lonePairs = newLonePairs;
1661
* Grows the single electron array by a given size.
1663
*@see #growArraySize
1665
protected void growSingleElectronArray()
1667
growArraySize = (singleElectrons.length < growArraySize) ? growArraySize : singleElectrons.length;
1668
ISingleElectron[] newSingleElectrons = new ISingleElectron[singleElectrons.length + growArraySize];
1669
System.arraycopy(singleElectrons, 0, newSingleElectrons, 0, singleElectrons.length);
1670
singleElectrons = newSingleElectrons;
1674
* Called by objects to which this object has
1675
* registered as a listener.
1677
*@param event A change event pointing to the source of the change
1679
public void stateChanged(IChemObjectChangeEvent event)
1681
notifyChanged(event);