~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/AtomContainer.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $RCSfile$
 
2
 *  $Author: egonw $
 
3
 *  $Date: 2007-10-14 21:44:41 +0200 (Sun, 14 Oct 2007) $
 
4
 *  $Revision: 9060 $
 
5
 *
 
6
 *  Copyright (C) 1997-2007  Christoph Steinbeck
 
7
 *
 
8
 *  Contact: cdk-devel@lists.sourceforge.net
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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.
 
23
 */
 
24
package org.openscience.cdk;
 
25
 
 
26
import org.openscience.cdk.interfaces.*;
 
27
 
 
28
import java.io.Serializable;
 
29
import java.util.*;
 
30
 
 
31
/**
 
32
 *  Base class for all chemical objects that maintain a list of Atoms and
 
33
 *  ElectronContainers. <p>
 
34
 *
 
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();
 
39
 * }
 
40
 *
 
41
 *  </pre>
 
42
 *
 
43
 * @cdk.module data
 
44
 *
 
45
 * @author steinbeck
 
46
 * @cdk.created 2000-10-02
 
47
 */
 
48
public class AtomContainer extends ChemObject 
 
49
  implements IAtomContainer, IChemObjectListener, Serializable, Cloneable {
 
50
 
 
51
        /**
 
52
     * Determines if a de-serialized object is compatible with this class.
 
53
     *
 
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>.
 
58
         */
 
59
        private static final long serialVersionUID = 5678100348445919254L;
 
60
 
 
61
        /**
 
62
         *  Number of atoms contained by this object.
 
63
         */
 
64
        protected int atomCount;
 
65
 
 
66
        /**
 
67
         *  Number of bonds contained by this object.
 
68
         */
 
69
        protected int bondCount;
 
70
        
 
71
        /**
 
72
         *  Number of lone pairs contained by this object.
 
73
         */
 
74
        protected int lonePairCount;
 
75
 
 
76
        /**
 
77
         *  Number of single electrons contained by this object.
 
78
         */
 
79
        protected int singleElectronCount;
 
80
        
 
81
        /**
 
82
         *  Amount by which the bond and arom arrays grow when elements are added and
 
83
         *  the arrays are not large enough for that.
 
84
         */
 
85
        protected int growArraySize = 10;
 
86
 
 
87
        /**
 
88
         *  Internal array of atoms.
 
89
         */
 
90
        protected IAtom[] atoms;
 
91
 
 
92
        /**
 
93
         *  Internal array of bonds.
 
94
         */
 
95
        protected IBond[] bonds;
 
96
        
 
97
        /**
 
98
         *  Internal array of lone pairs.
 
99
         */
 
100
        protected ILonePair[] lonePairs;
 
101
        
 
102
        /**
 
103
         *  Internal array of single electrons.
 
104
         */
 
105
        protected ISingleElectron[] singleElectrons;
 
106
 
 
107
        /**
 
108
         * Internal list of atom parities.
 
109
         */
 
110
        protected Hashtable atomParities;
 
111
 
 
112
 
 
113
        /**
 
114
         *  Constructs an empty AtomContainer.
 
115
         */
 
116
        public AtomContainer() {
 
117
        this(10, 10, 0, 0);
 
118
        }
 
119
 
 
120
 
 
121
        /**
 
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).
 
125
         *
 
126
         * @param  container  An AtomContainer to copy the atoms and electronContainers from
 
127
         */
 
128
        public AtomContainer(IAtomContainer container)
 
129
        {
 
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];
 
138
                
 
139
                atomParities = new Hashtable(atomCount/2);
 
140
 
 
141
                for (int f = 0; f < container.getAtomCount(); f++) {
 
142
                        atoms[f] = container.getAtom(f);
 
143
                        container.getAtom(f).addListener(this);
 
144
                }
 
145
                for (int f = 0; f < this.bondCount; f++) {
 
146
                        bonds[f] = container.getBond(f);
 
147
                        container.getBond(f).addListener(this);
 
148
                }
 
149
                for (int f = 0; f < this.lonePairCount; f++) {
 
150
                        lonePairs[f] = container.getLonePair(f);
 
151
                        container.getLonePair(f).addListener(this);
 
152
                }
 
153
                for (int f = 0; f < this.singleElectronCount; f++) {
 
154
                        singleElectrons[f] = container.getSingleElectron(f);
 
155
                        container.getSingleElectron(f).addListener(this);
 
156
                }
 
157
        }
 
158
 
 
159
 
 
160
        /**
 
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.
 
164
         *
 
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
 
169
         *
 
170
         */
 
171
        public AtomContainer(int atomCount, int bondCount, int lpCount, int seCount)
 
172
        {
 
173
                this.atomCount = 0;
 
174
                this.bondCount = 0;
 
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);
 
182
        }
 
183
 
 
184
    /**
 
185
     * Adds an AtomParity to this container. If a parity is already given for the
 
186
     * affected Atom, it is overwritten.
 
187
     *
 
188
     * @param parity The new AtomParity for this container
 
189
     * @see   #getAtomParity
 
190
     */
 
191
    public void addAtomParity(IAtomParity parity) {
 
192
        atomParities.put(parity.getAtom(), parity);
 
193
    }
 
194
 
 
195
    /**
 
196
     * Returns the atom parity for the given Atom. If no parity is associated
 
197
     * with the given Atom, it returns null.
 
198
     *
 
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
 
203
     */
 
204
    public IAtomParity getAtomParity(IAtom atom) {
 
205
        return (AtomParity)atomParities.get(atom);
 
206
    }
 
207
    
 
208
        /**
 
209
         *  Sets the array of atoms of this AtomContainer.
 
210
         *
 
211
         *@param  atoms  The array of atoms to be assigned to this AtomContainer
 
212
         *@see           #getAtom
 
213
         */
 
214
        public void setAtoms(IAtom[] atoms)
 
215
        {
 
216
                this.atoms = atoms;
 
217
                for (int f = 0; f < atoms.length; f++)
 
218
                {
 
219
                        atoms[f].addListener(this);     
 
220
                }
 
221
                this.atomCount = atoms.length;
 
222
                notifyChanged();
 
223
        }
 
224
 
 
225
        /**
 
226
         * Sets the array of bonds of this AtomContainer.
 
227
         *
 
228
         * @param  bonds  The array of bonds to be assigned to
 
229
         *                             this AtomContainer
 
230
         * @see  #getBond
 
231
         */
 
232
        public void setBonds(IBond[] bonds)
 
233
        {
 
234
                this.bonds = bonds;
 
235
                for (int i = 0; i < bonds.length; ++i) {
 
236
                        bonds[i].addListener(this);
 
237
                }
 
238
                this.bondCount = bonds.length;
 
239
        }
 
