File: ChemSequence.java

package info (click to toggle)
cdk 1%3A1.2.10-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 16,820 kB
  • sloc: java: 118,076; xml: 23,060; makefile: 64
file content (233 lines) | stat: -rw-r--r-- 6,148 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* $RCSfile$
 * $Author$    
 * $Date$    
 * $Revision$
 *
 * Copyright (C) 1997-2007  Christoph Steinbeck <steinbeck@users.sf.net>
 * 
 * Contact: cdk-devel@lists.sourceforge.net
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 */

package org.openscience.cdk;

import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
import org.openscience.cdk.interfaces.IChemObjectListener;
import org.openscience.cdk.interfaces.IChemSequence;

import java.io.Serializable;
import java.util.Iterator;

/** 
 * A sequence of ChemModels, which can, for example, be used to
 * store the course of a reaction. Each state of the reaction would be
 * stored in one ChemModel.
 *
 * @cdk.module data
 * @cdk.githash
 *
 * @cdk.keyword animation
 * @cdk.keyword reaction
 */
public class ChemSequence extends ChemObject implements Serializable, IChemSequence, IChemObjectListener, Cloneable
{

	/**
     * Determines if a de-serialized object is compatible with this class.
     *
     * This value must only be changed if and only if the new version
     * of this class is incompatible with the old version. See Sun docs
     * for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
     * /serialization/spec/version.doc.html>details</a>.
	 */
	private static final long serialVersionUID = 2199218627455492000L;

	/**
	 *  Array of ChemModels.
	 */
	protected IChemModel[] chemModels;
	
	/**
	 *  Number of ChemModels contained by this container.
	 */
	protected int chemModelCount;
	
	/**
	 *  Amount by which the chemModels array grows when elements are added and
	 *  the array is not large enough for that. 
	 */
	protected int growArraySize = 4;



	/**
	 *  Constructs an empty ChemSequence.
	 */
	public ChemSequence()   
	{
		chemModelCount = 0;
		chemModels = new ChemModel[growArraySize];
	}


	
	/**
	 *  Adds an chemModel to this container.
	 *
	 * @param  chemModel  The chemModel to be added to this container
     *
     * @see            #getChemModel
	 */
	public void addChemModel(IChemModel chemModel)
	{
		if (chemModelCount + 1 >= chemModels.length)
		{
			growChemModelArray();
		}
		chemModels[chemModelCount] = chemModel;
		chemModelCount++;
		chemModel.addListener(this);
		notifyChanged();
	}

	
	/**
	 * Remove a ChemModel from this ChemSequence.
	 *
	 * @param  pos  The position of the ChemModel to be removed.
	 */
	public void removeChemModel(int pos) {
		chemModels[pos].removeListener(this);
		for (int i = pos; i < chemModelCount - 1; i++) {
			chemModels[i] = chemModels[i + 1];
		}
		chemModels[chemModelCount - 1] = null;
		chemModelCount--;
		notifyChanged();
	}

	/**
     * Returns an Iterable to ChemModels in this container.
     *
     * @return    The Iterable to ChemModels in this container
     * @see       #addChemModel
     */
     public Iterable<IChemModel> chemModels() {
    	 return new Iterable<IChemModel>(){
             public Iterator<IChemModel> iterator() {
                 return new ChemModelIterator();
             }
         };
     }

     /**
      * The inner Iterator class.
      *
      */
     private class ChemModelIterator implements Iterator<IChemModel> {

         private int pointer = 0;
     	
         public boolean hasNext() {
             return pointer < chemModelCount;
         }

         public IChemModel next() {
             return chemModels[pointer++];
         }

         public void remove() {
             removeChemModel(--pointer);
         }
     	
     }
     
     
	/**
	 *
	 * Returns the ChemModel at position <code>number</code> in the
	 * container.
	 *
	 * @param  number  The position of the ChemModel to be returned.
	 * @return         The ChemModel at position <code>number</code>.
     *
     * @see            #addChemModel
	 */
	public IChemModel getChemModel(int number)
	{
		return chemModels[number];
	}
	
	/**
	 *  Grows the chemModel array by a given size.
	 *
	 * @see    growArraySize
	 */
	protected void growChemModelArray()
	{
		ChemModel[] newchemModels = new ChemModel[chemModels.length + growArraySize];
		System.arraycopy(chemModels, 0, newchemModels, 0, chemModels.length);
		chemModels = newchemModels;
	}
	

	/**
	 * Returns the number of ChemModels in this Container.
	 *
	 * @return    The number of ChemModels in this Container
	 */
	public int getChemModelCount()
	{
		return this.chemModelCount;
	}

	public String toString() {
        StringBuffer buffer = new StringBuffer(32);
        buffer.append("ChemSequence(#M=");
        buffer.append(chemModelCount);
        if (chemModelCount > 0) {
        	buffer.append(", ");
        	for (int i=0; i<chemModelCount; i++) {
        		buffer.append(chemModels[i].toString());
        	}
        }
        buffer.append(')');
        return buffer.toString();
    }
	
	public Object clone() throws CloneNotSupportedException {
		ChemSequence clone = (ChemSequence)super.clone();
        // clone the chemModels
        clone.chemModelCount = getChemModelCount();
		clone.chemModels = new ChemModel[clone.chemModelCount];
		for (int f = 0; f < clone.chemModelCount; f++) {
			clone.chemModels[f] = (ChemModel)((ChemModel)chemModels[f]).clone();
		}
		return clone;
	}
	
	/**
	 *  Called by objects to which this object has
	 *  registered as a listener.
	 *
	 *@param  event  A change event pointing to the source of the change
	 */
	public void stateChanged(IChemObjectChangeEvent event)
	{
		notifyChanged(event);
	}
}