Hi Cyrus,

Looks good to me - Iíd need to check the API but you might also be able to use the immutable elements.

MolecularFormulaManipulator.getElementCount(formula, Elements.CARBON);


MolecularFormulaManipulator.getElementCount(formula, Elements.ofString(ďCĒ).toIElement());

Unfortunately weíre stuck with the IElement instance for now and so Elements.Carbon and Elements.CARBON are different classes. The sentence case is preferred for methods though as itís an enum you can do this.

switch(Elements.ofString(atom.getSymbol()) {
    case Nitrogen:       // do something
    case Oxygen:         // do something
    case Sulfur:         // do something


On 29 Jan 2014, at 00:48, Cyrus Harmon <ch-cdk@bobobeach.com> wrote:

Hello cdk-developers,

Iíve been trying to work some of Egonís old code that wants to count elements, but doesnít work with the current CDK API, so Iíve been trying to get element counts for a given symbol. The MolecularFormulaManipulator class has a getElementCount method that takes an IElement:

public static int getElementCount(IMolecularFormula formula, IElement element) {

And the shortest path Iíve found to actually using this is something like:

      MolecularFormulaManipulator.getElementCount(formula, builder.newInstance(IElement.class, builder.newInstance(IIsotope.class,"C")));

Thatís a lot of code. I propose reducing this by adding some new methods to the MolecularFormulaManipulator:

    public static int getElementCount(IMolecularFormula formula, IIsotope isotope) {
           return getElementCount(formula, formula.getBuilder().newInstance(IElement.class, isotope));

       public static int getElementCount(IMolecularFormula formula, String symbol) {
           return getElementCount(formula, formula.getBuilder().newInstance(IIsotope.class, symbol));

Any objections?

And, on a related note, is there a way to short-cut the trip through isotope such that I can make an instance of a class that implements IElement that corresponds to, say, Carbon, without going through the isotope?



WatchGuard Dimension instantly turns raw network data into actionable
security intelligence. It gives you real-time visual feedback on key
security issues and trends.  Skip the complicated setup - simply import
a virtual appliance and go from zero to informed in seconds.
Cdk-devel mailing list