240
 
 
241
        /**
 
242
         *  Sets the array of electronContainers of this AtomContainer.
 
243
         *
 
244
         *@param  electronContainers  The array of electronContainers to be assigned to
 
245
         *      this AtomContainer
 
246
         *@see  #getElectronContainers
 
247
         */
 
248
//      public void setElectronContainers(IElectronContainer[] electronContainers)
 
249
//      {
 
250
//              this.electronContainers = electronContainers;
 
251
//              for (int f = 0; f < electronContainers.length; f++)
 
252
//              {
 
253
//                      electronContainers[f].addListener(this);        
 
254
//              }
 
255
//              setElectronContainerCount(electronContainers.length);
 
256
//              notifyChanged();
 
257
//      }
 
258
 
 
259
 
 
260
        /**
 
261
         *  Set the atom at position <code>number</code> in [0,..].
 
262
         *
 
263
         *@param  number  The position of the atom to be set.
 
264
         *@param  atom    The atom to be stored at position <code>number</code>
 
265
         *@see            #getAtom(int)
 
266
         */
 
267
        public void setAtom(int number, IAtom atom)
 
268
        {
 
269
                atom.addListener(this);
 
270
                atoms[number] = atom;
 
271
                notifyChanged();
 
272
        }
 
273
 
 
274
 
 
275
        /**
 
276
         *  Get the atom at position <code>number</code> in [0,..].
 
277
         *
 
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[])
 
282
     *
 
283
         */
 
284
        public IAtom getAtom(int number)
 
285
        {
 
286
                return atoms[number];
 
287
        }
 
288
 
 
289
 
 
290
        /**
 
291
         *  Get the bond at position <code>number</code> in [0,..].
 
292
         *
 
293
         *@param  number  The position of the bond to be retrieved.
 
294
         *@return         The bondAt value
 
295
         */
 
296
        public IBond getBond(int number)
 
297
        {
 
298
                return bonds[number];
 
299
        }
 
300
 
 
301
        /**
 
302
         *  Get the lone pair at position <code>number</code> in [0,..].
 
303
         *
 
304
         *@param  number  The position of the LonePair to be retrieved.
 
305
         *@return         The lone pair number
 
306
         */
 
307
        public ILonePair getLonePair(int number)
 
308
        {
 
309
                return lonePairs[number];
 
310
        }
 
311
 
 
312
        /**
 
313
         *  Get the single electron at position <code>number</code> in [0,..].
 
314
         *
 
315
         *@param  number  The position of the SingleElectron to be retrieved.
 
316
         *@return         The single electron number
 
317
         */
 
318
        public ISingleElectron getSingleElectron(int number)
 
319
        {
 
320
                return singleElectrons[number];
 
321
        }
 
322
        
 
323
        /**
 
324
         * Sets the ElectronContainer at position <code>number</code> in [0,..].
 
325
         *
 
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)
 
329
         */
 
330
//      public void setElectronContainer(int number, IElectronContainer electronContainer)
 
331
//      {
 
332
//              electronContainer.addListener(this);
 
333
//              electronContainers[number] = electronContainer;
 
334
//              notifyChanged();
 
335
//      }
 
336
 
 
337
 
 
338
        /**
 
339
         * Sets the number of electronContainers in this container.
 
340
         *
 
341
         * @param  electronContainerCount  The number of electronContainers in this
 
342
         *                                 container
 
343
         * @see                            #getElectronContainerCount
 
344
         */
 
345
//      public void setElectronContainerCount(int electronContainerCount)
 
346
//      {
 
347
//              this.electronContainerCount = electronContainerCount;
 
348
//              notifyChanged();
 
349
//      }
 
350
 
 
351
 
 
352
        /**
 
353
         *  Sets the number of atoms in this container.
 
354
         *
 
355
         *@param  atomCount  The number of atoms in this container
 
356
         *@see               #getAtomCount
 
357
         */
 
358
//      public void setAtomCount(int atomCount)
 
359
//      {
 
360
//              this.atomCount = atomCount;
 
361
//              notifyChanged();
 
362
//      }
 
363
 
 
364
 
 
365
        /**
 
366
         *  Returns an Iterator for looping over all atoms in this container.
 
367
         *
 
368
         *@return    An Iterator with the atoms in this container
 
369
         */
 
370
        public java.util.Iterator atoms()
 
371
        {
 
372
                return new AtomIterator();
 
373
        }
 
374
 
 
375
        /**
 
376
     * The inner AtomIterator class.
 
377
     *
 
378
     */
 
379
    private class AtomIterator implements java.util.Iterator {
 
380
 
 
381
        private int pointer = 0;
 
382
        
 
383
        public boolean hasNext() {
 
384
            return pointer < atomCount;
 
385
        }
 
386
 
 
387
        public Object next() {
 
388
            return atoms[pointer++];
 
389
        }
 
390
 
 
391
        public void remove() {
 
392
            removeAtom(--pointer);
 
393
        }
 
394
        
 
395
    }
 
396
        
 
397
    /**
 
398
         *  Returns an Iterator for looping over all bonds in this container.
 
399
         *
 
400
         *@return    An Iterator with the bonds in this container
 
401
         */
 
402
        public java.util.Iterator bonds()
 
403
        {
 
404
                return new BondIterator();
 
405
        }
 
406
 
 
407
        /**
 
408
     * The inner BondIterator class.
 
409
     *
 
410
     */
 
411
    private class BondIterator implements java.util.Iterator {
 
412
 
 
413
        private int pointer = 0;
 
414
        
 
415
        public boolean hasNext() {
 
416
            return pointer < bondCount;
 
417
        }
 
418
 
 
419
        public Object next() {
 
420
            return bonds[pointer++];
 
421
        }
 
422
 
 
423
        public void remove() {
 
424
            removeBond(--pointer);
 
425
        }
 
426
        
 
427
    }
 
428
    
 
429
    /**
 
430
         *  Returns an Iterator for looping over all lone pairs in this container.
 
431
         *
 
432
         *@return    An Iterator with the lone pairs in this container
 
433
         */
 
434
        public Iterator lonePairs()
 
435
        {
 
436
                return new LonePairIterator();
 
437
        }
 
438
    
 
439
        /**
 
440
     * The inner LonePairIterator class.
 
441
     *
 
442
     */
 
443
    private class LonePairIterator implements java.util.Iterator {
 
444
 
 
445
        private int pointer = 0;
 
446
        
 
447
        public boolean hasNext() {
 
448
            return pointer < lonePairCount;
 
449
        }
 
450
 
 
451
        public Object next() {
 
452
            return lonePairs[pointer++];
 
453
        }
 
454
 
 
455
        public void remove() {
 
456
            removeLonePair(--pointer);
 
457
        }
 
458
        
 
459
    }
 
460
        
 
461
    /**
 
462
         *  Returns an Iterator for looping over all single electrons in this container.
 
463
         *
 
464
         *@return    An Iterator with the single electrons in this container
 
465
         */
 
