Skip to content

Commit 050f7db

Browse files
committed
Initial cleanup, this class had a lot of warnings.
1 parent ed0759c commit 050f7db

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

storage/io/src/main/java/org/openscience/cdk/io/PMPReader.java

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,6 @@
2323
*/
2424
package org.openscience.cdk.io;
2525

26-
import java.io.BufferedReader;
27-
import java.io.IOException;
28-
import java.io.InputStream;
29-
import java.io.InputStreamReader;
30-
import java.io.Reader;
31-
import java.io.StringReader;
32-
import java.util.Hashtable;
33-
import java.util.Iterator;
34-
import java.util.Map;
35-
import java.util.StringTokenizer;
36-
import java.util.regex.Matcher;
37-
import java.util.regex.Pattern;
38-
39-
import javax.vecmath.Point3d;
40-
import javax.vecmath.Vector3d;
41-
4226
import org.openscience.cdk.exception.CDKException;
4327
import org.openscience.cdk.graph.rebond.RebondTool;
4428
import org.openscience.cdk.interfaces.IAtom;
@@ -47,14 +31,28 @@
4731
import org.openscience.cdk.interfaces.IChemFile;
4832
import org.openscience.cdk.interfaces.IChemModel;
4933
import org.openscience.cdk.interfaces.IChemObject;
34+
import org.openscience.cdk.interfaces.IChemObjectBuilder;
5035
import org.openscience.cdk.interfaces.IChemSequence;
5136
import org.openscience.cdk.interfaces.ICrystal;
52-
import org.openscience.cdk.interfaces.IChemObjectBuilder;
5337
import org.openscience.cdk.io.formats.IResourceFormat;
5438
import org.openscience.cdk.io.formats.PMPFormat;
5539
import org.openscience.cdk.tools.ILoggingTool;
5640
import org.openscience.cdk.tools.LoggingToolFactory;
5741

42+
import javax.vecmath.Point3d;
43+
import javax.vecmath.Vector3d;
44+
import java.io.BufferedReader;
45+
import java.io.IOException;
46+
import java.io.InputStream;
47+
import java.io.InputStreamReader;
48+
import java.io.Reader;
49+
import java.io.StringReader;
50+
import java.util.Hashtable;
51+
import java.util.Map;
52+
import java.util.StringTokenizer;
53+
import java.util.regex.Matcher;
54+
import java.util.regex.Pattern;
55+
5856
/**
5957
* Reads an frames from a PMP formated input.
6058
* Both compilation and use of this class requires Java 1.4.
@@ -81,20 +79,20 @@ public class PMPReader extends DefaultChemObjectReader {
8179
private IAtomContainer modelStructure;
8280
private IChemObject chemObject;
8381
/* Keep an index of PMP id -> AtomCountainer id */
84-
private Map<Integer, Integer> atomids = new Hashtable<Integer, Integer>();
85-
private Map<Integer, Integer> atomGivenIds = new Hashtable<Integer, Integer>();
86-
private Map<Integer, Integer> atomZOrders = new Hashtable<Integer, Integer>();
87-
private Map<Integer, Integer> bondids = new Hashtable<Integer, Integer>();
88-
private Map<Integer, Integer> bondAtomOnes = new Hashtable<Integer, Integer>();
89-
private Map<Integer, Integer> bondAtomTwos = new Hashtable<Integer, Integer>();
90-
private Map<Integer, Double> bondOrders = new Hashtable<Integer, Double>();
82+
private Map<Integer, Integer> atomids = new Hashtable<>();
83+
private Map<Integer, Integer> atomGivenIds = new Hashtable<>();
84+
private Map<Integer, Integer> atomZOrders = new Hashtable<>();
85+
private Map<Integer, Integer> bondids = new Hashtable<>();
86+
private Map<Integer, Integer> bondAtomOnes = new Hashtable<>();
87+
private Map<Integer, Integer> bondAtomTwos = new Hashtable<>();
88+
private Map<Integer, Double> bondOrders = new Hashtable<>();
9189

9290
/* Often used patterns */
9391
Pattern objHeader;
9492
Pattern objCommand;
9593
Pattern atomTypePattern;
9694

97-
int lineNumber = 0;
95+
int lineNumber;
9896
int bondCounter = 0;
9997
private RebondTool rebonder;
10098

