Skip to content

Commit

Permalink
Merge pull request #310 from cdk/patch/factorymalloc
Browse files Browse the repository at this point in the history
Looks good to me.
  • Loading branch information
egonw committed Apr 29, 2017
2 parents baffeee + f4f069c commit 10a0417
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 32 deletions.
Expand Up @@ -206,4 +206,27 @@ public <T extends ICDKObject> T newInstance(Class<T> clazz, Object... params) {
return factory.ofClass(clazz, params);
}

/**
* {@inheritDoc}
*/
@Override
public IAtom newAtom() {
return new Atom();
}

/**
* {@inheritDoc}
*/
@Override
public IBond newBond() {
return new Bond();
}

/**
* {@inheritDoc}
*/
@Override
public IAtomContainer newAtomContainer() {
return new AtomContainer();
}
}
Expand Up @@ -204,4 +204,27 @@ public <T extends ICDKObject> T newInstance(Class<T> clazz, Object... params) {
return factory.ofClass(clazz, params);
}

/**
* {@inheritDoc}
*/
@Override
public IAtom newAtom() {
return new DebugAtom();
}

/**
* {@inheritDoc}
*/
@Override
public IBond newBond() {
return new DebugBond();
}

/**
* {@inheritDoc}
*/
@Override
public IAtomContainer newAtomContainer() {
return new DebugAtomContainer();
}
}
Expand Up @@ -41,6 +41,32 @@ public interface IChemObjectBuilder {
* @throws IllegalArgumentException Exception thrown when the {@link IChemObjectBuilder}
* builder cannot instantiate the <code>clazz</code> with the given parameters.
*/
public <T extends ICDKObject> T newInstance(Class<T> clazz, Object... params) throws IllegalArgumentException;
<T extends ICDKObject> T newInstance(Class<T> clazz, Object... params) throws IllegalArgumentException;

/**
* Create a new atom using the default constructor. This method is considerably faster
* than the dynamic dispatch of {@code newInstance(IAtom.class)} and should be used for
* high throughput applications (e.g. IO).
*
* @return new atom
*/
IAtom newAtom();

/**
* Create a new bond using the default constructor. This method is considerably faster
* than the dynamic dispatch of {@code newInstance(IAtom.class)} and should be used for
* high throughput applications (e.g. IO).
*
* @return new bond
*/
IBond newBond();

/**
* Create a new atom container using the default constructor. This method is considerably faster
* than the dynamic dispatch of {@code newInstance(IAtom.class)} and should be used for
* high throughput applications (e.g. IO).
*
* @return the new atom container
*/
IAtomContainer newAtomContainer();
}
Expand Up @@ -201,4 +201,27 @@ public <T extends ICDKObject> T newInstance(Class<T> clazz, Object... params) th
return factory.ofClass(clazz, params);
}

/**
* {@inheritDoc}
*/
@Override
public IAtom newAtom() {
return new Atom();
}

/**
* {@inheritDoc}
*/
@Override
public IBond newBond() {
return new Bond();
}

/**
* {@inheritDoc}
*/
@Override
public IAtomContainer newAtomContainer() {
return new AtomContainer(0,0,0,0);
}
}
Expand Up @@ -763,7 +763,8 @@ IBond readBondFast(String line, IChemObjectBuilder builder, IAtom[] atoms, int[]
throw new CDKException("invalid line length: " + length + " " + line);
}

IBond bond = builder.newInstance(IBond.class, atoms[u], atoms[v]);
IBond bond = builder.newBond();
bond.setAtoms(new IAtom[]{atoms[u], atoms[v]});