466
        public Iterator singleElectrons()
 
467
        {
 
468
                return new SingleElectronIterator();
 
469
        }
 
470
        
 
471
        /**
 
472
     * The inner SingleElectronIterator class.
 
473
     *
 
474
     */
 
475
    private class SingleElectronIterator implements java.util.Iterator {
 
476
 
 
477
        private int pointer = 0;
 
478
        
 
479
        public boolean hasNext() {
 
480
            return pointer < singleElectronCount;
 
481
        }
 
482
 
 
483
        public Object next() {
 
484
            return singleElectrons[pointer++];
 
485
        }
 
486
 
 
487
        public void remove() {
 
488
            removeSingleElectron(--pointer);
 
489
        }
 
490
        
 
491
    }
 
492
    
 
493
    /**
 
494
         *  Returns an Iterator for looping over all electron containers in this container.
 
495
         *
 
496
         *@return    An Iterator with the electron containers in this container
 
497
         */
 
498
        public Iterator electronContainers()
 
499
        {
 
500
                return new ElectronContainerIterator();
 
501
        }
 
502
    
 
503
        /**
 
504
     * The inner ElectronContainerIterator class.
 
505
     *
 
506
     */
 
507
    private class ElectronContainerIterator implements java.util.Iterator {
 
508
 
 
509
        private int pointer = 0;
 
510
        
 
511
        public boolean hasNext() {
 
512
            return pointer < (bondCount + lonePairCount + singleElectronCount);
 
513
        }
 
514
 
 
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];
 
519
            return null;
 
520
        }
 
521
 
 
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);
 
526
        }
 
527
        
 
528
    }
 
529
        
 
530
        /**
 
531
         *  Returns the atom at position 0 in the container.
 
532
         *
 
533
         *@return    The atom at position 0 .
 
534
         */
 
535
        public IAtom getFirstAtom()
 
536
        {
 
537
                return (Atom)atoms[0];
 
538
        }
 
539
 
 
540
 
 
541
        /**
 
542
         *  Returns the atom at the last position in the container.
 
543
         *
 
544
         *@return    The atom at the last position
 
545
         */
 
546
        public IAtom getLastAtom()
 
547
        {
 
548
                return getAtomCount() > 0 ? (Atom)atoms[getAtomCount() - 1] : null;
 
549
        }
 
550
 
 
551
 
 
552
        /**
 
553
         *  Returns the position of a given atom in the atoms array. It returns -1 if
 
554
         *  the atom does not exist.
 
555
         *
 
556
         *@param  atom  The atom to be sought
 
557
         *@return       The Position of the atom in the atoms array in [0,..].
 
558
         */
 
559
        public int getAtomNumber(IAtom atom)
 
560
        {
 
561
                for (int f = 0; f < atomCount; f++)
 
562
                {
 
563
                        if (atoms[f] == atom) return f;
 
564
                }
 
565
                return -1;
 
566
        }
 
567
 
 
568
 
 
569
        /**
 
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.
 
572
         *
 
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.
 
577
         */
 
578
        public int getBondNumber(IAtom atom1, IAtom atom2)
 
579
        {
 
580
                return (getBondNumber(getBond(atom1, atom2)));
 
581
        }
 
582
 
 
583
 
 
584
        /**
 
585
         *  Returns the position of a given bond in the electronContainers array. It
 
586
         *  returns -1 if the bond does not exist.
 
587
         *
 
588
         *@param  bond  The bond to be sought
 
589
         *@return       The Position of the bond in the electronContainers array in [0,..].
 
590
         */
 
591
        public int getBondNumber(IBond bond)
 
592
        {
 
593
                for (int f = 0; f < bondCount; f++)
 
594
                {
 
595
                        if (bonds[f] == bond) return f;
 
596
                }
 
597
                return -1;
 
598
        }
 
599
 
 
600
        /**
 
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.
 
603
         *
 
604
         *@param  lonePair  The lone pair to be sought
 
605
         *@return       The Position of the lone pair in the array..
 
606
         */
 
607
        public int getLonePairNumber(ILonePair lonePair)
 
608
        {
 
609
                for (int f = 0; f < lonePairCount; f++)
 
610
                {
 
611
                        if (lonePairs[f] == lonePair) return f;
 
612
                }
 
613
                return -1;
 
614
        }
 
615
 
 
616
        /**
 
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.
 
619
         *
 
620
         *@param  atom  The single electron to be sought
 
621
         *@return       The Position of the single electron in the array.
 
622
         */
 
623
        public int getSingleElectronNumber(ISingleElectron singleElectron)
 
624
        {
 
625
                for (int f = 0; f < singleElectronCount; f++)
 
626
                {
 
627
                        if (singleElectrons[f] == singleElectron) return f;
 
628
                }
 
629
                return -1;
 
630
        }
 
631
        
 
632
        /**
 
633
         *  Returns the ElectronContainer at position <code>number</code> in the
 
634
         *  container.
 
635
         *
 
636
         * @param  number  The position of the ElectronContainer to be returned.
 
637
         * @return         The ElectronContainer at position <code>number</code>.
 
638
         */
 
639
        public IElectronContainer getElectronContainer(int number)
 
640
        {
 
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];
 
646
                return null;
 
647
        }
 
648
 
 
649
 
 
650
        /**
 
651
         * Returns the bond that connectes the two given atoms.
 
652
         *
 
653
         * @param  atom1  The first atom
 
654
         * @param  atom2  The second atom
 
655
         * @return        The bond that connectes the two atoms
 
656
         */
 
657
        public IBond getBond(IAtom atom1, IAtom atom2)
 
658
        {
 
659
                for (int i = 0; i < getBondCount(); i++)
 
660
                {
 
661
                        if (bonds[i].contains(atom1) &&
 
662
                            bonds[i].getConnectedAtom(atom1) == atom2) {
 
663
                                return bonds[i];
 
664
                        }
 
665
                }
 
666
                return null;
 
667
        }
 
668
 
 
669
        /**
 
670
         *  Returns the number of Atoms in this Container.
 
671
         *
 
672
         *@return    The number of Atoms in this Container
 
673
         */
 
674
        public int getAtomCount()
 
675
        {
 
676
                return this.atomCount;
 
677
        }
 
678
        
 
679
        /**
 
680
         *  Returns the number of Bonds in this Container.
 
681
         *
 
682
         *@return    The number of Bonds in this Container
 
683
         */
 
684
        public int getBondCount()
 
685
        {
 
686
                return this.bondCount;
 
687
        }
 
688
 
 
689
        /**
 
690
         *  Returns the number of LonePairs in this Container.
 
691
         *
 
692
         *@return    The number of LonePairs in this Container
 
693
         */
 
