1818 */
1919package org .openscience .cdk .qsar .descriptors .atomic ;
2020
21- import java .io .BufferedReader ;
22- import java .io .IOException ;
23- import java .io .InputStream ;
24- import java .io .InputStreamReader ;
25- import java .util .ArrayList ;
26- import java .util .HashMap ;
27- import java .util .List ;
28- import java .util .StringTokenizer ;
29-
3021import org .openscience .cdk .exception .CDKException ;
3122import org .openscience .cdk .interfaces .IAtom ;
3223import org .openscience .cdk .interfaces .IAtomContainer ;
3526import org .openscience .cdk .qsar .DescriptorValue ;
3627import org .openscience .cdk .qsar .result .DoubleResult ;
3728import org .openscience .cdk .tools .HOSECodeGenerator ;
29+ import org .openscience .cdk .tools .LoggingToolFactory ;
3830import org .openscience .cdk .tools .LonePairElectronChecker ;
3931import org .openscience .cdk .tools .manipulator .AtomContainerManipulator ;
4032
33+ import java .io .BufferedReader ;
34+ import java .io .IOException ;
35+ import java .io .InputStream ;
36+ import java .io .InputStreamReader ;
37+ import java .util .ArrayList ;
38+ import java .util .HashMap ;
39+ import java .util .List ;
40+ import java .util .StringTokenizer ;
41+
4142/**
4243 * This class returns the proton affinity of an atom containing.
4344 *
@@ -117,7 +118,7 @@ public String[] getDescriptorNames() {
117118 */
118119 @ Override
119120 public DescriptorValue calculate (IAtom atom , IAtomContainer container ) {
120- double value = 0 ;
121+ double value ;
121122
122123 try {
123124 int i = container .indexOf (atom );
@@ -130,10 +131,7 @@ public DescriptorValue calculate(IAtom atom, IAtomContainer container) {
130131 AtomContainerManipulator .percieveAtomTypesAndConfigureAtoms (container );
131132 LonePairElectronChecker lpcheck = new LonePairElectronChecker ();
132133 lpcheck .saturate (container );
133- } catch (CDKException e ) {
134- return new DescriptorValue (getSpecification (), getParameterNames (), getParameters (), new DoubleResult (
135- Double .NaN ), NAMES , null );
136- } catch (CloneNotSupportedException e ) {
134+ } catch (CDKException | CloneNotSupportedException e ) {
137135 return new DescriptorValue (getSpecification (), getParameterNames (), getParameters (), new DoubleResult (
138136 Double .NaN ), NAMES , null );
139137 }
@@ -185,8 +183,8 @@ public Object getParameterType(String name) {
185183 */
186184 private class Affinitydb {
187185
188- HashMap < String , HashMap < String , Double >> listGroup = new HashMap < String , HashMap < String , Double >>() ;
189- HashMap < String , HashMap < String , Double >> listGroupS = new HashMap < String , HashMap < String , Double >>() ;
186+ public static final String X_AFFI_PROTON_HOSE_DB = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_AffiProton_HOSE.db" ;
187+ public static final String X_AFFI_PROTON_HOSE_S_DB = "/org/openscience/cdk/qsar/descriptors/atomic/data/X_AffiProton_HOSE_S.db" ;
190188
191189 /**
192190 * The constructor of the IPdb.
@@ -205,26 +203,23 @@ public Affinitydb() {
205203 */
206204 public double extractAffinity (IAtomContainer container , IAtom atom ) {
207205 // loading the files if they are not done
208- String name = "" ;
209- String nameS = "" ;
210- HashMap <String , Double > hoseVSenergy = new HashMap <String , Double >();
211- HashMap <String , Double > hoseVSenergyS = new HashMap <String , Double >();
206+ HashMap <String , Double > hoseVSenergy ;
207+ HashMap <String , Double > hoseVSenergyS ;
212208
213209 if (familyHalogen (atom )) {
214- name = "X_AffiProton_HOSE.db" ;
215- nameS = "X_AffiProton_HOSE_S.db" ;
216- if (listGroup .containsKey (name )) {
217- hoseVSenergy = listGroup .get (name );
218- hoseVSenergyS = listGroupS .get (nameS );
219- } else {
220- String path = "org/openscience/cdk/qsar/descriptors/atomic/data/" + name ;
221- String pathS = "org/openscience/cdk/qsar/descriptors/atomic/data/" + nameS ;
222- InputStream ins = this .getClass ().getClassLoader ().getResourceAsStream (path );
223- BufferedReader insr = new BufferedReader (new InputStreamReader (ins ));
210+ try (InputStream ins = getClass ().getResourceAsStream (X_AFFI_PROTON_HOSE_DB );
211+ BufferedReader insr = new BufferedReader (new InputStreamReader (ins ))) {
224212 hoseVSenergy = extractAttributes (insr );
225- ins = this .getClass ().getClassLoader ().getResourceAsStream (pathS );
226- insr = new BufferedReader (new InputStreamReader (ins ));
213+ } catch (IOException e ) {
214+ LoggingToolFactory .createLoggingTool (getClass ()).error (e );
215+ return 0 ;
216+ }
217+ try (InputStream ins = getClass ().getResourceAsStream (X_AFFI_PROTON_HOSE_S_DB );
218+ BufferedReader insr = new BufferedReader (new InputStreamReader (ins ))) {
227219 hoseVSenergyS = extractAttributes (insr );
220+ } catch (IOException e ) {
221+ LoggingToolFactory .createLoggingTool (getClass ()).error (e );
222+ return 0 ;
228223 }
229224 } else
230225 return 0 ;
@@ -292,7 +287,7 @@ public double extractAffinity(IAtomContainer container, IAtom atom) {
292287 * @return HashMap with the Hose vs energy attributes
293288 */
294289 private HashMap <String , Double > extractAttributes (BufferedReader input ) {
295- HashMap <String , Double > hoseVSenergy = new HashMap <String , Double >();
290+ HashMap <String , Double > hoseVSenergy = new HashMap <>();
296291 String line ;
297292
298293 try {
@@ -316,37 +311,19 @@ private HashMap<String, Double> extractAttributes(BufferedReader input) {
316311 * @return List with String = HOSECode and String = energy
317312 */
318313 private static List <String > extractInfo (String str ) {
319-
320- StringBuffer idEdited = new StringBuffer ();
321- StringBuffer valEdited = new StringBuffer ();
322-
323- int strlen = str .length ();
324-
325- boolean foundSpace = false ;
326- int countSpace = 0 ;
327- boolean foundDigit = false ;
328- for (int i = 0 ; i < strlen ; i ++) {
329- if (!foundDigit ) if (Character .isLetter (str .charAt (i ))) foundDigit = true ;
330-
331- if (foundDigit ) {
332- if (Character .isWhitespace (str .charAt (i ))) {
333- if (countSpace == 0 ) {
334- foundSpace = true ;
335- } else
336- break ;
337- } else {
338- if (foundSpace ) {
339- valEdited .append (str .charAt (i ));
340- } else {
341- idEdited .append (str .charAt (i ));
342- }
343- }
344- }
345- }
346- List <String > objec = new ArrayList <String >();
347- objec .add (idEdited .toString ());
348- objec .add (valEdited .toString ());
349- return objec ;
350-
314+ int beg = 0 ;
315+ int end = 0 ;
316+ int len = str .length ();
317+ List <String > parts = new ArrayList <>();
318+ while (end < len && !Character .isSpaceChar (str .charAt (end )))
319+ end ++;
320+ parts .add (str .substring (beg ,end ));
321+ while (end < len && Character .isSpaceChar (str .charAt (end )))
322+ end ++;
323+ beg = end ;
324+ while (end < len && !Character .isSpaceChar (str .charAt (end )))
325+ end ++;
326+ parts .add (str .substring (beg ,end ));
327+ return parts ;
351328 }
352329}
0 commit comments