Skip to content

Commit

Permalink
Revert "Updated isotope to have proper equals and hashCode methods"
Browse files Browse the repository at this point in the history
This reverts commit 4945c3f.
  • Loading branch information
egonw committed Oct 18, 2010
1 parent 1128a53 commit 79c0868
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 156 deletions.
92 changes: 5 additions & 87 deletions src/main/org/openscience/cdk/Isotope.java
Expand Up @@ -28,8 +28,6 @@
*/
package org.openscience.cdk;

import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IIsotope;

Expand Down Expand Up @@ -65,7 +63,6 @@
*
* @cdk.keyword isotope
*/
@TestClass("org.openscience.cdk.IsotopeTest")
public class Isotope extends Element implements Serializable, IIsotope, Cloneable
{

Expand Down Expand Up @@ -290,91 +287,12 @@ public boolean compare(Object object) {
if (!super.compare(object)) {
return false;
}
Isotope isotope = (Isotope) object;

double mn1, mn2;
double em1, em2;
double na1, na2;

if (this.massNumber == null) mn1 = -1;
else mn1 = this.massNumber.doubleValue();
if (isotope.massNumber == null) mn2 = -1;
else mn2 = isotope.massNumber.doubleValue();

if (this.exactMass == null) em1 = -1;
else em1 = this.exactMass;
if (isotope.exactMass == null) em2 = -1;
else em2 = isotope.exactMass;

if (this.naturalAbundance == null) na1= -1;
else na1 = this.naturalAbundance;
if (isotope.naturalAbundance == null) na2 = -1;
else na2 = isotope.naturalAbundance;

return symbol.equals(isotope.getSymbol()) &&
mn1 == mn2 &&
em1 == em2 &&
na1 == na2;
Isotope isotope = (Isotope)object;
return massNumber == isotope.massNumber &&
exactMass == isotope.exactMass &&
naturalAbundance == isotope.naturalAbundance;
}

/**
* Tests (state) equality of two isotope objects.
*
* @param object The isotope object to compare to
* @return true if the two objects are equal in terms of symbol, mass number, exact mass
* and natural abundance
*/
@TestMethod("testEquals")
public boolean equals(Object object) {
if (this == object) return true;

if (!(object instanceof Isotope)) {
return false;
}

Isotope isotope = (Isotope) object;

double mn1, mn2;
double em1, em2;
double na1, na2;

if (this.massNumber == null) mn1 = -1;
else mn1 = this.massNumber.doubleValue();
if (isotope.massNumber == null) mn2 = -1;
else mn2 = isotope.massNumber.doubleValue();

if (this.exactMass == null) em1 = -1;
else em1 = this.exactMass;
if (isotope.exactMass == null) em2 = -1;
else em2 = isotope.exactMass;

if (this.naturalAbundance == null) na1= -1;
else na1 = this.naturalAbundance;
if (isotope.naturalAbundance == null) na2 = -1;
else na2 = isotope.naturalAbundance;

return symbol.equals(isotope.getSymbol()) &&
mn1 == mn2 &&
em1 == em2 &&
na1 == na2;
}

private int getDoubleAsLong(double value) {
long longbits = Double.doubleToLongBits(value);
return (int) (longbits ^ (longbits >>> 32));
}

@TestMethod("testHashcode")
public int hashCode() {
int hash = 23;
int prime = 37;
hash = hash * prime + (symbol == null ? 0 : symbol.hashCode());
hash = hash * prime + (exactMass == null ? 0 : getDoubleAsLong(exactMass));
hash = hash * prime + (naturalAbundance == null ? 0 : getDoubleAsLong(naturalAbundance));
hash = hash * prime + (massNumber == null ? 0 : massNumber);
return hash;
}


public Object clone() throws CloneNotSupportedException {
return super.clone();
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/org/openscience/cdk/formula/MolecularFormula.java
Expand Up @@ -27,8 +27,6 @@
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
Expand Down Expand Up @@ -121,16 +119,14 @@ public IMolecularFormula addIsotope(IIsotope isotope) {
@TestMethod("testAddIsotope_IIsotope_int")
public IMolecularFormula addIsotope(IIsotope isotope, int count) {
boolean flag = false;
for (IIsotope thisIsotope : isotopes.keySet()) {
System.out.println("thisIsotope = " + thisIsotope);
if(isTheSame(isotope, thisIsotope)){
for (IIsotope thisIsotope : isotopes()) {
if(isTheSame(thisIsotope, isotope)){
isotopes.put(thisIsotope, isotopes.get(thisIsotope) + count);
flag = true;
break;
}
}
if(!flag){
System.out.println("Saw "+isotope.getSymbol()+" for first time with count = "+count);
isotopes.put(isotope, count);
}

Expand Down Expand Up @@ -395,9 +391,9 @@ protected boolean isTheSame(IIsotope isotopeOne, IIsotope isotopeTwo) {

if(!isotopeOne.getSymbol().equals(isotopeTwo.getSymbol() ))
return false;
if(natAbund1.doubleValue() != natAbund2.doubleValue())
if(natAbund1.doubleValue() != natAbund2)
return false;
return exactMass1.doubleValue() == exactMass2.doubleValue();
return exactMass1.doubleValue() == exactMass2;
}

public IChemObjectBuilder getBuilder() {
Expand Down
Expand Up @@ -25,26 +25,17 @@
package org.openscience.cdk.tools.manipulator;

import java.io.IOException;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.HashMap;

import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.ChemFile;
import org.openscience.cdk.io.MDLReader;
import org.openscience.cdk.tools.CDKHydrogenAdder;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.config.AtomTypeFactory;
import org.openscience.cdk.config.IsotopeFactory;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.exception.InvalidSmilesException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IAtomType;
Expand All @@ -64,35 +55,7 @@
*/
@TestClass("org.openscience.cdk.formula.MolecularFormulaManipulatorTest")
public class MolecularFormulaManipulator {

public static void main(String[] args) throws CDKException, FileNotFoundException {
SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance());
// IAtomContainer mol = sp.parseSmiles("[H]OC3([H])(C([H])(O[H])C([H])(OP(=O)(O[H])O[H])C([H])(OP(=O)(O[H])O[H])C([H])(O[H])C3([H])(OP(=O)(O[H])OC([H])([H])C([H])(OC(=O)C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])[H])C([H])([H])OC(=O)C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])C([H])([H])N([H])C(=O)C([H])([H])C([H])([H])C([H])([H])C([H])([H])C1([H])(C2([H])(N([H])C(=O)N([H])C2([H])(C([H])([H])S1)))))");
// IAtomContainer mol = sp.parseSmiles("[nH]1c(=O)nc2c(c1=O)nc1c(n2C[C@@H]([C@@H]([C@@H](COP(=O)(OP(=O)(OC[C@@H]2[C@H]([C@H]([C@@H](O2)n2c3ncnc(c3nc2)N)O)O)O)O)O)O)O)cc(c(c1)C)C");
IAtomContainer mol = sp.parseSmiles("[C@H]1([C@H]([C@@H]([C@H]([C@@H](O1)O)O)O)O)CO");

// MDLReader reader = new MDLReader(new FileReader("/Users/guhar/Downloads/45002.sdf"));
// ChemFile chemFile = (ChemFile) reader.read(DefaultChemObjectBuilder.getInstance().newInstance(ChemFile.class));
// List<IAtomContainer> mols = ChemFileManipulator.getAllAtomContainers(chemFile);
// mol = mols.get(0);

AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol);
CDKHydrogenAdder ha = CDKHydrogenAdder.getInstance(DefaultChemObjectBuilder.getInstance());
ha.addImplicitHydrogens(mol);
AtomContainerManipulator.convertImplicitToExplicitHydrogens(mol);

int nh = 0;
for (IAtom atom : mol.atoms()) {
if (atom.getSymbol().equals("H")) nh++;
}
System.out.println("nh = " + nh);
IMolecularFormula molecularFormula = MolecularFormulaManipulator.getMolecularFormula(mol);
System.out.println(MolecularFormulaManipulator.getString(molecularFormula));


Iterable<IIsotope> map = molecularFormula.isotopes();
for (IIsotope iso : map) System.out.println(iso.getSymbol());
}

/**
* Checks a set of Nodes for the occurrence of each isotopes
* instance in the molecular formula. In short number of atoms.
Expand Down Expand Up @@ -225,7 +188,6 @@ public static String getString(IMolecularFormula formula, String[] orderElements
List<IIsotope> isotopesList = putInOrder(orderElements, formula);
for (IIsotope isotope : isotopesList) {
int elemCount = getElementCount(formula, isotope);
System.out.println(isotope.getSymbol()+" -> "+elemCount);
if (elemCount == 1 && !setOne)
stringMF = stringMF + isotope.getSymbol();
else
Expand Down
23 changes: 1 addition & 22 deletions src/test/org/openscience/cdk/IsotopeTest.java
Expand Up @@ -23,10 +23,10 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openscience.cdk.interfaces.AbstractIsotopeTest;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IElement;
import org.openscience.cdk.interfaces.IIsotope;
import org.openscience.cdk.interfaces.AbstractIsotopeTest;
import org.openscience.cdk.interfaces.ITestObjectBuilder;

/**
Expand Down Expand Up @@ -80,25 +80,4 @@ public IChemObject newTestObject() {
Assert.assertEquals(80.0, i.getNaturalAbundance(), 0.001);
}

@Test public void testEquals() {
IIsotope i1 = new Isotope(6, "C", 12.001, 80.0);
IIsotope i2 = new Isotope(6, "C", 12.001, 80.0);

Assert.assertTrue(i1.equals(i2));

i1 = new Isotope(6, "C", 12, 12.001, 80.0);
i2 = new Isotope(6, "C", 12.001, 80.0);
Assert.assertFalse(i1.equals(i2));
}

@Test
public void testHashcode() {
IIsotope i1 = new Isotope(6, "C", 12.001, 80.0);
IIsotope i2 = new Isotope(6, "C", 12.001, 80.0);

int hash1 = i1.hashCode();
int hash2 = i2.hashCode();
Assert.assertEquals(hash1, hash2);
}

}

0 comments on commit 79c0868

Please sign in to comment.