694
        public int getLonePairCount()
 
695
        {
 
696
                return this.lonePairCount;
 
697
        }
 
698
 
 
699
        /**
 
700
         *  Returns the number of the single electrons in this container,
 
701
         *
 
702
         *@return       The number of SingleElectron objects of this AtomContainer
 
703
         */
 
704
        public int getSingleElectronCount()
 
705
        {
 
706
                return this.singleElectronCount;
 
707
        }
 
708
 
 
709
        /**
 
710
         * Returns the number of ElectronContainers in this Container.
 
711
         *
 
712
         * @return    The number of ElectronContainers in this Container
 
713
         */
 
714
        public int getElectronContainerCount()
 
715
        {
 
716
                return this.bondCount + this.lonePairCount + this.singleElectronCount;
 
717
        }
 
718
 
 
719
        /**
 
720
         *  Returns an ArrayList of all atoms connected to the given atom.
 
721
         *
 
722
         *@param  atom  The atom the bond partners are searched of.
 
723
         *@return       The ArrayList with the connected atoms
 
724
         */
 
725
        public List getConnectedAtomsList(IAtom atom)
 
726
        {
 
727
                List atomsList = new ArrayList();
 
728
                for (int i = 0; i < bondCount; i++)
 
729
                {
 
730
                        if (bonds[i].contains(atom)) atomsList.add(bonds[i].getConnectedAtom(atom));
 
731
                }
 
732
                return atomsList;
 
733
        }
 
734
 
 
735
        /**
 
736
         *  Returns an ArrayList of all Bonds connected to the given atom.
 
737
         *
 
738
         *@param  atom  The atom the connected bonds are searched of
 
739
         *@return       The ArrayList with connected atoms
 
740
         */
 
741
        public List getConnectedBondsList(IAtom atom)
 
742
        {
 
743
                List bondsList = new ArrayList();
 
744
                for (int i = 0; i < bondCount; i++)
 
745
                {
 
746
                        if (bonds[i].contains(atom)) bondsList.add(bonds[i]);
 
747
                }
 
748
                return bondsList;
 
749
        }
 
750
 
 
751
    /**
 
752
     * Returns the array of lone pairs connected to an atom.
 
753
     *
 
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()
 
758
     * @see #getBond
 
759
     */
 
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]);
 
764
        }
 
765
        return lps;
 
766
    }
 
767
        
 
768
        /**
 
769
         *  Returns an array of all SingleElectron connected to the given atom.
 
770
         *
 
771
         *@param  atom  The atom on which the single electron is located
 
772
         *@return       The array of SingleElectron of this AtomContainer
 
773
         */
 
774
        public List getConnectedSingleElectronsList(IAtom atom)
 
775
        {
 
776
                List lps = new ArrayList();
 
777
                for (int i = 0; i < singleElectronCount; i++)
 
778
                {
 
779
                        if (singleElectrons[i].contains(atom)) lps.add(singleElectrons[i]);
 
780
                }
 
781
                return lps;
 
782
        }
 
783
        
 
784
        /**
 
785
         *  Returns an ArrayList of all electronContainers connected to the given atom.
 
786
         *
 
787
         *@param  atom  The atom the connected electronContainers are searched of
 
788
         *@return       The ArrayList with the  connected atoms
 
789
         */
 
790
        public List getConnectedElectronContainersList(IAtom atom)
 
791
        {
 
792
                List lps = new ArrayList();
 
793
                for (int i = 0; i < bondCount; i++)
 
794
                {
 
795
                        if (bonds[i].contains(atom)) lps.add(bonds[i]);
 
796
                }
 
797
                for (int i = 0; i < lonePairCount; i++)
 
798
                {
 
799
                        if (lonePairs[i].contains(atom)) lps.add(lonePairs[i]);
 
800
                }
 
801
                for (int i = 0; i < singleElectronCount; i++)
 
802
                {
 
803
                        if (singleElectrons[i].contains(atom)) lps.add(singleElectrons[i]);
 
804
                }
 
805
                return lps;
 
806
        }
 
807
        
 
808
        /**
 
809
         *  Returns the number of atoms connected to the given atom.
 
810
         *
 
811
         *@param  atom  The atom the number of bond partners are searched of.
 
812
         *@return       The the size of connected atoms
 
813
         */
 
814
        public int getConnectedAtomsCount(IAtom atom)
 
815
        {
 
816
                int count = 0;
 
817
                for (int i = 0; i < bondCount; i++)
 
818
                {
 
819
                        if (bonds[i].contains(atom)) ++count;
 
820
                }
 
821
                return count;
 
822
        }
 
823
 
 
824
        /**
 
825
         *  Returns the number of Bonds for a given Atom.
 
826
         *
 
827
         *@param  atom  The atom
 
828
         *@return       The number of Bonds for this atom
 
829
         */
 
830
        public int getConnectedBondsCount(IAtom atom)
 
831
        {
 
832
                return getConnectedAtomsCount(atom);
 
833
        }
 
834
        
 
835
        /**
 
836
         *  Returns the number of connected atoms (degree) to the given atom.
 
837
         *
 
838
         *@param  atomNumber  The atomnumber the degree is searched for
 
839
         *@return             The number of connected atoms (degree)
 
840
         */
 
841
        public int getConnectedBondsCount(int atomNumber)
 
842
        {
 
843
                return getConnectedAtomsCount(atoms[atomNumber]);
 
844
        }
 
845
 
 
846
        /**
 
847
         *  Returns the number of LonePairs for a given Atom.
 
848
         *
 
849
         *@param  atom  The atom
 
850
         *@return       The number of LonePairs for this atom
 
851
         */
 
852
        public int getConnectedLonePairsCount(IAtom atom)
 
853
        {
 
854
                int count = 0;
 
855
                for (int i = 0; i < lonePairCount; i++)
 
856
                {
 
857
                        if (lonePairs[i].contains(atom)) ++count;
 
858
                }
 
859
                return count;
 
860
        }
 
861
        
 
862
        /**
 
863
         *  Returns the sum of the SingleElectron for a given Atom.
 
864
         *
 
865
         *@param  atom  The atom on which the single electron is located
 
866
         *@return       The array of SingleElectron of this AtomContainer
 
867
         */
 
868
        public int getConnectedSingleElectronsCount(IAtom atom)
 
869
        {
 
870
                int count = 0;
 
871
                for (int i = 0; i < singleElectronCount; i++)
 
872
                {
 
873
                        if (singleElectrons[i].contains(atom)) ++count;
 
874
                }
 
875
                return count;
 
876
        }
 
877
        
 
878
 
 
879
        /**
 
880
         * Returns the sum of the bond orders for a given Atom.
 
881
         *
 
882
         * @param  atom  The atom
 
883
         * @return       The number of bondorders for this atom
 
884
         */
 
