Skip to content

Commit

Permalink
Replace Guava Predicate/Function with the JDK 1.8 versions. This tech…
Browse files Browse the repository at this point in the history
…nically breaks the API but is very easy to update thanks to the :: operator. We will be revisiting the Mappings.java to use the Streams API
  • Loading branch information
johnmay authored and egonw committed Jan 1, 2022
1 parent 92a03c1 commit 40dabd1
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

package org.openscience.cdk.isomorphism;

import com.google.common.base.Predicate;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.primitives.Ints;
Expand All @@ -39,6 +38,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

/**
* A filter for substructure matches implementing the logic for Atom-Atom Mapping matching. The following
Expand Down Expand Up @@ -141,7 +141,7 @@ private ReactionRole role(IAtom atom) {
* @return whether the match should be accepted
*/
@Override
public boolean apply(int[] perm) {
public boolean test(int[] perm) {
for (MappedPairs mpair : mapped) {

// possibly 'or' of query maps, need to use a set
Expand Down Expand Up @@ -178,6 +178,16 @@ public boolean apply(int[] perm) {
return true;
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

/**
* Helper class list all reactant atom indices (rIdxs) and product
* atom indices (pIdxs) that are in the same Atom-Atom-Mapping class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

package org.openscience.cdk.isomorphism;

import com.google.common.base.Predicate;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.graph.ConnectedComponents;
import org.openscience.cdk.graph.GraphUtil;
import org.openscience.cdk.interfaces.IAtomContainer;

import java.util.Arrays;
import java.util.function.Predicate;

/**
* A predicate for verifying component level grouping in query/target structure
Expand Down Expand Up @@ -135,7 +135,7 @@ public ComponentFilter(int[] grouping, int[] targetComponents) {
* @return the mapping preserves the specified grouping
*/
@Override
public boolean apply(final int[] mapping) {
public boolean test(final int[] mapping) {

// no grouping required
if (queryComponents == null) return true;
Expand Down Expand Up @@ -168,4 +168,14 @@ public boolean apply(final int[] mapping) {

return true;
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package org.openscience.cdk.isomorphism;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand All @@ -40,6 +38,8 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
* A fluent interface for handling (sub)-graph mappings from a query to a target
Expand Down Expand Up @@ -202,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));
return new Mappings(query, target, Iterables.filter(iterable, predicate::test));
}

/**
Expand Down Expand Up @@ -238,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);
return Iterables.transform(iterable, f::apply);
}

/**
Expand Down Expand Up @@ -282,7 +282,7 @@ public Mappings uniqueAtoms() {

@Override
public Iterator<int[]> iterator() {
return Iterators.filter(iterable.iterator(), new UniqueAtomMatches());
return Iterators.filter(iterable.iterator(), new UniqueAtomMatches()::test);
}
});
}
Expand All @@ -302,7 +302,7 @@ public Mappings uniqueBonds() {

@Override
public Iterator<int[]> iterator() {
return Iterators.filter(iterable.iterator(), new UniqueBondMatches(g));
return Iterators.filter(iterable.iterator(), new UniqueBondMatches(g)::test);
}
});
}
Expand Down Expand Up @@ -435,12 +435,7 @@ public Iterable<Map<IChemObject, IChemObject>> toAtomBondMap() {
*/
public Iterable<IChemObject> toChemObjects() {
return FluentIterable.from(map(new ToAtomBondMap(query, target)))
.transformAndConcat(new Function<Map<IChemObject, IChemObject>, Iterable<? extends IChemObject>>() {
@Override
public Iterable<? extends IChemObject> apply(Map<IChemObject, IChemObject> map) {
return map.values();
}
});
.transformAndConcat(map -> map.values());
}

/**
Expand All @@ -462,9 +457,7 @@ public Iterable<? extends IChemObject> apply(Map<IChemObject, IChemObject> map)
*/
public Iterable<IAtomContainer> toSubstructures() {
return FluentIterable.from(map(new ToAtomBondMap(query, target)))
.transform(new Function<Map<IChemObject, IChemObject>, IAtomContainer>() {
@Override
public IAtomContainer apply(Map<IChemObject, IChemObject> map) {
.transform(map -> {
final IAtomContainer submol = target.getBuilder()
.newInstance(IAtomContainer.class,
query.getAtomCount(), target.getBondCount(), 0, 0);
Expand All @@ -473,7 +466,6 @@ public IAtomContainer apply(Map<IChemObject, IChemObject> map) {
for (IBond bond : query.bonds())
submol.addBond((IBond)map.get(bond));
return submol;
}
});
}

Expand Down
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.base.Predicate;
import com.google.common.collect.Maps;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand All @@ -40,6 +39,7 @@

import java.util.Arrays;
import java.util.Map;
import java.util.function.Predicate;

import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation;
import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.TOGETHER;
Expand Down Expand Up @@ -115,7 +115,7 @@ public QueryStereoFilter(IAtomContainer query, IAtomContainer target) {
* @return the stereo chemistry is value
*/
@Override
public boolean apply(final int[] mapping) {
public boolean test(final int[] mapping) {

// reset augment group config if it was initialised
if (groupConfigAdjust != null)
Expand Down Expand Up @@ -432,6 +432,16 @@ private int parity(Conformation conformation) {
return conformation == TOGETHER ? 1 : -1;
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

// could be moved into the IStereoElement to allow faster introspection
private static enum Type {
Tetrahedral, Geometric
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.base.Predicate;
import com.google.common.collect.Maps;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand All @@ -35,6 +34,7 @@

import java.util.Arrays;
import java.util.Map;
import java.util.function.Predicate;

import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation;
import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.TOGETHER;
Expand Down Expand Up @@ -109,7 +109,7 @@ final class StereoMatch implements Predicate<int[]> {
* @return the stereo chemistry is value
*/
@Override
public boolean apply(final int[] mapping) {
public boolean test(final int[] mapping) {

// n.b. not true for unspecified queries e.g. [C@?H](*)(*)*
if (queryStereoIndices.length > targetStereoIndices.length) return false;
Expand All @@ -131,6 +131,16 @@ public boolean apply(final int[] mapping) {
return true;
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

/**
* Verify the tetrahedral stereochemistry (clockwise/anticlockwise) of atom
* {@code u} is preserved in the target when the {@code mapping} is used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

package org.openscience.cdk.isomorphism;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

import java.util.BitSet;
import java.util.Set;
import java.util.function.Predicate;

/**
* A predicate for filtering atom-mapping results. This class is intended for
Expand Down Expand Up @@ -70,10 +70,20 @@ public UniqueAtomMatches() {
*{@inheritDoc}
*/
@Override
public boolean apply(int[] input) {
public boolean test(int[] input) {
return unique.add(toBitSet(input));
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

/**
* Convert a mapping to a bitset.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

package org.openscience.cdk.isomorphism;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

/**
* A predicate for filtering atom-mapping results for those which cover unique
Expand Down Expand Up @@ -70,10 +70,20 @@ public UniqueBondMatches(int[][] g) {

/**{@inheritDoc} */
@Override
public boolean apply(int[] input) {
public boolean test(int[] input) {
return unique.add(toEdgeSet(input));
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

/**
* Convert a mapping to a bitset.
*
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.base.Predicate;
import com.google.common.collect.Maps;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand All @@ -39,6 +38,7 @@

import java.util.Arrays;
import java.util.Map;
import java.util.function.Predicate;

import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation;
import static org.openscience.cdk.interfaces.IDoubleBondStereochemistry.Conformation.TOGETHER;
Expand Down Expand Up @@ -107,7 +107,7 @@ public SmartsStereoMatch(IAtomContainer query, IAtomContainer target) {
* @return the stereo chemistry is value
*/
@Override
public boolean apply(final int[] mapping) {
public boolean test(final int[] mapping) {
for (final int u : queryStereoIndices) {
switch (queryTypes[u]) {
case Tetrahedral:
Expand All @@ -121,6 +121,16 @@ public boolean apply(final int[] mapping) {
return true;
}

/**
* Backwards compatible method from when we used GUAVA predicates.
* @param ints atom index bijection
* @return true/false
* @see #test(int[])
*/
public boolean apply(int[] ints) {
return test(ints);
}

/**
* Verify the tetrahedral stereochemistry (clockwise/anticlockwise) of atom
* {@code u} is preserved in the target when the {@code mapping} is used.
Expand Down

0 comments on commit 40dabd1

Please sign in to comment.