~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/ChemFile.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
/*
 
2
 *  $RCSfile$
 
3
 *  $Author: egonw $
 
4
 *  $Date: 2007-02-16 14:03:31 +0100 (Fri, 16 Feb 2007) $
 
5
 *  $Revision: 7958 $
 
6
 *
 
7
 *  Copyright (C) 1997-2007  Christoph Steinbeck <steinbeck@users.sf.net>
 
8
 *
 
9
 *  Contact: cdk-devel@lists.sourceforge.net
 
10
 *
 
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.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 */
 
25
package org.openscience.cdk;
 
26
 
 
27
import java.io.Serializable;
 
28
 
 
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;
 
33
 
 
34
/**
 
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
 
37
 *  document
 
38
 *
 
39
 *@author        steinbeck
 
40
 *@cdk.module    data
 
41
 */
 
42
public class ChemFile extends ChemObject implements Serializable, Cloneable,
 
43
                IChemFile, IChemObjectListener
 
44
{
 
45
 
 
46
        /**
 
47
     * Determines if a de-serialized object is compatible with this class.
 
48
     *
 
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>.
 
53
         */
 
54
        private static final long serialVersionUID = 1926781734333430132L;
 
55
 
 
56
        /**
 
57
         *  Array of ChemSquences.
 
58
         */
 
59
        protected IChemSequence[] chemSequences;
 
60
 
 
61
        /**
 
62
         *  Number of ChemSequences contained by this container.
 
63
         */
 
64
        protected int chemSequenceCount;
 
65
 
 
66
        /**
 
67
         *  Amount by which the chemsequence array grows when elements are added and
 
68
         *  the array is not large enough for that.
 
69
         */
 
70
        protected int growArraySize = 4;
 
71
 
 
72
 
 
73
        /**
 
74
         *  Constructs an empty ChemFile.
 
75
         */
 
76
        public ChemFile()
 
77
        {
 
78
                chemSequenceCount = 0;
 
79
                chemSequences = new ChemSequence[growArraySize];
 
80
        }
 
81
 
 
82
 
 
83
        /**
 
84
         *  Adds a ChemSequence to this container.
 
85
         *
 
86
         *@param  chemSequence  The chemSequence to be added to this container
 
87
         *@see                  #chemSequences
 
88
         */
 
89
        public void addChemSequence(IChemSequence chemSequence)
 
90
        {
 
91
                chemSequence.addListener(this);
 
92
                if (chemSequenceCount + 1 >= chemSequences.length)
 
93
                {
 
94
                        growChemSequenceArray();
 
95
                }
 
96
                chemSequences[chemSequenceCount] = chemSequence;
 
97
                chemSequenceCount++;
 
98
                notifyChanged();
 
99
        }
 
100
 
 
101
        /**
 
102
         *  Removes a ChemSequence from this container.
 
103
         *
 
104
         *@param  chemSequence  The chemSequence to be added to this container
 
105
         *@see                  #chemSequences
 
106
         */
 
107
        public void removeChemSequence(int pos)
 
108
        {
 
109
                chemSequences[pos].removeListener(this);
 
110
                for (int i = pos; i < chemSequenceCount - 1; i++) {
 
111
                        chemSequences[i] = chemSequences[i + 1];
 
112
                }
 
113
                chemSequences[chemSequenceCount - 1] = null;
 
114
                chemSequenceCount--;
 
115
                notifyChanged();
 
116
        }
 
117
 
 
118
        /**
 
119
         *  Returns the Iterator to ChemSequences of this container.
 
120
         *
 
121
         *@return    The Iterator to ChemSequences of this container
 
122
         *@see       #addChemSequence
 
123
         */
 
124
        public java.util.Iterator chemSequences()
 
125
        {
 
126
                return new ChemSequenceIterator();
 
127
        }
 
128
 
 
129
        /**
 
130
     * The inner Iterator class.
 
131
     *
 
132
     */
 
133
    private class ChemSequenceIterator implements java.util.Iterator {
 
134
 
 
135
        private int pointer = 0;
 
136
        
 
137
        public boolean hasNext() {
 
138
            if (pointer < chemSequenceCount) return true;
 
139
            return false;
 
140
        }
 
141
 
 
142
        public Object next() {
 
143
            return chemSequences[pointer++];
 
144
        }
 
145
 
 
146
        public void remove() {
 
147
            removeChemSequence(--pointer);
 
148
        }
 
149
        
 
150
    }
 
151
        
 
152
        /**
 
153
         *  Returns the ChemSequence at position <code>number</code> in the container.
 
154
         *
 
155
         *@param  number  The position of the ChemSequence to be returned.
 
156
         *@return         The ChemSequence at position <code>number</code>.
 
157
         *@see            #addChemSequence
 
158
         */
 
159
        public IChemSequence getChemSequence(int number)
 
160
        {
 
161
                return (ChemSequence)chemSequences[number];
 
162
        }
 
163
 
 
164
 
 
165
        /**
 
166
         *  Grows the ChemSequence array by a given size.
 
167
         *
 
168
         *@see    #growArraySize
 
169
         */
 
170
        protected void growChemSequenceArray()
 
171
        {
 
172
                growArraySize = chemSequences.length;
 
173
                IChemSequence[] newchemSequences = new ChemSequence[chemSequences.length + growArraySize];
 
174
                System.arraycopy(chemSequences, 0, newchemSequences, 0, chemSequences.length);
 
175
                chemSequences = newchemSequences;
 
176
        }
 
177
 
 
178
 
 
179
        /**
 
180
         *  Returns the number of ChemSequences in this Container.
 
181
         *
 
182
         *@return    The number of ChemSequences in this Container
 
183
         */
 
184
        public int getChemSequenceCount()
 
185
        {
 
186
                return this.chemSequenceCount;
 
187
        }
 
188
 
 
189
 
 
190
        /**
 
191
         * Returns a String representation of this class. It implements
 
192
         * RFC #9.
 
193
         *
 
194
         *@return    String representation of the Object
 
195
         */
 
196
        public String toString()
 
197
        {
 
198
                StringBuffer buffer = new StringBuffer();
 
199
                buffer.append("ChemFile(#S=");
 
200
                java.util.Iterator seqs = chemSequences();
 
201
                buffer.append(chemSequenceCount);
 
202
                buffer.append(", ");
 
203
                while (seqs.hasNext())
 
204
                {
 
205
                        IChemSequence sequence = (IChemSequence)seqs.next();
 
206
                        buffer.append(sequence.toString());
 
207
                }
 
208
                buffer.append(')');
 
209
                return buffer.toString();
 
210
        }
 
211
 
 
212
 
 
213
        /**
 
214
         *  Allows for getting an clone of this object.
 
215
         *
 
216
         *@return    a clone of this object
 
217
         */
 
218
        public Object clone() throws CloneNotSupportedException
 
219
        {
 
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++)
 
225
                {
 
226
                        clone.chemSequences[f] = (ChemSequence)((ChemSequence)chemSequences[f]).clone();
 
227
                }
 
228
                return clone;
 
229
        }
 
230
 
 
231
 
 
232
        /**
 
233
         *  Called by objects to which this object has
 
234
         *  registered as a listener.
 
235
         *
 
236
         *@param  event  A change event pointing to the source of the change
 
237
         */
 
238
        public void stateChanged(IChemObjectChangeEvent event)
 
239
        {
 
240
                notifyChanged(event);
 
241
        }
 
242
}
 
243