885
        public double getBondOrderSum(IAtom atom)
 
886
        {
 
887
                double count = 0;
 
888
                for (int i = 0; i < bondCount; i++)
 
889
                {
 
890
                        if (bonds[i].contains(atom)) count += bonds[i].getOrder();
 
891
                }
 
892
                return count;
 
893
        }
 
894
 
 
895
    /**
 
896
         * Returns the maximum bond order that this atom currently has in the context
 
897
         * of this AtomContainer.
 
898
         *
 
899
         * @param  atom  The atom
 
900
         * @return       The maximum bond order that this atom currently has
 
901
         */
 
902
        public double getMaximumBondOrder(IAtom atom) {
 
903
                double max = 0.0;
 
904
                for (int i = 0; i < bondCount; i++)
 
905
                {
 
906
                        if (bonds[i].contains(atom) && bonds[i].getOrder() > max)
 
907
                        {
 
908
                                max = bonds[i].getOrder();
 
909
                        }
 
910
                }
 
911
                return max;
 
912
        }
 
913
 
 
914
 
 
915
        /**
 
916
         *  Returns the minimum bond order that this atom currently has in the context
 
917
         *  of this AtomContainer.
 
918
         *
 
919
         *@param  atom  The atom
 
920
         *@return       The minimim bond order that this atom currently has
 
921
         */
 
922
        public double getMinimumBondOrder(IAtom atom)
 
923
        {
 
924
                double min = 6;
 
925
                for (int i = 0; i < bondCount; i++)
 
926
                {
 
927
                        if (bonds[i].contains(atom) && bonds[i].getOrder() < min)
 
928
                        {
 
929
                                min = bonds[i].getOrder();
 
930
                        }
 
931
                }
 
932
                return min;
 
933
        }
 
934
 
 
935
        /**
 
936
         *  Adds all atoms and electronContainers of a given atomcontainer to this
 
937
         *  container.
 
938
         *
 
939
         *@param  atomContainer  The atomcontainer to be added
 
940
         */
 
941
        public void add(IAtomContainer atomContainer)
 
942
        {
 
943
                for (int f = 0; f < atomContainer.getAtomCount(); f++)
 
944
                {
 
945
                        if (!contains(atomContainer.getAtom(f)))
 
946
                        {
 
947
                                addAtom(atomContainer.getAtom(f));
 
948
                        }
 
949
                }
 
950
                for (int f = 0; f < atomContainer.getBondCount(); f++)
 
951
                {
 
952
                        if (!contains(atomContainer.getBond(f)))
 
953
                        {
 
954
                                addBond(atomContainer.getBond(f));
 
955
                        }
 
956
                }
 
957
                for (int f = 0; f < atomContainer.getLonePairCount(); f++)
 
958
                {
 
959
                        if (!contains(atomContainer.getLonePair(f)))
 
960
                        {
 
961
                                addLonePair(atomContainer.getLonePair(f));
 
962
                        }
 
963
                }
 
964
                for (int f = 0; f < atomContainer.getSingleElectronCount(); f++)
 
965
                {
 
966
                        if (!contains(atomContainer.getSingleElectron(f)))
 
967
                        {
 
968
                                addSingleElectron(atomContainer.getSingleElectron(f));
 
969
                        }
 
970
                }
 
971
                notifyChanged();
 
972
        }
 
973
 
 
974
        /**
 
975
         *  Adds the <code>ElectronContainer</code>s found in atomContainer to this
 
976
         *  container.
 
977
         *
 
978
         *@param  atomContainer  AtomContainer with the new ElectronContainers
 
979
         */
 
980
//      public void addElectronContainers(IAtomContainer atomContainer)
 
981
//      {
 
982
//              
 
983
//              notifyChanged();
 
984
//      }
 
985
        
 
986
 
 
987
        /**
 
988
         *  Adds an atom to this container.
 
989
         *
 
990
         *@param  atom  The atom to be added to this container
 
991
         */
 
992
        public void addAtom(IAtom atom)
 
993
        {
 
994
                if (contains(atom))
 
995
                {
 
996
                        return;
 
997
                }
 
998
 
 
999
                if (atomCount + 1 >= atoms.length)
 
1000
                {
 
1001
                        growAtomArray();
 
1002
                }
 
1003
                atom.addListener(this);
 
1004
                atoms[atomCount] = atom;
 
1005
                atomCount++;
 
1006
                notifyChanged();
 
1007
        }
 
1008
 
 
1009
 
 
1010
        /**
 
1011
         *  Adds a Bond to this AtomContainer.
 
1012
         *
 
1013
         *@param  bond  The bond to added to this container
 
1014
         */
 
1015
        public void addBond(IBond bond)
 
1016
        {
 
1017
                if (bondCount >= bonds.length) growBondArray();
 
1018
                bonds[bondCount] = bond;
 
1019
                ++bondCount;
 
1020
                notifyChanged();
 
1021
        }
 
1022
 
 
1023
        /**
 
1024
         *  Adds a lone pair to this AtomContainer.
 
1025
         *
 
1026
         *@param  lonePair  The LonePair to added to this container
 
1027
         */
 
1028
        public void addLonePair(ILonePair lonePair)
 
1029
        {
 
1030
                if (lonePairCount >= lonePairs.length) growLonePairArray();
 
1031
                lonePairs[lonePairCount] = lonePair;
 
1032
                ++lonePairCount;
 
1033
                notifyChanged();
 
1034
        }
 
1035
        
 
1036
        /**
 
1037
         *  Adds a single electron to this AtomContainer.
 
1038
         *
 
1039
         *@param  singleElectron  The SingleElectron to added to this container
 
1040
         */
 
1041
        public void addSingleElectron(ISingleElectron singleElectron)
 
1042
        {
 
1043
                if (singleElectronCount >= singleElectrons.length) growSingleElectronArray();
 
1044
                singleElectrons[singleElectronCount] = singleElectron;
 
1045
                ++singleElectronCount;
 
1046
                notifyChanged();
 
1047
        }
 
1048
        
 
1049
        /**
 
1050
         *  Adds a ElectronContainer to this AtomContainer.
 
1051
         *
 
1052
         *@param  electronContainer  The ElectronContainer to added to this container
 
1053
         */
 
1054
        public void addElectronContainer(IElectronContainer electronContainer)
 
1055
        {
 
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);
 
1059
        }
 
1060
 
 
1061
 
 
1062
        /**
 
1063
         *  Removes all atoms and electronContainers of a given atomcontainer from this
 
1064
         *  container.
 
1065
         *
 
1066
         *@param  atomContainer  The atomcontainer to be removed
 
1067
         */
 
1068
        public void remove(IAtomContainer atomContainer)
 
