4
* $Date: 2007-02-16 14:03:31 +0100 (Fri, 16 Feb 2007) $
7
* Copyright (C) 1997-2007 Christoph Steinbeck <steinbeck@users.sf.net>
9
* Contact: cdk-devel@lists.sourceforge.net
11
* This program is free software; you can redistribute it and/or
12
* modify it under the terms of the GNU Lesser General Public License
13
* as published by the Free Software Foundation; either version 2.1
14
* of the License, or (at your option) any later version.
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Lesser General Public License for more details.
21
* You should have received a copy of the GNU Lesser General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25
package org.openscience.cdk;
27
import java.io.Serializable;
29
import org.openscience.cdk.interfaces.IChemFile;
30
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
31
import org.openscience.cdk.interfaces.IChemObjectListener;
32
import org.openscience.cdk.interfaces.IChemSequence;
35
* A Object containing a number of ChemSequences. This is supposed to be the
36
* top level container, which can contain all the concepts stored in a chemical
42
public class ChemFile extends ChemObject implements Serializable, Cloneable,
43
IChemFile, IChemObjectListener
47
* Determines if a de-serialized object is compatible with this class.
49
* This value must only be changed if and only if the new version
50
* of this class is imcompatible with the old version. See Sun docs
51
* for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
52
* /serialization/spec/version.doc.html>details</a>.
54
private static final long serialVersionUID = 1926781734333430132L;
57
* Array of ChemSquences.
59
protected IChemSequence[] chemSequences;
62
* Number of ChemSequences contained by this container.
64
protected int chemSequenceCount;
67
* Amount by which the chemsequence array grows when elements are added and
68
* the array is not large enough for that.
70
protected int growArraySize = 4;
74
* Constructs an empty ChemFile.
78
chemSequenceCount = 0;
79
chemSequences = new ChemSequence[growArraySize];
84
* Adds a ChemSequence to this container.
86
*@param chemSequence The chemSequence to be added to this container
89
public void addChemSequence(IChemSequence chemSequence)
91
chemSequence.addListener(this);
92
if (chemSequenceCount + 1 >= chemSequences.length)
94
growChemSequenceArray();
96
chemSequences[chemSequenceCount] = chemSequence;
102
* Removes a ChemSequence from this container.
104
*@param chemSequence The chemSequence to be added to this container
107
public void removeChemSequence(int pos)
109
chemSequences[pos].removeListener(this);
110
for (int i = pos; i < chemSequenceCount - 1; i++) {
111
chemSequences[i] = chemSequences[i + 1];
113
chemSequences[chemSequenceCount - 1] = null;
119
* Returns the Iterator to ChemSequences of this container.
121
*@return The Iterator to ChemSequences of this container
122
*@see #addChemSequence
124
public java.util.Iterator chemSequences()
126
return new ChemSequenceIterator();
130
* The inner Iterator class.
133
private class ChemSequenceIterator implements java.util.Iterator {
135
private int pointer = 0;
137
public boolean hasNext() {
138
if (pointer < chemSequenceCount) return true;
142
public Object next() {
143
return chemSequences[pointer++];
146
public void remove() {
147
removeChemSequence(--pointer);
153
* Returns the ChemSequence at position <code>number</code> in the container.
155
*@param number The position of the ChemSequence to be returned.
156
*@return The ChemSequence at position <code>number</code>.
157
*@see #addChemSequence
159
public IChemSequence getChemSequence(int number)
161
return (ChemSequence)chemSequences[number];
166
* Grows the ChemSequence array by a given size.
170
protected void growChemSequenceArray()
172
growArraySize = chemSequences.length;
173
IChemSequence[] newchemSequences = new ChemSequence[chemSequences.length + growArraySize];
174
System.arraycopy(chemSequences, 0, newchemSequences, 0, chemSequences.length);
175
chemSequences = newchemSequences;
180
* Returns the number of ChemSequences in this Container.
182
*@return The number of ChemSequences in this Container
184
public int getChemSequenceCount()
186
return this.chemSequenceCount;
191
* Returns a String representation of this class. It implements
194
*@return String representation of the Object
196
public String toString()
198
StringBuffer buffer = new StringBuffer();
199
buffer.append("ChemFile(#S=");
200
java.util.Iterator seqs = chemSequences();
201
buffer.append(chemSequenceCount);
203
while (seqs.hasNext())
205
IChemSequence sequence = (IChemSequence)seqs.next();
206
buffer.append(sequence.toString());
209
return buffer.toString();
214
* Allows for getting an clone of this object.
216
*@return a clone of this object
218
public Object clone() throws CloneNotSupportedException
220
ChemFile clone = (ChemFile) super.clone();
221
// clone the chemModels
222
clone.chemSequenceCount = getChemSequenceCount();
223
clone.chemSequences = new ChemSequence[clone.chemSequenceCount];
224
for (int f = 0; f < clone.chemSequenceCount; f++)
226
clone.chemSequences[f] = (ChemSequence)((ChemSequence)chemSequences[f]).clone();
233
* Called by objects to which this object has
234
* registered as a listener.
236
*@param event A change event pointing to the source of the change
238
public void stateChanged(IChemObjectChangeEvent event)
240
notifyChanged(event);