Skip to content

Commit d20a316

Browse files
johnmayegonw
authored andcommitted
Replace ImmutableMap usage, JDK 9+ has Map.of() which is a direct replacement. However usage is minimal so we can just do it the verbose way.
1 parent d0afb59 commit d20a316

File tree

4 files changed

+103
-44
lines changed

4 files changed

+103
-44
lines changed

base/isomorphism/src/main/java/org/openscience/cdk/isomorphism/Mappings.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package org.openscience.cdk.isomorphism;
2626

27-
import com.google.common.collect.ImmutableMap;
2827
import org.openscience.cdk.graph.GraphUtil;
2928
import org.openscience.cdk.interfaces.IAtom;
3029
import org.openscience.cdk.interfaces.IAtomContainer;
@@ -33,7 +32,10 @@
3332
import org.openscience.cdk.isomorphism.matchers.IQueryAtomContainer;
3433

3534
import java.util.Arrays;
35+
import java.util.Collections;
36+
import java.util.HashMap;
3637
import java.util.Iterator;
38+
import java.util.LinkedHashMap;
3739
import java.util.Map;
3840
import java.util.function.Function;
3941
import java.util.function.Predicate;
@@ -578,10 +580,10 @@ private ToAtomMap(IAtomContainer query, IAtomContainer target) {
578580
/**{@inheritDoc} */
579581
@Override
580582
public Map<IAtom, IAtom> apply(int[] mapping) {
581-
ImmutableMap.Builder<IAtom, IAtom> map = ImmutableMap.builder();
583+
Map<IAtom, IAtom> map = new HashMap<>();
582584
for (int i = 0; i < mapping.length; i++)
583585
map.put(query.getAtom(i), target.getAtom(mapping[i]));
584-
return map.build();
586+
return Collections.unmodifiableMap(map);
585587
}
586588
}
587589

@@ -610,15 +612,15 @@ private ToBondMap(IAtomContainer query, IAtomContainer target) {
610612
/**{@inheritDoc} */
611613
@Override
612614
public Map<IBond, IBond> apply(int[] mapping) {
613-
ImmutableMap.Builder<IBond, IBond> map = ImmutableMap.builder();
615+
Map<IBond, IBond> map = new LinkedHashMap<>();
614616
for (int u = 0; u < g1.length; u++) {
615617
for (int v : g1[u]) {
616618
if (v > u) {
617619
map.put(bonds1.get(u, v), bonds2.get(mapping[u], mapping[v]));
618620
}
619621
}
620622
}
621-
return map.build();
623+
return Collections.unmodifiableMap(map);
622624
}
623625
}
624626

@@ -647,7 +649,7 @@ private ToAtomBondMap(IAtomContainer query, IAtomContainer target) {
647649
/**{@inheritDoc} */
648650
@Override
649651
public Map<IChemObject, IChemObject> apply(int[] mapping) {
650-
ImmutableMap.Builder<IChemObject, IChemObject> map = ImmutableMap.builder();
652+
Map<IChemObject, IChemObject> map = new LinkedHashMap<>();
651653
for (int u = 0; u < g1.length; u++) {
652654
map.put(query.getAtom(u), target.getAtom(mapping[u]));
653655
for (int v : g1[u]) {
@@ -656,7 +658,7 @@ public Map<IChemObject, IChemObject> apply(int[] mapping) {
656658
}
657659
}
658660
}
659-
return map.build();
661+
return Collections.unmodifiableMap(map);
660662
}
661663
}
662664
}

base/standard/src/main/java/org/openscience/cdk/aromaticity/AtomTypeModel.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
package org.openscience.cdk.aromaticity;
2626

27-
import com.google.common.collect.ImmutableMap;
2827
import org.openscience.cdk.CDKConstants;
2928
import org.openscience.cdk.config.AtomTypeFactory;
3029
import org.openscience.cdk.exception.NoSuchAtomTypeException;
@@ -35,6 +34,7 @@
3534
import org.openscience.cdk.ringsearch.RingSearch;
3635