switch (type) {
case 1: // single
Expand Down Expand Up @@ -1279,8 +1280,11 @@ static int length(final String str) {
* @throws CDKException the symbol is not allowed
*/
private IAtom createAtom(String symbol, IChemObjectBuilder builder, int lineNum) throws CDKException {
if (isPeriodicElement(symbol))
return builder.newInstance(IAtom.class, symbol);
if (isPeriodicElement(symbol)) {
IAtom atom = builder.newAtom();
atom.setSymbol(symbol);
return atom;
}
if (symbol.equals("D") && interpretHydrogenIsotopes.isSet()) {
if (mode == Mode.STRICT) throw new CDKException("invalid symbol: " + symbol);
IAtom atom = builder.newInstance(IAtom.class, "H");
Expand Down
Expand Up @@ -156,7 +156,7 @@ public IAtomContainer readConnectionTable(IChemObjectBuilder builder) throws CDK


logger.info("Reading CTAB block");
IAtomContainer readData = builder.newInstance(IAtomContainer.class);
IAtomContainer readData = builder.newAtomContainer();
boolean foundEND = false;
String lastLine = readHeader(readData);
while (isReady() && !foundEND) {
Expand Down Expand Up @@ -256,7 +256,7 @@ public void readAtomBlock(IAtomContainer readData) throws CDKException {
foundEND = true;
} else {
logger.debug("Parsing atom from: " + command);
IAtom atom = readData.getBuilder().newInstance(IAtom.class);
IAtom atom = readData.getBuilder().newAtom();
StringTokenizer tokenizer = new StringTokenizer(command);
// parse the index
try {
Expand All @@ -270,7 +270,8 @@ public void readAtomBlock(IAtomContainer readData) throws CDKException {
// parse the element
String element = tokenizer.nextToken();
if (isotopeFactory.isElement(element)) {
atom = isotopeFactory.configure(readData.getBuilder().newInstance(IAtom.class, element));
atom.setSymbol(element);
isotopeFactory.configure(atom); // ?
} else if ("A".equals(element)) {
atom = readData.getBuilder().newInstance(IPseudoAtom.class, element);
} else if ("Q".equals(element)) {
Expand Down Expand Up @@ -401,7 +402,7 @@ public void readBondBlock(IAtomContainer readData) throws CDKException {
} else {
logger.debug("Parsing bond from: " + command);
StringTokenizer tokenizer = new StringTokenizer(command);
IBond bond = readData.getBuilder().newInstance(IBond.class);
IBond bond = readData.getBuilder().newBond();
// parse the index
try {
String indexString = tokenizer.nextToken();
Expand Down
Expand Up @@ -248,7 +248,7 @@ public boolean hasNext() {
try {
ISimpleChemObjectReader reader = getReader(currentFormat);
reader.setReader(new StringReader(buffer.toString()));
molecule = reader.read(builder.newInstance(IAtomContainer.class, 0, 0, 0, 0));
molecule = reader.read(builder.newAtomContainer());
} catch (Exception exception) {
logger.error("Error while reading next molecule: " + exception.getMessage());
logger.debug(exception);
Expand Down
Expand Up @@ -490,11 +490,7 @@ private IBond.Order toCDKBondOrder(Edge edge) {
* @return a new atom container instance
*/
private IAtomContainer emptyContainer() {
try {
return (IAtomContainer) emptyContainer.clone();
} catch (CloneNotSupportedException e) {
return builder.newInstance(IAtomContainer.class, 0, 0, 0, 0);
}
return builder.newAtomContainer();
}

/**
Expand All @@ -506,15 +502,10 @@ private IAtomContainer emptyContainer() {
* @return new atom with configured symbol and atomic number
*/
private IAtom createAtom(Element element) {
try {
IAtom atom = (IAtom) templateAtom.clone();
atom.setSymbol(element.symbol());
atom.setAtomicNumber(element.atomicNumber());
return atom;
} catch (CloneNotSupportedException e) {
// clone is always supported if overridden but just in case :-)
return builder.newInstance(IAtom.class, element.symbol());
}
IAtom atom = builder.newAtom();
atom.setSymbol(element.symbol());
atom.setAtomicNumber(element.atomicNumber());
return atom;
}

/**
Expand All @@ -529,14 +520,9 @@ private IAtom createAtom(Element element) {
* @return new bond instance
*/
private IBond createBond(IAtom either, IAtom other, IBond.Order order) {
try {
IBond bond = (IBond) templateBond.clone();
bond.setAtoms(new IAtom[]{either, other});
bond.setOrder(order);
return bond;
} catch (CloneNotSupportedException e) {
// clone is always supported if overridden but just in case :-)
return builder.newInstance(IBond.class, either, other, order);
}
IBond bond = builder.newBond();
bond.setAtoms(new IAtom[]{either, other});
bond.setOrder(order);
return bond;
}
}

0 comments on commit 10a0417

Please sign in to comment.