Skip to content

Commit b3f0f8d

Browse files
johnmayegonw
authored andcommitted
Remove usage of FluentIterable using the Stream API. Some usages are more verbose but OK and perhaps indicate a List should have been consumed/returned in the first place.
1 parent 314cd4b commit b3f0f8d

File tree

11 files changed

+134
-101
lines changed

11 files changed

+134
-101
lines changed

app/depict/src/main/java/org/openscience/cdk/depict/DepictionGenerator.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.openscience.cdk.depict;
2020

21-
import com.google.common.collect.FluentIterable;
2221
import org.openscience.cdk.CDKConstants;
2322
import org.openscience.cdk.exception.CDKException;
2423
import org.openscience.cdk.geometry.GeometryUtil;
@@ -61,6 +60,7 @@
6160
import java.util.Map;
6261
import java.util.Set;
6362
import java.util.TreeMap;
63+
import java.util.stream.StreamSupport;
6464

6565
/**
6666
* A high-level API for depicting molecules and reactions.
@@ -321,8 +321,8 @@ public Depiction depict(final IAtomContainer mol) throws CDKException {
321321
* @see #depict(Iterable, int, int)
322322
*/
323323
public Depiction depict(Iterable<IAtomContainer> mols) throws CDKException {
324-
int nMols = FluentIterable.from(mols).size();
325-
Dimension grid = Dimensions.determineGrid(nMols);
324+
int count = (int) StreamSupport.stream(mols.spliterator(), false).count();
325+
Dimension grid = Dimensions.determineGrid(count);
326326
return depict(mols, grid.height, grid.width);
327327
}
328328

@@ -358,7 +358,8 @@ public Depiction depict(Iterable<IAtomContainer> mols, int nrow, int ncol) throw
358358
e.getKey().setProperty(StandardGenerator.HIGHLIGHT_COLOR, e.getValue());
359359

360360
// setup the model scale
361-
List<IAtomContainer> molList = FluentIterable.from(mols).toList();
361+
List<IAtomContainer> molList = new ArrayList<>();
362+
mols.forEach(molList::add);
362363
DepictionGenerator copy = this.withParam(BasicSceneGenerator.Scale.class,
363364
caclModelScale(molList));
364365

@@ -603,7 +604,9 @@ private Bounds generatePlusSymbol(double scale, Color fgcol) {
603604
}
604605

605606
private List<IAtomContainer> toList(IAtomContainerSet set) {
606-
return FluentIterable.from(set.atomContainers()).toList();
607+
List<IAtomContainer> mols = new ArrayList<>();
608+
set.atomContainers().forEach(mols::add);
609+
return mols;
607610
}
608611

