Skip to content

Commit

Permalink
add isEmpty() to classes/interfaces ChemModel, AtomContainerSet, Reac…
Browse files Browse the repository at this point in the history
…tionSet, Crystal and AtomContainer

Signed-off-by: John May <john.wilkinsonmay@gmail.com>
  • Loading branch information
rwst authored and egonw committed Nov 1, 2012
1 parent ee6da32 commit ec21865
Show file tree
Hide file tree
Showing 22 changed files with 211 additions and 28 deletions.
9 changes: 8 additions & 1 deletion src/main/org/openscience/cdk/AtomContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1721,8 +1721,15 @@ private void growSingleElectronArray()
public void stateChanged(IChemObjectChangeEvent event)
{
notifyChanged(event);
}
}

/**
* @inheritDoc
*/
@Override
public boolean isEmpty() {
return atomCount == 0;
}
}


17 changes: 10 additions & 7 deletions src/main/org/openscience/cdk/AtomContainerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,16 @@ public void stateChanged(IChemObjectChangeEvent event) {
}


/**
* Sort the AtomContainers using a provided Comparator
* @param comparator defines the sorting method
*/
public void sortAtomContainers(Comparator<IAtomContainer> comparator) {
Arrays.sort(atomContainers, comparator);
}
/**
* Sort the AtomContainers using a provided Comparator
* @param comparator defines the sorting method
*/
public void sortAtomContainers(Comparator<IAtomContainer> comparator) {
Arrays.sort(atomContainers, comparator);
}

public boolean isEmpty() {
return atomContainerCount == 0;
}
}

16 changes: 16 additions & 0 deletions src/main/org/openscience/cdk/ChemModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,21 @@ public void stateChanged(IChemObjectChangeEvent event)
{
notifyChanged(event);
}

/**
* Returns true if this ChemModel has no atoms.
*/
public boolean isEmpty()
{
if (setOfMolecules != null && !setOfMolecules.isEmpty())
return false;
if (setOfReactions != null && !setOfReactions.isEmpty())
return false;
if (ringSet != null && !ringSet.isEmpty())
return false;
if (crystal != null && !crystal.isEmpty())
return false;
return true;
}
}

1 change: 0 additions & 1 deletion src/main/org/openscience/cdk/Crystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,4 @@ private void setZeroAxes() {
bAxis = new Vector3d(0.0, 0.0, 0.0);
cAxis = new Vector3d(0.0, 0.0, 0.0);
}

}
6 changes: 4 additions & 2 deletions src/main/org/openscience/cdk/ReactionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,14 @@ public void stateChanged(IChemObjectChangeEvent event) {
notifyChanged(event);
}



public void removeReaction(IReaction relevantReaction) {
for (int i = reactionCount-1; i >= 0; i--) {
if (reactions[i] == relevantReaction)
removeReaction(i);
}
}

public boolean isEmpty() {
return reactionCount == 0;
}
}
9 changes: 9 additions & 0 deletions src/main/org/openscience/cdk/debug/DebugAtomContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,13 @@ public IChemObjectBuilder getBuilder() {
return DebugChemObjectBuilder.getInstance();
}

/**
* @inheritDoc
*/
@Override
public boolean isEmpty() {
logger.debug("AtomContainer.isEmpty(): ", atomCount == 0);
return atomCount == 0;
}

}
4 changes: 4 additions & 0 deletions src/main/org/openscience/cdk/debug/DebugAtomContainerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,8 @@ public int getAtomContainerCount() {
return super.getAtomContainerCount();
}

public boolean isEmpty() {
logger.debug("Checking if the atom container set empty: ", atomContainerCount == 0);
return atomContainerCount == 0;
}
}
14 changes: 14 additions & 0 deletions src/main/org/openscience/cdk/debug/DebugChemModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,18 @@ public void setReactionSet(IReactionSet sor) {
super.setReactionSet(sor);
}