3736
import java.util.Arrays;
37+
import java.util.Collections;
3838
import java.util.HashMap;
3939
import java.util.Map;
4040
import java.util.Objects;
@@ -55,12 +55,23 @@
5555
// mores tests in - org.openscience.cdk.aromaticity.ExocyclicAtomTypeModelTest
5656
final class AtomTypeModel extends ElectronDonation {
5757

58+
// JDK 9+ has Map.of() which is more concise
59+
private static Map<String, Integer> typeToElectronContribMap() {
60+
Map<String,Integer> map = new HashMap<>();
61+
map.put("N.planar3", 2);
62+
map.put("N.minus.planar3", 2);
63+
map.put("N.amide", 2);
64+
map.put("S.2", 2);
65+
map.put("S.planar3", 2);
66+
map.put("C.minus.planar", 2);
67+
map.put("O.planar3", 2);
68+
map.put("N.sp2.3", 1);
69+
map.put("C.sp2", 1);
70+
return Collections.unmodifiableMap(map);
71+
}
72+
5873
/** Predefined electron contribution for several atom types. */
59-
private final static Map<String, Integer> TYPES = ImmutableMap.<String, Integer> builder().put("N.planar3", 2)
60-
.put("N.minus.planar3", 2).put("N.amide", 2).put("S.2", 2)
61-
.put("S.planar3", 2).put("C.minus.planar", 2)
62-
.put("O.planar3", 2).put("N.sp2.3", 1).put("C.sp2", 1)
63-
.build();
74+
private final static Map<String, Integer> TYPES = typeToElectronContribMap();
6475

6576
/** Allow exocyclic pi bonds. */
6677
private final boolean exocyclic;

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import java.io.InputStreamReader;
2929
import java.io.Reader;
3030
import java.io.StringReader;
31+
import java.util.Collections;
32+
import java.util.HashMap;
3133
import java.util.Map;
3234
import java.util.StringTokenizer;
3335

3436
import javax.vecmath.Point3d;
3537

36-
import com.google.common.collect.ImmutableMap;
3738
import org.openscience.cdk.CDKConstants;
3839
import org.openscience.cdk.config.AtomTypeFactory;
3940
import org.openscience.cdk.config.Elements;
@@ -74,21 +75,29 @@ public class Mol2Reader extends DefaultChemObjectReader {
7475
private static ILoggingTool logger = LoggingToolFactory
7576
.createLoggingTool(Mol2Reader.class);
7677

78+
// helper function for immutable map of String -> String, JDK 9+ has Map.of()
79+
private static Map<String,String> immutableMap(String ... strs) {
80+
if ((strs.length & 0x1) != 0)
81+
throw new IllegalArgumentException();
82+
Map<String,String> map = new HashMap<>(2*strs.length);
83+
for (int i=0; i<strs.length; i+=2)
84+
map.put(strs[i],strs[i+1]);
85+
return Collections.unmodifiableMap(map);
86+
}
87+
7788
/**
7889
* Dictionary of known atom type aliases. If the key is seen on input, it
7990
* is repleaced with the specified value. Bugs /openbabel/bug/214 and /cdk/bug/1346
8091
*/
81-
private static final Map<String, String> ATOM_TYPE_ALIASES = ImmutableMap
82-
.<String, String> builder()
83-
// previously produced by Open Babel
84-
.put("S.o2", "S.O2")
85-
.put("S.o", "S.O")
86-
// seen in MMFF94 validation suite
87-
.put("CL", "Cl").put("CU", "Cu")
88-
.put("FE", "Fe").put("BR", "Br")
89-
.put("NA", "Na").put("SI", "Si")
90-
.put("CA", "Ca").put("ZN", "Zn")
91-
.put("LI", "Li").put("MG", "Mg").build();
92+
private static final Map<String, String> ATOM_TYPE_ALIASES = immutableMap("S.o2", "S.O2", // previously produced by Open Babel
93+
"S.o", "S.O",
94+
// seen in MMFF94 validation suite
95+
"CL", "Cl",
96+
"CU", "Cu",
97+
"FE", "Fe", "BR", "Br",
98+
"NA", "Na", "SI", "Si",
99+
"CA", "Ca", "ZN", "Zn",
100+
"LI", "Li", "MG", "Mg");
92101

93102
/**
94103
* Constructs a new MDLReader that can read Molecule from a given Reader.

tool/forcefield/src/main/java/org/openscience/cdk/forcefield/mmff/MmffAromaticTypeMapping.java

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424

2525
package org.openscience.cdk.forcefield.mmff;
2626

27-
import com.google.common.collect.ImmutableMap;
2827
import org.openscience.cdk.exception.Intractable;
2928
import org.openscience.cdk.graph.Cycles;
3029
import org.openscience.cdk.interfaces.IAtomContainer;
3130
import org.openscience.cdk.interfaces.IBond;
3231

3332
import java.util.ArrayList;
3433
import java.util.Arrays;
34+
import java.util.Collections;
35+
import java.util.HashMap;
3536
import java.util.List;
3637
import java.util.Map;
3738
import java.util.Set;
@@ -427,36 +428,72 @@ private static void setupContributionAndDoubleBonds(IAtomContainer molecule, Edg
427428
}
428429
}
429430

431+
// helper function for immutable map of String -> String, JDK 9+ has Map.of()
432+
private static Map<String,String> immutableMap(String ... strs) {
433+
if ((strs.length & 0x1) != 0)
434+
throw new IllegalArgumentException();
435+
Map<String,String> map = new HashMap<>(2*strs.length);
436+
for (int i=0; i<strs.length; i+=2)
437+
map.put(strs[i],strs[i+1]);
438+
return Collections.unmodifiableMap(map);
439+
}
440+
430441
/**
431442
* Mapping of preliminary atom MMFF symbolic types to aromatic types for atoms that contribute a
432443
* lone pair.
433444
*/
434-
private final Map<String, String> hetroTypes = ImmutableMap.<String, String>builder().put("S", STHI)
435-
.put("-O-", OFUR).put("OC=C", OFUR).put("OC=N", OFUR)
436-
.put(NCN_PLUS, NIM_PLUS).put(NGD_PLUS, NIM_PLUS)
437-
.put("NM", N5M).put("NC=C", NPYL).put("NC=N", NPYL).put("NN=N", NPYL)
438-
.put("NC=O", NPYL).put("NC=S", NPYL).put("NSO2", NPYL)
439-
.put("NR", NPYL).build();
445+
private final Map<String, String> hetroTypes = immutableMap("S", STHI,
446+
"-O-", OFUR,
447+
"OC=C", OFUR,
448+
"OC=N", OFUR,
449+
NCN_PLUS, NIM_PLUS,
450+
NGD_PLUS, NIM_PLUS,
451+
"NM", N5M,
452+
"NC=C", NPYL,
453+
"NC=N", NPYL,
454+
"NN=N", NPYL,
455+
"NC=O", NPYL,
456+
"NC=S", NPYL,
457+
"NSO2", NPYL,
458+
"NR", NPYL);
440459
/**
441460
* Mapping of preliminary atom MMFF symbolic types to aromatic types for atoms that contribute
442461
* one electron and are alpha to an atom that contributes a lone pair.
443462
*/
444-
private final Map<String, String> alphaTypes = ImmutableMap.<String, String> builder().put("CNN+", CIM_PLUS)
445-
.put("CGD+", CIM_PLUS).put("C=C", C5A).put("C=N", C5A)
446-
.put("CGD", C5A).put("CB", C5A).put(C5B, C5).put("N2OX", N5AX)
447-
.put(NCN_PLUS, NIM_PLUS).put(NGD_PLUS, NIM_PLUS)
448-
.put("N+=C", N5A_PLUS).put("N+=N", N5A_PLUS)
449-
.put("NPD+", N5A_PLUS).put("N=C", N5A).put("N=N", N5A).build();
463+
private final Map<String, String> alphaTypes = immutableMap("CNN+", CIM_PLUS,
464+
"CGD+", CIM_PLUS,
465+
"C=C", C5A,
466+
"C=N", C5A,
467+
"CGD", C5A,
468+
"CB", C5A,
469+
C5B, C5,
470+
"N2OX", N5AX,
471+
NCN_PLUS, NIM_PLUS,
472+
NGD_PLUS, NIM_PLUS,
473+
"N+=C", N5A_PLUS,
474+
"N+=N", N5A_PLUS,
475+
"NPD+", N5A_PLUS,
476+
"N=C", N5A,
477+
"N=N", N5A);
450478
/**
451479
* Mapping of preliminary atom MMFF symbolic types to aromatic types for atoms that contribute
452480
* one electron and are beta to an atom that contributes a lone pair.
453481
*/
454-
private final Map<String, String> betaTypes = ImmutableMap.<String, String> builder().put("CNN+", CIM_PLUS)
455-
.put("CGD+", CIM_PLUS).put("C=C", C5B).put("C=N", C5B)
456-
.put("CGD", C5B).put("CB", C5B).put(C5A, C5).put("N2OX", N5BX)
457-
.put(NCN_PLUS, NIM_PLUS).put(NGD_PLUS, NIM_PLUS)
458-
.put("N+=C", N5B_PLUS).put("N+=N", N5B_PLUS)
459-
.put("NPD+", N5B_PLUS).put("N=C", N5B).put("N=N", N5B).build();
482+
private final Map<String, String> betaTypes = immutableMap("CNN+", CIM_PLUS,
483+
"CGD+", CIM_PLUS,
484+
"C=C", C5B,
485+
"C=N", C5B,
486+
"CGD", C5B,
487+
"CB", C5B,
488+
C5A, C5,
489+
"N2OX", N5BX,
490+
NCN_PLUS, NIM_PLUS,
491+
NGD_PLUS, NIM_PLUS,
492+
"N+=C", N5B_PLUS,
493+
"N+=N", N5B_PLUS,
494+
"NPD+", N5B_PLUS,
495+
"N=C", N5B,
496+
"N=N", N5B);
460497

461498
@SuppressWarnings("PMD.ShortVariable")
462499
// C5 is intended

0 commit comments

Comments
 (0)