Skip to content

Commit

Permalink
Replace Iterables and Iterables usage - alot of this can be handled w…
Browse files Browse the repository at this point in the history
…ith Stream's now. Again slightly more verbose, there is one extra case which needs special comment due to mocking.
  • Loading branch information
johnmay authored and egonw committed Jan 1, 2022
1 parent b3f0f8d commit 02b9d6b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
package org.openscience.cdk.isomorphism;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import org.openscience.cdk.graph.GraphUtil;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand Down Expand Up @@ -203,7 +202,7 @@ public final class Mappings implements Iterable<int[]> {
* @return fluent-api reference
*/
public Mappings filter(final Predicate<int[]> predicate) {
return new Mappings(query, target, Iterables.filter(iterable, predicate::test));
return new Mappings(query, target, () -> stream().filter(predicate).iterator());
}

/**
Expand Down Expand Up @@ -239,7 +238,7 @@ public Mappings filter(final Predicate<int[]> predicate) {
* @return iterable of the transformed type
*/
public <T> Iterable<T> map(final Function<int[], T> f) {
return Iterables.transform(iterable, f::apply);
return () -> stream().map(f).iterator();
}

/**
Expand All @@ -250,7 +249,7 @@ public <T> Iterable<T> map(final Function<int[], T> f) {
* @return fluent-api instance
*/
public Mappings limit(int limit) {
return new Mappings(query, target, Iterables.limit(iterable, limit));
return new Mappings(query, target, () -> stream().limit(limit).iterator());
}

/**
Expand Down Expand Up @@ -523,7 +522,7 @@ public boolean atLeast(int n) {
* @return first match
*/
public int[] first() {
return Iterables.getFirst(iterable, new int[0]);
return stream().findFirst().orElseGet(() -> new int[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

package org.openscience.cdk.stereo;

import com.google.common.collect.Iterables;
import org.junit.Test;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand Down Expand Up @@ -627,10 +625,11 @@ public void inverse_style_downbond_dopachrome() throws Exception {
IAtomContainer ac = mdl.read(new AtomContainer());

// MDL reader currently adds stereo automatically
IStereoElement[] ses = Iterables.toArray(ac.stereoElements(), IStereoElement.class);
List<IStereoElement> ses = new ArrayList<>();
ac.stereoElements().forEach(ses::add);

assertThat(ses.length, is(1));
assertNotNull(ses[0]);
assertThat(ses.size(), is(1));
assertNotNull(ses.get(0));
} finally {
if (mdl != null) mdl.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.openscience.cdk.layout;

import com.google.common.collect.Iterables;
import org.openscience.cdk.graph.GraphUtil;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand Down Expand Up @@ -78,7 +77,8 @@ final class CorrectGeometricConfiguration {
* @throws IllegalArgumentException an atom had unset coordinates
*/
public static IAtomContainer correct(IAtomContainer container) {
if (!Iterables.isEmpty(container.stereoElements())) new CorrectGeometricConfiguration(container);
if (container.stereoElements().iterator().hasNext())
new CorrectGeometricConfiguration(container);
return container;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.openscience.cdk.isomorphism;

import com.google.common.collect.Iterables;
import org.junit.Test;
import org.openscience.cdk.graph.GraphUtil;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand Down Expand Up @@ -58,13 +57,13 @@ public void uniqueBonds() throws Exception {
IAtomContainer query = smi("C1CCC1");
IAtomContainer target = smi("C12C3C1C23");

Iterable<int[]> mappings = VentoFoggia.findSubstructure(query).matchAll(target);
Mappings mappings = VentoFoggia.findSubstructure(query).matchAll(target);

// using unique atoms we may think we only found 1 mapping
assertThat(Iterables.size(Iterables.filter(mappings, new UniqueAtomMatches()::test)), is(1));
assertThat(mappings.stream().filter(new UniqueAtomMatches()).count(), is(1L));

// when in fact we found 4 different mappings
assertThat(Iterables.size(Iterables.filter(mappings, new UniqueBondMatches(GraphUtil.toAdjList(query))::test)), is(3));
// when in fact we found 3 different mappings
assertThat(mappings.stream().filter(new UniqueBondMatches(GraphUtil.toAdjList(query))).count(), is(3L));
}

@Test
Expand Down

0 comments on commit 02b9d6b

Please sign in to comment.