1069
        {
 
1070
                for (int f = 0; f < atomContainer.getAtomCount(); f++)
 
1071
                {
 
1072
                        removeAtom(atomContainer.getAtom(f));
 
1073
                }
 
1074
                for (int f = 0; f < atomContainer.getBondCount(); f++)
 
1075
                {
 
1076
                        removeBond(atomContainer.getBond(f));
 
1077
                }
 
1078
                for (int f = 0; f < atomContainer.getLonePairCount(); f++)
 
1079
                {
 
1080
                        removeLonePair(atomContainer.getLonePair(f));
 
1081
                }
 
1082
                for (int f = 0; f < atomContainer.getSingleElectronCount(); f++)
 
1083
                {
 
1084
                        removeSingleElectron(atomContainer.getSingleElectron(f));
 
1085
                }
 
1086
        }
 
1087
 
 
1088
        /**
 
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.
 
1092
         *
 
1093
         *@param  position  The position of the atom to be removed.
 
1094
         */
 
1095
        public void removeAtom(int position)
 
1096
        {
 
1097
                atoms[position].removeListener(this);
 
1098
                for (int i = position; i < atomCount - 1; i++)
 
1099
                {
 
1100
                        atoms[i] = atoms[i + 1];
 
1101
                }
 
1102
                atoms[atomCount - 1] = null;
 
1103
                atomCount--;
 
1104
                notifyChanged();
 
1105
        }
 
1106
        
 
1107
        /**
 
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.
 
1111
         *
 
1112
         *@param  atom  The atom to be removed
 
1113
         */
 
1114
        public void removeAtom(IAtom atom)
 
1115
        {
 
1116
                int position = getAtomNumber(atom);
 
1117
                if (position != -1)
 
1118
                {
 
1119
                        removeAtom(position);
 
1120
                }
 
1121
        }
 
1122
 
 
1123
        /**
 
1124
         *  Removes the bond at the given position from the AtomContainer.
 
1125
         *
 
1126
         *@param  position  The position of the bond to be removed.
 
1127
         */
 
1128
        public IBond removeBond(int position)
 
1129
        {
 
1130
                IBond bond = bonds[position];
 
1131
                bond.removeListener(this);
 
1132
                for (int i = position; i < bondCount - 1; i++)
 
1133
                {
 
1134
                        bonds[i] = bonds[i + 1];
 
1135
                }
 
1136
                bonds[bondCount - 1] = null;
 
1137
                bondCount--;
 
1138
                notifyChanged();
 
1139
                return bond;
 
1140
        }
 
1141
        
 
1142
        /**
 
1143
         * Removes the bond that connects the two given atoms.
 
1144
         *
 
1145
         * @param  atom1  The first atom
 
1146
         * @param  atom2  The second atom
 
1147
         * @return        The bond that connectes the two atoms
 
1148
         */
 
1149
        public IBond removeBond(IAtom atom1, IAtom atom2)
 
1150
        {
 
1151
                int pos = getBondNumber(atom1, atom2);
 
1152
                IBond bond = null;
 
1153
                if (pos != -1) {
 
1154
                        bond = bonds[pos];
 
1155
                        removeBond(pos);
 
1156
                }
 
1157
                return bond;
 
1158
        }
 
1159
        
 
1160
        /**
 
1161
         * Removes the bond from this container.
 
1162
         *
 
1163
         * @param  bond   The bond to be removed.
 
1164
         */
 
1165
        public void removeBond(IBond bond)
 
1166
        {
 
1167
                int pos = getBondNumber(bond);
 
1168
                if (pos != -1) removeBond(pos);
 
1169
        }
 
1170
        
 
1171
        /**
 
1172
         *  Removes the lone pair at the given position from the AtomContainer.
 
1173
         *
 
1174
         *@param  position  The position of the LonePair to be removed.
 
1175
         */
 
1176
        public ILonePair removeLonePair(int position)
 
1177
        {
 
1178
                ILonePair lp = lonePairs[position];
 
1179
                lp.removeListener(this);
 
1180
                for (int i = position; i < lonePairCount - 1; i++)
 
1181
                {
 
1182
                        lonePairs[i] = lonePairs[i + 1];
 
1183
                }
 
1184
                lonePairs[lonePairCount - 1] = null;
 
1185
                lonePairCount--;
 
1186
                notifyChanged();
 
1187
                return lp;
 
1188
        }
 
1189
        
 
1190
        /**
 
1191
         *  Removes the lone pair from the AtomContainer.
 
1192
         *
 
1193
         *@param  lonePair  The LonePair to be removed.
 
1194
         */
 
1195
        public void removeLonePair(ILonePair lonePair)
 
1196
        {
 
1197
                int pos = getLonePairNumber(lonePair);
 
1198
                if (pos != -1) removeLonePair(pos);
 
1199
        }
 
1200
        
 
1201
        /**
 
1202
         *  Removes the single electron at the given position from the AtomContainer.
 
1203
         *
 
1204
         *@param  position  The position of the SingleElectron to be removed.
 
1205
         */
 
1206
        public ISingleElectron removeSingleElectron(int position)
 
1207
        {
 
1208
                ISingleElectron se = singleElectrons[position];
 
1209
                se.removeListener(this);
 
1210
                for (int i = position; i < singleElectronCount - 1; i++)
 
1211
                {
 
1212
                        singleElectrons[i] = singleElectrons[i + 1];
 
1213
                }
 
1214
                singleElectrons[singleElectronCount - 1] = null;
 
1215
                singleElectronCount--;
 
1216
                notifyChanged();
 
1217
                return se;
 
1218
        }
 
1219
        
 
1220
        /**
 
1221
         *  Removes the single electron from the AtomContainer.
 
1222
         *
 
1223
         *@param  singleElectron  The SingleElectron to be removed.
 
1224
         */
 
1225
        public void removeSingleElectron(ISingleElectron singleElectron)
 
1226
        {
 
1227
                int pos = getSingleElectronNumber(singleElectron);
 
1228
                if (pos != -1) removeSingleElectron(pos);
 
1229
        }
 
1230
        
 
1231
        /**
 
1232
         * Removes the bond at the given position from this container.
 
1233
         *
 
1234
         * @param  number  The position of the bond in the electronContainers array
 
1235
         * @return           Bond that was removed
 
1236
         */
 
1237
        public IElectronContainer removeElectronContainer(int number)
 
1238
        {
 
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);
 
1244
                return null;
 
1245
        }
 
1246
 
 
1247
 
 
1248
    /**
 
1249
     * Removes this ElectronContainer from this container.
 
1250
     *
 
1251
     * @param electronContainer The electronContainer to be removed
 
1252
     */
 
1253
    public void removeElectronContainer(IElectronContainer electronContainer) {
 
1254
        if (electronContainer instanceof IBond) removeBond((IBond) electronContainer);
 
1255
        else if (electronContainer instanceof ILonePair) removeLonePair((ILonePair) electronContainer);
 
1256
        else
 
1257
        if (electronContainer instanceof ISingleElectron) removeSingleElectron((ISingleElectron) electronContainer);
 
1258
        }
 