@@ -108,7 +106,7 @@ public PMPReader(Reader input) {
108106

109107
/* compile patterns */
110108
objHeader = Pattern.compile(".*\\((\\d+)\\s(\\w+)$");
111-
objCommand = Pattern.compile(".*\\(A\\s(C|F|D|I|O)\\s(\\w+)\\s+\"?(.*?)\"?\\)$");
109+
objCommand = Pattern.compile(".*\\(A\\s([CFDIO])\\s(\\w+)\\s+\"?(.*?)\"?\\)$");
112110
atomTypePattern = Pattern.compile("^(\\d+)\\s+(\\w+)$");
113111

114112
rebonder = new RebondTool(2.0, 0.5, 0.5);
@@ -142,11 +140,12 @@ public void setReader(InputStream input) throws CDKException {
142140
}
143141

144142
@Override
143+
@SuppressWarnings("unchecked")
145144
public boolean accepts(Class<? extends IChemObject> classObject) {
146145
if (IChemFile.class.equals(classObject)) return true;
147146
Class<?>[] interfaces = classObject.getInterfaces();
148-
for (int i = 0; i < interfaces.length; i++) {
149-
if (IChemFile.class.equals(interfaces[i])) return true;
147+
for (Class<?> anInterface : interfaces) {
148+
if (IChemFile.class.equals(anInterface)) return true;
150149
}
151150
Class superClass = classObject.getSuperclass();
152151
if (superClass != null) return this.accepts(superClass);
@@ -162,6 +161,7 @@ public boolean accepts(Class<? extends IChemObject> classObject) {
162161
* @see IChemFile
163162
*/
164163
@Override
164+
@SuppressWarnings("unchecked")
165165
public <T extends IChemObject> T read(T object) throws CDKException {
166166
if (object instanceof IChemFile) {
167167
return (T) readChemFile((IChemFile) object);
@@ -189,9 +189,9 @@ private String readLine() throws IOException {
189189
* @return A ChemFile containing the data parsed from input.
190190
*/
191191
private IChemFile readChemFile(IChemFile chemFile) {
192-
IChemSequence chemSequence = chemFile.getBuilder().newInstance(IChemSequence.class);
193-
IChemModel chemModel = chemFile.getBuilder().newInstance(IChemModel.class);
194-
ICrystal crystal = chemFile.getBuilder().newInstance(ICrystal.class);
192+
IChemSequence chemSequence;
193+
IChemModel chemModel;
194+
ICrystal crystal;
195195

196196
try {
197197
String line = readLine();
@@ -240,11 +240,11 @@ private IChemFile readChemFile(IChemFile chemFile) {
240240
line = readLine();
241241
}
242242
if (chemObject instanceof IAtom) {
243-
atomids.put(Integer.valueOf(id), Integer.valueOf(modelStructure.getAtomCount()));
243+
atomids.put(id, modelStructure.getAtomCount());
244244
atomZOrders.put(Integer.valueOf((String) chemObject.getProperty(PMP_ZORDER)),
245-
Integer.valueOf(id));
245+
id);
246246
atomGivenIds.put(Integer.valueOf((String) chemObject.getProperty(PMP_ID)),
247-
Integer.valueOf(id));
247+
id);
248248
modelStructure.addAtom((IAtom) chemObject);
249249
} else if (chemObject instanceof IBond) {
250250
// ignored: bonds may be defined before their
@@ -257,6 +257,7 @@ private IChemFile readChemFile(IChemFile chemFile) {
257257
}
258258
line = readLine();
259259
}
260+
assert line != null;
260261
if (line.startsWith("%%Model End")) {
261262
// during the Model Start, all bonds are cached as PMP files might
262263
// define bonds *before* the involved atoms :(
@@ -269,17 +270,12 @@ private IChemFile readChemFile(IChemFile chemFile) {
269270
logger.debug("#atom ones: ", bondAtomOnes.size());
270271
logger.debug("#atom twos: ", bondAtomTwos.size());
271272
logger.debug("#orders: ", bondOrders.size());
272-
Iterator<Integer> bonds = bondids.keySet().iterator();
273-
while (bonds.hasNext()) {
274-
Integer index = bonds.next();
275-
double order = (bondOrders.get(index) != null ? ((Double) bondOrders.get(index))
276-
.doubleValue() : 1.0);
273+
for (Integer index : bondids.keySet()) {
274+
double order = (bondOrders.get(index) != null ? bondOrders.get(index) : 1.0);
277275
logger.debug("index: ", index);
278276
logger.debug("ones: ", bondAtomOnes.get(index));
279-
IAtom atom1 = modelStructure.getAtom(((Integer) atomids.get((Integer) bondAtomOnes
280-
.get(index))).intValue());
281-
IAtom atom2 = modelStructure.getAtom(((Integer) atomids.get((Integer) bondAtomTwos
282-
.get(index))).intValue());
277+
IAtom atom1 = modelStructure.getAtom(atomids.get(bondAtomOnes.get(index)));
278+
IAtom atom2 = modelStructure.getAtom(atomids.get(bondAtomTwos.get(index)));
283279
IBond bond = modelStructure.getBuilder().newInstance(IBond.class, atom1, atom2);
284280
if (order == 1.0) {
285281
bond.setOrder(IBond.Order.SINGLE);
@@ -324,7 +320,7 @@ private IChemFile readChemFile(IChemFile chemFile) {
324320
.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
325321
a.setCovalentRadius(0.6);
326322
IAtom modelAtom = modelStructure.getAtom(atomids.get(atomGivenIds
327-
.get(Integer.valueOf(i + 1))));
323+
.get(i + 1)));
328324
a.setSymbol(modelAtom.getSymbol());
329325
clone.addAtom(a);
330326
}
@@ -362,7 +358,6 @@ private IChemFile readChemFile(IChemFile chemFile) {
362358
} else {
363359
crystal.setSpaceGroup("P1");
364360
}
365-
} else {
366361
}
367362
line = readLine();
368363
}
@@ -372,9 +367,8 @@ private IChemFile readChemFile(IChemFile chemFile) {
372367
line = readLine();
373368
}
374369
chemFile.addChemSequence(chemSequence);
375-
} else {
376-
// disregard line
377-
}
370+
} // else disregard line
371+
378372
// read next line
379373
line = readLine();
380374
}
@@ -431,7 +425,7 @@ private void processModelCommand(String object, String command, String format, S
431425
int atomid = Integer.parseInt(field);
432426
// this assumes that the atoms involved in this bond are
433427
// already added, which seems the case in the PMP files
434-
bondAtomOnes.put(Integer.valueOf(bondCounter), Integer.valueOf(atomid));
428+
bondAtomOnes.put(bondCounter, atomid);
435429
// IAtom a = molecule.getAtom(realatomid);
436430
// ((IBond)chemObject).setAtomAt(a, 0);
437431
} else if ("Atom2".equals(command)) {
@@ -440,16 +434,16 @@ private void processModelCommand(String object, String command, String format, S
440434
// already added, which seems the case in the PMP files
441435
logger.debug("atomids: " + atomids);
442436
logger.debug("atomid: " + atomid);
443-
bondAtomTwos.put(Integer.valueOf(bondCounter), Integer.valueOf(atomid));
437+
bondAtomTwos.put(bondCounter, atomid);
444438
// IAtom a = molecule.getAtom(realatomid);
445439
// ((IBond)chemObject).setAtomAt(a, 1);
446440
} else if ("Order".equals(command)) {
447441
double order = Double.parseDouble(field);
448-
bondOrders.put(Integer.valueOf(bondCounter), order);
442+
bondOrders.put(bondCounter, order);
449443
// ((IBond)chemObject).setOrder(order);
450444
} else if ("Id".equals(command)) {
451445
int bondid = Integer.parseInt(field);
452-
bondids.put(Integer.valueOf(bondCounter), Integer.valueOf(bondid));
446+
bondids.put(bondCounter, bondid);
453447
} else if ("Label".equals(command)) {
454448
} else if ("3DGridOrigin".equals(command)) {
455449
} else if ("3DGridMatrix".equals(command)) {
@@ -473,7 +467,7 @@ private void constructObject(IChemObjectBuilder builder, String object) {
473467
} else {
474468
logger.error("Cannot construct PMP object type: " + object);
475469
}
476-
};
470+
}
477471

478472
@Override
479473
public void close() throws IOException {

0 commit comments

Comments
 (0)