Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View DescriptorEngine.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
import java.util.ArrayList;
import java.util.List;
 
public class Atom {
public Atom(String element) {
this.element = element;
}
public String element;
}
 
public class AtomContainer {
public List<Atom> atoms = new ArrayList<Atom>();
public List<Bond> bonds = new ArrayList<Bond>();
}
 
public class AtomPair {
public List<Atom> atoms;
 
}
 
public class Bond {
public Bond(Atom firstAtom, Atom secondAtom) {
this.firstAtom = firstAtom;
this.secondAtom = secondAtom;
}
public Atom firstAtom;
public Atom secondAtom;
 
}
 
public interface IAtomPairDescriptor extends IMoleculePartDescriptor<AtomPair> {}
 
public interface IAtomicDescriptor extends IMoleculePartDescriptor<Atom> {}
 
public interface IBondDescriptor extends IMoleculePartDescriptor<Bond> {}
 
public interface IMoleculePartDescriptor<T> {
public String calculate(T part, AtomContainer atomContainer);
 
}
 
public class BondElementDescriptor implements IBondDescriptor {
 
@Override
public String calculate(Bond part, AtomContainer atomContainer) {
return part.firstAtom.element + ":" + part.secondAtom.element;
}
 
}
 
public class ElementDescriptor implements IAtomicDescriptor {
 
@Override
public String calculate(Atom part, AtomContainer atomContainer) {
return part.element;
}
 
}
 
public class DescriptorEngine {
public void process(AtomContainer mol, IMoleculePartDescriptor descriptor) {
IMoleculePartDescriptor dA = new ElementDescriptor();
IMoleculePartDescriptor dB = new BondElementDescriptor();
if (descriptor.getClass().isInstance(dA)) {
for (Atom atom : mol.atoms) {
System.out.println(descriptor.calculate(atom, mol));
}
}
if (descriptor.getClass().isInstance(dB)) {
for (Bond bond : mol.bonds) {
System.out.println(descriptor.calculate(bond, mol));
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
AtomContainer mol = new AtomContainer();
mol.atoms.add(new Atom("C"));
mol.atoms.add(new Atom("N"));
mol.atoms.add(new Atom("O"));
mol.bonds.add(new Bond(mol.atoms.get(0), mol.atoms.get(1)));
mol.bonds.add(new Bond(mol.atoms.get(1), mol.atoms.get(2)));
DescriptorEngine engine = new DescriptorEngine();
IMoleculePartDescriptor atomDescriptor = new ElementDescriptor();
engine.process(mol, atomDescriptor);
IMoleculePartDescriptor bondDescriptor = new BondElementDescriptor();
engine.process(mol, bondDescriptor);
}
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.