Created
February 20, 2012 18:47
-
-
Save gilleain/1870655 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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