1259
 
 
1260
        /**
 
1261
         *  Removes the given atom and all connected electronContainers from the
 
1262
         *  AtomContainer.
 
1263
         *
 
1264
         *@param  atom  The atom to be removed
 
1265
         */
 
1266
        public void removeAtomAndConnectedElectronContainers(IAtom atom)
 
1267
        {
 
1268
                int position = getAtomNumber(atom);
 
1269
                if (position != -1)
 
1270
                {
 
1271
                        for (int i = 0; i < bondCount; i++)
 
1272
                        {
 
1273
                                if (bonds[i].contains(atom)) {
 
1274
                                        removeBond(i);
 
1275
                                        --i;
 
1276
                                }
 
1277
                        }
 
1278
                        for (int i = 0; i < lonePairCount; i++)
 
1279
                        {
 
1280
                                if (lonePairs[i].contains(atom)) {
 
1281
                                        removeLonePair(i);
 
1282
                                        --i;
 
1283
                                }
 
1284
                        }
 
1285
                        for (int i = 0; i < singleElectronCount; i++)
 
1286
                        {
 
1287
                                if (singleElectrons[i].contains(atom)) {
 
1288
                                        removeSingleElectron(i);
 
1289
                                        --i;
 
1290
                                }
 
1291
                        }
 
1292
                        removeAtom(position);
 
1293
                }
 
1294
                notifyChanged();
 
1295
        }
 
1296
 
 
1297
        /**
 
1298
         * Removes all atoms and bond from this container.
 
1299
         */
 
1300
        public void removeAllElements() {
 
1301
                removeAllElectronContainers();
 
1302
        for (int f = 0; f < getAtomCount(); f++) {
 
1303
                        getAtom(f).removeListener(this);        
 
1304
                }
 
1305
        atoms = new IAtom[growArraySize];
 
1306
        atomCount = 0;
 
1307
                notifyChanged();
 
1308
        }
 
1309
 
 
1310
 
 
1311
        /**
 
1312
         *  Removes electronContainers from this container.
 
1313
         */
 
1314
        public void removeAllElectronContainers()
 
1315
        {
 
1316
                removeAllBonds();
 
1317
                for (int f = 0; f < getLonePairCount(); f++) {
 
1318
                        getLonePair(f).removeListener(this);    
 
1319
                }
 
1320
                for (int f = 0; f < getSingleElectronCount(); f++) {
 
1321
                        getSingleElectron(f).removeListener(this);      
 
1322
                }
 
1323
                lonePairs = new ILonePair[growArraySize];
 
1324
                singleElectrons = new ISingleElectron[growArraySize];
 
1325
                lonePairCount = 0;
 
1326
                singleElectronCount = 0;
 
1327
                notifyChanged();
 
1328
        }
 
1329
 
 
1330
    /**
 
1331
     *  Removes all Bonds from this container.
 
1332
     */
 
1333
    public void removeAllBonds() {
 
1334
        for (int f = 0; f < getBondCount(); f++) {
 
1335
                        getBond(f).removeListener(this);        
 
1336
                }
 
1337
        bonds = new IBond[growArraySize];
 
1338
        bondCount = 0;
 
1339
        notifyChanged();
 
1340
    }
 
1341
 
 
1342
        /**
 
1343
         *  Adds a bond to this container.
 
1344
         *
 
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
 
1349
         */
 
1350
        public void addBond(int atom1, int atom2, double order, int stereo)
 
1351
        {
 
1352
                IBond bond = getBuilder().newBond(getAtom(atom1), getAtom(atom2), order, stereo);
 
1353
 
 
1354
                if (contains(bond))
 
1355
                {
 
1356
                        return;
 
1357
                }
 
1358
 
 
1359
                if (bondCount >= bonds.length)
 
1360
                {
 
1361
                        growBondArray();
 
1362
                }
 
1363
                addBond(bond);
 
1364
                /* no notifyChanged() here because addBond(bond) does 
 
1365
                   it already */
 
1366
        }
 
1367
 
 
1368
 
 
1369
        /**
 
1370
         *  Adds a bond to this container.
 
1371
         *
 
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
 
1375
         */
 
1376
        public void addBond(int atom1, int atom2, double order)
 
1377
        {
 
1378
                IBond bond = getBuilder().newBond(getAtom(atom1), getAtom(atom2), order);
 
1379
 
 
1380
                if (bondCount >= bonds.length)
 
1381
                {
 
1382
                        growBondArray();
 
1383
                }
 
1384
                addBond(bond);
 
1385
                /* no notifyChanged() here because addBond(bond) does 
 
1386
                   it already */
 
1387
        }
 
1388
 
 
1389
 
 
1390
        /**
 
1391
         *  Adds a LonePair to this Atom.
 
1392
         *
 
1393
         *@param  atomID  The atom number to which the LonePair is added in [0,..]
 
1394
         */
 
1395
        public void addLonePair(int atomID)
 
1396
        {
 
1397
                ILonePair lonePair = getBuilder().newLonePair(atoms[atomID]);
 
1398
                lonePair.addListener(this);
 
1399
                addLonePair(lonePair);
 
1400
                /* no notifyChanged() here because addElectronContainer() does 
 
1401
                   it already */
 
1402
        }
 
1403
        
 
1404
        /**
 
1405
         *  Adds a LonePair to this Atom.
 
1406
         *
 
1407
         *@param  atomID  The atom number to which the LonePair is added in [0,..]
 
1408
         */
 
1409
        public void addSingleElectron(int atomID)
 
1410
        {
 
1411
                ISingleElectron singleElectron = getBuilder().newSingleElectron(atoms[atomID]);
 
1412
                singleElectron.addListener(this);
 
1413
                addSingleElectron(singleElectron);
 
1414
                /* no notifyChanged() here because addSingleElectron() does 
 
1415
                   it already */
 
1416
        }
 
1417
 
 
1418
        /**
 
1419
         *  True, if the AtomContainer contains the given atom object.
 
1420
         *
 
1421
         *@param  atom  the atom this AtomContainer is searched for
 
1422
         *@return       True, if the AtomContainer contains the given atom object
 
1423
         */
 
1424
        public boolean contains(IAtom atom)
 
1425
        {
 
1426
                for (int i = 0; i < getAtomCount(); i++)
 
1427
                {
 
1428
                        if (atom == atoms[i]) return true;
 
1429
                }
 
1430
                return false;
 
1431
        }
 
1432
        
 
1433
        /**
 
1434
         *  True, if the AtomContainer contains the given bond object.
 
1435
         *
 
1436
         *@param  bond  the bond this AtomContainer is searched for
 
1437
         *@return       True, if the AtomContainer contains the given bond object
 
1438
         */
 