609612
private IRenderingElement generate(IAtomContainer molecule, RendererModel model, int atomNum) throws CDKException {

base/core/src/main/java/org/openscience/cdk/config/Isotopes.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import java.nio.channels.Channels;
3131
import java.nio.channels.ReadableByteChannel;
3232
import java.util.ArrayList;
33-
import java.util.HashMap;
3433
import java.util.List;
3534

36-
import com.google.common.collect.FluentIterable;
3735
import org.openscience.cdk.interfaces.IAtom;
3836
import org.openscience.cdk.interfaces.IAtomContainer;
3937
import org.openscience.cdk.interfaces.IIsotope;
@@ -132,19 +130,22 @@ public static void clearMajorIsotopes(IAtomContainer mol) {
132130
* @param formula the formula
133131
*/
134132
public static void clearMajorIsotopes(IMolecularFormula formula) {
135-
for (IIsotope iso : FluentIterable.from(formula.isotopes()).toList())
136-
if (isMajor(iso)) {
137-
int count = formula.getIsotopeCount(iso);
138-
formula.removeIsotope(iso);
139-
iso.setMassNumber(null);
140-
// may be immutable
141-
if (iso.getMassNumber() != null) {
142-
iso = formula.getBuilder().newInstance(IIsotope.class, iso.getSymbol());
143-
iso.setAtomicNumber(iso.getAtomicNumber());
144-
}
145-
iso.setExactMass(null);
146-
iso.setNaturalAbundance(null);
147-
formula.addIsotope(iso, count);
133+
List<IIsotope> majorIsotopes = new ArrayList<>();
134+
formula.isotopes().forEach(i -> {
135+
if (isMajor(i)) majorIsotopes.add(i);
136+
});
137+
for (IIsotope iso : majorIsotopes) {
138+
int count = formula.getIsotopeCount(iso);
139+
formula.removeIsotope(iso);
140+
iso.setMassNumber(null);
141+
// may be immutable
142+
if (iso.getMassNumber() != null) {
143+
iso = formula.getBuilder().newInstance(IIsotope.class, iso.getSymbol());
144+
iso.setAtomicNumber(iso.getAtomicNumber());
148145
}
146+
iso.setExactMass(null);
147+
iso.setNaturalAbundance(null);
148+
formula.addIsotope(iso, count);
149+
}
149150
}
150151
}

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

Lines changed: 56 additions & 32 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.FluentIterable;
2827
import com.google.common.collect.ImmutableMap;
2928
import com.google.common.collect.Iterables;
3029
import org.openscience.cdk.graph.GraphUtil;
@@ -39,6 +38,8 @@
3938
import java.util.Map;
4039
import java.util.function.Function;
4140
import java.util.function.Predicate;
41+
import java.util.stream.Collectors;
42+
import java.util.stream.Stream;
4243
import java.util.stream.StreamSupport;
4344

4445
/**
@@ -278,14 +279,7 @@ public Mappings stereochemistry() {
278279
public Mappings uniqueAtoms() {
279280
// we need the unique predicate to be reset for each new iterator -
280281
// otherwise multiple iterations are always filtered (seen before)
281-
return new Mappings(query, target, new Iterable<int[]>() {
282-
283-
@Override
284-
public Iterator<int[]> iterator() {
285-
return StreamSupport.stream(iterable.spliterator(), false)
286-
.filter(new UniqueAtomMatches()).iterator();
287-
}
288-
});
282+
return new Mappings(query, target, () -> stream().filter(new UniqueAtomMatches()).iterator());
289283
}
290284

291285
/**
@@ -299,14 +293,7 @@ public Mappings uniqueBonds() {
299293
// we need the unique predicate to be reset for each new iterator -
300294
// otherwise multiple iterations are always filtered (seen before)
301295
final int[][] g = GraphUtil.toAdjList(query);
302-
return new Mappings(query, target, new Iterable<int[]>() {
303-
304-
@Override
305-
public Iterator<int[]> iterator() {
306-
return StreamSupport.stream(iterable.spliterator(), false)
307-
.filter(new UniqueBondMatches(g)).iterator();
308-
}
309-
});
296+
return new Mappings(query, target, () -> stream().filter(new UniqueBondMatches(g)).iterator());
310297
}
311298

312299
/**
@@ -422,6 +409,18 @@ public Iterable<Map<IChemObject, IChemObject>> toAtomBondMap() {
422409
return map(new ToAtomBondMap(query, target));
423410
}
424411

412+
/**
413+
* Convert the Mappings to a Java 8 {@link java.util.stream.Stream}. The Stream API was written after
414+
* this class and provides much of the functionality (e.g. {@link #map} is {@link
415+
* Stream#map(java.util.function.Function)} etc. Unlike an Iterable, a stream cannot be traversed more
416+
* than once.
417+
*
418+
* @return the stream
419+
*/
420+
public Stream<int[]> stream() {
421+
return StreamSupport.stream(this.spliterator(), false);
422+
}
423+
425424
/**
426425
* Obtain the chem objects (atoms and bonds) that have 'hit' in the target molecule.
427426
*
@@ -433,11 +432,46 @@ public Iterable<Map<IChemObject, IChemObject>> toAtomBondMap() {
433432
* }
434433
* </pre></blockquote>
435434
*
436-
* @return lazy iterable of chem objects
435+
* @return non-lazy iterable of chem objects
437436
*/
438437
public Iterable<IChemObject> toChemObjects() {
439-
return FluentIterable.from(map(new ToAtomBondMap(query, target)))
440-
.transformAndConcat(map -> map.values());
438+
return stream()
439+
.map(new ToAtomBondMap(query, target))
440+
.flatMap(map -> map.values().stream())
441+
.collect(Collectors.toList());
442+
}
443+
444+
/**
445+
* Obtain the mapped substructures (atoms/bonds) of the target compound. The atoms
446+
* and bonds are the same as in the target molecule but there may be less of them.
447+
*
448+
* <blockquote><pre>
449+
* IAtomContainer query, target
450+
* Mappings mappings = ...;
451+
* for (IAtomContainer mol : mol.toSubstructures()) {
452+
* for (IAtom atom : mol.atoms())
453+
* target.contains(atom); // always true
454+
* for (IAtom atom : target.atoms())
455+
* mol.contains(atom): // not always true
456+
* }
457+
* </pre></blockquote>
458+
*
459+
* @return lazy stream iterable of molecules
460+
*/
461+
public Stream<IAtomContainer> toSubstructuresStream() {
462+
return stream()
463+
.map(new ToAtomBondMap(query, target))
464+
.map(map -> {
465+
final IAtomContainer submol = target.getBuilder()
466+
.newInstance(IAtomContainer.class,
467+
query.getAtomCount(), target.getBondCount(), 0, 0);
468+
for (IAtom atom : query.atoms())
469+
submol.addAtom((IAtom)map.get(atom));
470+
for (IBond bond : query.bonds())
471+
submol.addBond((IBond)map.get(bond));
472+
return submol;
473+
});
474+
441475
}
442476

443477
/**
@@ -455,20 +489,10 @@ public Iterable<IChemObject> toChemObjects() {
455489
* }
456490
* </pre></blockquote>
457491
*
458-
* @return lazy iterable of molecules
492+
* @return non-lazy iterable of molecules
459493
*/
460494
public Iterable<IAtomContainer> toSubstructures() {
461-
return FluentIterable.from(map(new ToAtomBondMap(query, target)))
462-
.transform(map -> {
463-
final IAtomContainer submol = target.getBuilder()
464-
.newInstance(IAtomContainer.class,
465-
query.getAtomCount(), target.getBondCount(), 0, 0);
466-
for (IAtom atom : query.atoms())
467-
submol.addAtom((IAtom)map.get(atom));
468-
for (IBond bond : query.bonds())
469-
submol.addBond((IBond)map.get(bond));
470-
return submol;
471-
});
495+
return toSubstructuresStream().collect(Collectors.toList());
472496
}
473497

474498
/**

base/isomorphism/src/test/java/org/openscience/cdk/isomorphism/UllmannTest.java

Lines changed: 4 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.FluentIterable;
2827
import org.junit.Test;
2928
import org.openscience.cdk.templates.TestMoleculeFactory;
3029

@@ -44,9 +43,8 @@ public void benzeneSubsearch() throws Exception {
4443
int[] match = Ullmann.findSubstructure(TestMoleculeFactory.makeBenzene()).match(
4544
TestMoleculeFactory.makeNaphthalene());
4645
assertThat(match, is(new int[]{2, 7, 6, 5, 4, 3}));
47-
int count = FluentIterable.from(
48-
Ullmann.findSubstructure(TestMoleculeFactory.makeBenzene()).matchAll(
49-
TestMoleculeFactory.makeNaphthalene())).size();
46+
int count = Ullmann.findSubstructure(TestMoleculeFactory.makeBenzene())
47+
.matchAll(TestMoleculeFactory.makeNaphthalene()).count();
5048
assertThat(count, is(6)); // note: aromatic one would be 24
5149
}
5250

@@ -55,9 +53,8 @@ public void napthaleneSubsearch() throws Exception {
5553
int[] match = Ullmann.findSubstructure(TestMoleculeFactory.makeNaphthalene()).match(
5654
TestMoleculeFactory.makeBenzene());
5755
assertThat(match, is(new int[0]));
58-
int count = FluentIterable.from(
59-
Ullmann.findSubstructure(TestMoleculeFactory.makeNaphthalene()).matchAll(
60-
TestMoleculeFactory.makeBenzene())).size();
56+
int count = Ullmann.findSubstructure(TestMoleculeFactory.makeNaphthalene())
57+
.matchAll(TestMoleculeFactory.makeBenzene()).count();
6158
assertThat(count, is(0));
6259
}
6360
}

base/isomorphism/src/test/java/org/openscience/cdk/isomorphism/VentoFoggiaTest.java

Lines changed: 11 additions & 13 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.FluentIterable;
2827
import org.junit.Test;
2928
import org.openscience.cdk.templates.TestMoleculeFactory;
3029

@@ -44,9 +43,9 @@ public void benzeneIdentical() throws Exception {
4443
int[] match = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).match(
4544
TestMoleculeFactory.makeBenzene());
4645
assertThat(match, is(new int[]{0, 1, 2, 3, 4, 5}));
47-
int count = FluentIterable.from(
48-
VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene())
49-
.matchAll(TestMoleculeFactory.makeBenzene())).size();
46+
int count = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene())
47+
.matchAll(TestMoleculeFactory.makeBenzene())
48+
.count();
5049
assertThat(count, is(6)); // note: aromatic one would be 12
5150
}
5251

@@ -55,9 +54,9 @@ public void benzeneNonIdentical() throws Exception {
5554
int[] match = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).match(
5655
TestMoleculeFactory.makeNaphthalene());
5756
assertThat(match, is(new int[0]));
58-
int count = FluentIterable.from(
59-
VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene()).matchAll(
60-
TestMoleculeFactory.makeNaphthalene())).size();
57+
int count = VentoFoggia.findIdentical(TestMoleculeFactory.makeBenzene())
58+
.matchAll(TestMoleculeFactory.makeNaphthalene())
59+
.count();
6160
assertThat(count, is(0));
6261
}
6362

@@ -66,9 +65,9 @@ public void benzeneSubsearch() throws Exception {
6665
int[] match = VentoFoggia.findSubstructure(TestMoleculeFactory.makeBenzene()).match(
6766
TestMoleculeFactory.makeNaphthalene());
6867
assertThat(match, is(new int[]{2, 7, 6, 5, 4, 3}));
69-
int count = FluentIterable.from(
70-
VentoFoggia.findSubstructure(TestMoleculeFactory.makeBenzene()).matchAll(
71-
TestMoleculeFactory.makeNaphthalene())).size();
68+
int count = VentoFoggia.findSubstructure(TestMoleculeFactory.makeBenzene())
69+
.matchAll(TestMoleculeFactory.makeNaphthalene())
70+
.count();
7271
assertThat(count, is(6)); // note: aromatic one would be 24
7372
}
7473

@@ -77,9 +76,8 @@ public void napthaleneSubsearch() throws Exception {
7776
int[] match = VentoFoggia.findSubstructure(TestMoleculeFactory.makeNaphthalene()).match(
7877
TestMoleculeFactory.makeBenzene());
7978
assertThat(match, is(new int[0]));
80-
int count = FluentIterable.from(
81-
VentoFoggia.findSubstructure(TestMoleculeFactory.makeNaphthalene()).matchAll(
82-
TestMoleculeFactory.makeBenzene())).size();
79+
int count = VentoFoggia.findSubstructure(TestMoleculeFactory.makeNaphthalene())
80+
.matchAll(TestMoleculeFactory.makeBenzene()).count();
8381
assertThat(count, is(0));
8482
}
8583
}

legacy/src/main/java/org/openscience/cdk/smiles/smarts/SMARTSQueryTool.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.openscience.cdk.smiles.smarts;
2020

21-
import com.google.common.collect.FluentIterable;
2221
import org.openscience.cdk.aromaticity.Aromaticity;
2322
import org.openscience.cdk.aromaticity.ElectronDonation;
2423
import org.openscience.cdk.exception.CDKException;
@@ -50,6 +49,7 @@
5049
import java.util.TreeSet;
5150
import java.util.stream.Collectors;
5251
import java.util.stream.IntStream;
52+
import java.util.stream.StreamSupport;
5353

5454

5555
/**
@@ -365,10 +365,10 @@ public boolean matches(IAtomContainer atomContainer, boolean forceInitialization
365365
}
366366
}
367367
} else {
368-
mappings = FluentIterable.from(VentoFoggia.findSubstructure(query)
369-
.matchAll(atomContainer)
370-
.filter(new SmartsStereoMatch(query, atomContainer)))
371-
.toList();
368+
mappings = StreamSupport.stream(VentoFoggia.findSubstructure(query)
369+
.matchAll(atomContainer)
370+
.filter(new SmartsStereoMatch(query, atomContainer)).spliterator(), false)
371+
.collect(Collectors.toList());
372372
}
373373

374374
return !mappings.isEmpty();

0 commit comments

Comments
 (0)