public boolean isEmpty()
{
boolean res = true;
if (setOfMolecules != null && !setOfMolecules.isEmpty())
res = false;
if (setOfReactions != null && !setOfReactions.isEmpty())
res = false;
if (ringSet != null && !ringSet.isEmpty())
res = false;
if (crystal != null && !crystal.isEmpty())
res = false;
logger.debug("Checking if chemModel is empty: ", res);
return res;
}
}
4 changes: 4 additions & 0 deletions src/main/org/openscience/cdk/debug/DebugCrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,8 @@ public void stateChanged(IChemObjectChangeEvent event) {
super.stateChanged(event);
}

public boolean isEmpty() {
logger.debug("Checking if crystal is empty: ", super.isEmpty());
return super.isEmpty();
}
}
4 changes: 4 additions & 0 deletions src/main/org/openscience/cdk/debug/DebugReactionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,8 @@ public void removeReaction(IReaction reaction){
super.removeReaction(reaction);
}

public boolean isEmpty() {
logger.debug("Checking if reaction set is empty: ", super.isEmpty());
return super.isEmpty();
}
}
9 changes: 9 additions & 0 deletions src/main/org/openscience/cdk/interfaces/IAtomContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,15 @@ public void addBond(int atom1, int atom2, IBond.Order order,
*/
public boolean contains(IElectronContainer electronContainer);

/**
* Indicates whether this container is empty. The container is considered empty if
* there are no atoms. Bonds are not checked as a graph with no vertexes can not
* have edges.
*
* @return whether the container is empty
*/
public boolean isEmpty();

}


17 changes: 11 additions & 6 deletions src/main/org/openscience/cdk/interfaces/IAtomContainerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,16 @@ public interface IAtomContainerSet extends IChemObject {
*/
public int getAtomContainerCount();

/**
* Sort the AtomContainers using a provided Comparator.
*
* @param comparator defines the sorting method
*/
public void sortAtomContainers( Comparator<IAtomContainer> comparator);
/**
* Sort the AtomContainers using a provided Comparator.
*
* @param comparator defines the sorting method
*/
public void sortAtomContainers( Comparator<IAtomContainer> comparator);

/**
* Returns true if this IAtomContainerSet is empty.
*/
public boolean isEmpty();
}

8 changes: 7 additions & 1 deletion src/main/org/openscience/cdk/interfaces/IChemModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public interface IChemModel extends IChemObject {
* @see #getReactionSet
*/
public void setReactionSet(IReactionSet sor);


/**
* Returns true if this ChemModel is empty.
*
*/
public boolean isEmpty();

}

1 change: 0 additions & 1 deletion src/main/org/openscience/cdk/interfaces/ICrystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,4 @@ public interface ICrystal extends IAtomContainer {
* @see #getZ
*/
public void setZ(Integer value);

}
5 changes: 5 additions & 0 deletions src/main/org/openscience/cdk/interfaces/IReactionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public interface IReactionSet extends IChemObject {
* @param relevantReaction
*/
public void removeReaction(IReaction relevantReaction);

/**
* Returns true if this IReactionSet is empty.
*/
public boolean isEmpty();
}
4 changes: 4 additions & 0 deletions src/main/org/openscience/cdk/interfaces/IRingSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ public interface IRingSet extends IAtomContainerSet {
*/
public boolean contains(IAtomContainer container);

/**
* Returns true if this IRingSet has no atoms.
*/
public boolean isEmpty();
}
8 changes: 8 additions & 0 deletions src/main/org/openscience/cdk/silent/AtomContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,14 @@ private void growSingleElectronArray()
*/
public void stateChanged(IChemObjectChangeEvent event) {}

/**
* @inheritDoc
*/
@Override
public boolean isEmpty() {
return atomCount == 0;
}

}


20 changes: 13 additions & 7 deletions src/main/org/openscience/cdk/silent/AtomContainerSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,19 @@ public Object clone() throws CloneNotSupportedException {
public void stateChanged(IChemObjectChangeEvent event) {}


/**
* Sort the AtomContainers using a provided Comparator
* @param comparator defines the sorting method
*/
public void sortAtomContainers(Comparator<IAtomContainer> comparator) {
Arrays.sort(atomContainers, comparator);
}
/**
* Sort the AtomContainers using a provided Comparator
* @param comparator defines the sorting method
*/
public void sortAtomContainers(Comparator<IAtomContainer> comparator) {
Arrays.sort(atomContainers, comparator);
}

/**
* Returns true if this IAtomContainerSet has no atoms.
*/
public boolean isEmpty() {
return atomContainerCount == 0;
}
}