1439
        public boolean contains(IBond bond)
 
1440
        {
 
1441
                for (int i = 0; i < getBondCount(); i++)
 
1442
                {
 
1443
                        if (bond == bonds[i]) return true;
 
1444
                }
 
1445
                return false;
 
1446
        }
 
1447
        
 
1448
        /**
 
1449
         *  True, if the AtomContainer contains the given LonePair object.
 
1450
         *
 
1451
         *@param  lonePair  the LonePair this AtomContainer is searched for
 
1452
         *@return           True, if the AtomContainer contains the given LonePair object
 
1453
         */
 
1454
        public boolean contains(ILonePair lonePair)
 
1455
        {
 
1456
                for (int i = 0; i < getLonePairCount(); i++)
 
1457
                {
 
1458
                        if (lonePair == lonePairs[i]) return true;
 
1459
                }
 
1460
                return false;
 
1461
        }
 
1462
        
 
1463
        /**
 
1464
         *  True, if the AtomContainer contains the given SingleElectron object.
 
1465
         *
 
1466
         *@param  singleElectron  the LonePair this AtomContainer is searched for
 
1467
         *@return           True, if the AtomContainer contains the given LonePair object
 
1468
         */
 
1469
        public boolean contains(ISingleElectron singleElectron)
 
1470
        {
 
1471
                for (int i = 0; i < getSingleElectronCount(); i++)
 
1472
                {
 
1473
                        if (singleElectron == singleElectrons[i]) return true;
 
1474
                }
 
1475
                return false;
 
1476
        }
 
1477
        
 
1478
        /**
 
1479
         *  True, if the AtomContainer contains the given ElectronContainer object.
 
1480
         *
 
1481
         *@param  electronContainer ElectronContainer that is searched for
 
1482
         *@return                   True, if the AtomContainer contains the given bond object
 
1483
         */
 
1484
        public boolean contains(IElectronContainer electronContainer)
 
1485
        {
 
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);
 
1489
                return false;
 
1490
        }
 
1491
        
 
1492
        /**
 
1493
         *  Returns a one line string representation of this Container. This method is
 
1494
         *  conform RFC #9.
 
1495
         *
 
1496
         *@return    The string representation of this Container
 
1497
         */
 
1498
        public String toString()
 
1499
        {
 
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++)
 
1508
                {
 
1509
                        stringContent.append(getAtom(i).toString()).append(", ");
 
1510
                }
 
1511
                for (int i = 0; i < getBondCount(); i++)
 
1512
                {
 
1513
                        stringContent.append(getBond(i).toString()).append(", ");
 
1514
                }
 
1515
                for (int i = 0; i < getLonePairCount(); i++)
 
1516
                {
 
1517
                        stringContent.append(getLonePair(i).toString()).append(", ");
 
1518
                }
 
1519
                for (int i = 0; i < getSingleElectronCount(); i++)
 
1520
                {
 
1521
                        stringContent.append(getSingleElectron(i).toString()).append(", ");
 
1522
                }
 
1523
                
 
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(", ");
 
1529
                }
 
1530
                stringContent.append("])");
 
1531
                return stringContent.toString();
 
1532
        }
 
1533
 
 
1534
 
 
1535
        /**
 
1536
         * Clones this AtomContainer object and its content.
 
1537
         *
 
1538
         * @return    The cloned object
 
1539
         * @see       #shallowCopy
 
1540
         */
 
1541
        public Object clone() throws CloneNotSupportedException {
 
1542
                IAtom[] newAtoms;
 
1543
                IAtomContainer clone = (IAtomContainer) super.clone();
 
1544
        // start from scratch
 
1545
                clone.removeAllElements();
 
1546
        // clone all atoms
 
1547
                for (int f = 0; f < getAtomCount(); f++) {
 
1548
                        clone.addAtom((Atom) getAtom(f).clone());
 
1549
                }
 
1550
        // clone bonds
 
1551
                IBond bond;
 
1552
                IBond newBond;
 
1553
                for (int i = 0; i < getBondCount(); ++i) {
 
1554
                        bond = getBond(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)));
 
1559
                        }
 
1560
                        newBond.setAtoms(newAtoms);
 
1561
                        clone.addBond(newBond);
 
1562
                }
 
1563
                ILonePair lp;
 
1564
                ILonePair newLp;
 
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);
 
1570
                }
 
1571
                ISingleElectron se;
 
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);
 
1578
                }
 
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)));
 
1588
//                              }
 
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)));
 
1598
//                      } else {
 
1599
//                              //logger.debug("Expecting EC, got: " + electronContainer.getClass().getName());
 
1600
//                              newEC = (IElectronContainer) electronContainer.clone();
 
1601
//                      }
 
1602
//                      clone.addElectronContainer(newEC);
 
1603
//              }
 
1604
                return clone;
 
1605
        }
 
1606
 
 
1607
        /**
 
1608
         *  Grows the ElectronContainer array by a given size.
 
1609
         *
 
1610
         *@see    #growArraySize
 
1611
         */
 
1612
//      protected void growElectronContainerArray()
 
1613
//      {
 
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;
 
1618
//      }
 
1619
 
 
1620
 
 
1621
        /**
 
1622
         *  Grows the atom array by a given size.
 
1623
         *
 
1624
         *@see    #growArraySize
 
1625
         */
 
1626
        protected void growAtomArray()
 
1627
        {
 
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);
 
1631
                atoms = newatoms;
 
1632
        }
 
1633
        
 
1634
        /**
 
1635
         *  Grows the bond array by a given size.
 
1636
         *
 
1637
         *@see    #growArraySize
 
1638
         */
 
1639
        protected void growBondArray()
 
1640
        {
 
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);
 
1644
                bonds = newBonds;
 
1645
        }
 
1646
        
 
1647
        /**
 
1648
         *  Grows the lone pair array by a given size.
 
1649
         *
 
1650
         *@see    #growArraySize
 
1651
         */
 
1652
        protected void growLonePairArray()
 
1653
        {
 
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;
 
1658
        }
 
1659
        
 
1660
        /**
 
1661
         *  Grows the single electron array by a given size.
 
1662
         *
 
1663
         *@see    #growArraySize
 
1664
         */
 
1665
        protected void growSingleElectronArray()
 
1666
        {
 
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;
 
1671
        }
 
1672
        
 
1673
         /**
 
1674
         *  Called by objects to which this object has
 
1675
         *  registered as a listener.
 
1676
         *
 
1677
         *@param  event  A change event pointing to the source of the change
 
1678
         */
 
1679
        public void stateChanged(IChemObjectChangeEvent event)
 
1680
        {
 
1681
                notifyChanged(event);
 
1682
        }   
 
1683
 
 
1684
}
 
1685
 
 
1686