/
MoleculeSet.java
195 lines (177 loc) · 5.72 KB
/
MoleculeSet.java
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
/* $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.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
/**
* A set of molecules, for example those taking part in a reaction.
*
* To retrieve the molecules from the set, there are two options:
*
* <pre>
* Molecule[] mols = setOfMolecules.getMolecules();
* for (int i=0; i < mols.length; i++) {
* Molecule mol = mols[i];
* }
* </pre>
*
* and
*
* <pre>
* for (int i=0; i < setOfMolecules.getMoleculeCount(); i++) {
* Molecule mol = setOfMolecules.getMolecule(i);
* }
* </pre>
*
* @cdk.module data
* @cdk.githash
*
* @cdk.keyword reaction
* @cdk.keyword molecule
*/
public class MoleculeSet extends AtomContainerSet implements IMoleculeSet, 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 = -861287315770869699L;
public MoleculeSet() {}
/**
* Adds an atomContainer to this container.
*
* @param atomContainer The IMolecule to be added to this container
* @throws IllegalArgumentException when the passed IAtomContainer is not
* an IMolecule.
*/
public void addAtomContainer(IAtomContainer atomContainer) {
if (!(atomContainer instanceof IMolecule))
throw new IllegalArgumentException(
"Only IMolecule's can be stored in an IMoleculeSet"
);
super.addAtomContainer(atomContainer);
}
/**
* Adds an atomContainer to this container with the given
* multiplier.
*
* @param atomContainer The atomContainer to be added to this container
* @param multiplier The multiplier of this atomContainer
* @throws IllegalArgumentException when the passed IAtomContainer is not
* an IMolecule.
*/
public void addAtomContainer(IAtomContainer atomContainer, double multiplier) {
if (!(atomContainer instanceof IMolecule))
throw new IllegalArgumentException(
"Only IMolecule's can be stored in an IMoleculeSet"
);
super.addAtomContainer(atomContainer, multiplier);
}
/**
* Adds an molecule to this container.
*
* @param molecule The molecule to be added to this container
*/
public void addMolecule(IMolecule molecule) {
super.addAtomContainer(molecule);
/* notifyChanged() called in super.addAtomContainer() */
}
/**
* Adds all molecules in the MoleculeSet to this container.
*
* @param moleculeSet The MoleculeSet
*/
public void add(IMoleculeSet moleculeSet) {
for (IAtomContainer mol : moleculeSet.molecules()) {
addAtomContainer(mol);
}
}
public void setMolecules(IMolecule[] molecules)
{
if (atomContainerCount > 0) removeAllAtomContainers();
for (IMolecule molecule : molecules) {
addMolecule(molecule);
}
}
/**
* Returns the array of Molecules of this container.
*
* @return The array of Molecules of this container
* @see #setMolecules
*/
public Iterable<IAtomContainer> molecules() {
return super.atomContainers();
}
/**
*
* Returns the Molecule at position <code>number</code> in the
* container.
*
* @param number The position of the Molecule to be returned.
* @return The Molecule at position <code>number</code> .
*/
public IMolecule getMolecule(int number)
{
return (IMolecule)super.getAtomContainer(number);
}
/**
* Returns the number of Molecules in this Container.
*
* @return The number of Molecules in this Container
*/
public int getMoleculeCount() {
return super.getAtomContainerCount();
}
/**
* Clones this MoleculeSet and its content.
*
*@return the cloned object
*/
public Object clone() throws CloneNotSupportedException {
return (MoleculeSet)super.clone();
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("MoleculeSet(");
buffer.append(super.toString());
buffer.append(')');
return buffer.toString();
}
/**
* 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);
}
}