13 changes: 13 additions & 0 deletions src/main/org/openscience/cdk/silent/ChemModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,18 @@ public Object clone() throws CloneNotSupportedException {
*@param event A change event pointing to the source of the change
*/
public void stateChanged(IChemObjectChangeEvent event) {}

public boolean isEmpty()
{
if (setOfMolecules != null && !setOfMolecules.isEmpty())
return false;
if (setOfReactions != null && !setOfReactions.isEmpty())
return false;
if (ringSet != null && !ringSet.isEmpty())
return false;
if (crystal != null && !crystal.isEmpty())
return false;
return true;
}
}

6 changes: 6 additions & 0 deletions src/main/org/openscience/cdk/silent/Crystal.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,10 @@ private void setZeroAxes() {
cAxis = new Vector3d(0.0, 0.0, 0.0);
}

/**
* Returns true if this Crystal has no atoms.
*/
public boolean isEmpty() {
return aAxis == null;
}
}
9 changes: 7 additions & 2 deletions src/main/org/openscience/cdk/silent/ReactionSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,17 @@ public void stateChanged(IChemObjectChangeEvent event) {
notifyChanged(event);
}



public void removeReaction(IReaction relevantReaction) {
for (int i = reactionCount-1; i >= 0; i--) {
if (reactions[i] == relevantReaction)
removeReaction(i);
}
}

/**
* Returns true if this ReactionSet has no atoms.
*/
public boolean isEmpty() {
return reactionCount == 0;
}
}
55 changes: 55 additions & 0 deletions src/test/org/openscience/cdk/ChemModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openscience.cdk.Molecule;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.interfaces.IReactionSet;
import org.openscience.cdk.interfaces.IRingSet;
import org.openscience.cdk.interfaces.ICrystal;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.AbstractChemModelTest;
import org.openscience.cdk.interfaces.IChemObject;
Expand All @@ -53,6 +61,53 @@ public IChemObject newTestObject() {
@Test public void testChemModel() {
IChemModel chemModel = new ChemModel();
Assert.assertNotNull(chemModel);
Assert.assertTrue(chemModel.isEmpty());

IAtom atom = new Atom("N");
IMolecule mol = new Molecule();
IMoleculeSet mset = new MoleculeSet();
mol.addAtom(atom);
mset.addMolecule(mol);
chemModel.setMoleculeSet(mset);
Assert.assertFalse(chemModel.isEmpty());
mol.removeAtom(atom);
Assert.assertFalse(chemModel.isEmpty());
chemModel.setMoleculeSet(null);
Assert.assertTrue(chemModel.isEmpty());

IChemModel model1 = new ChemModel();
mol.addAtom(atom);
IReaction react = new Reaction();
react.addReactant(mol);
IReactionSet rset = new ReactionSet();
rset.addReaction(react);
model1.setReactionSet(rset);
Assert.assertFalse(model1.isEmpty());
mol.removeAtom(atom);
Assert.assertFalse(model1.isEmpty());
model1.setReactionSet(null);
Assert.assertTrue(model1.isEmpty());

IChemModel model2 = new ChemModel();
mol.addAtom(atom);
IRingSet ringset = new RingSet();
ringset.add(mset);
model2.setRingSet(ringset);
Assert.assertFalse(model2.isEmpty());
mol.removeAtom(atom);
Assert.assertFalse(model2.isEmpty());
model2.setRingSet(null);
Assert.assertTrue(model2.isEmpty());

IChemModel model3 = new ChemModel();
mol.addAtom(atom);
ICrystal cry = new Crystal(mol);
model3.setCrystal(cry);
Assert.assertFalse(model3.isEmpty());
mol.removeAtom(atom);
Assert.assertFalse(model3.isEmpty());
model3.setCrystal(null);
Assert.assertTrue(model3.isEmpty());
}

}

0 comments on commit ec21865

Please sign in to comment.