001package org.biojava.nbio.structure.io.cif;
002
003import org.biojava.nbio.structure.chem.MetalBondDistance;
004import org.rcsb.cif.model.Category;
005import org.rcsb.cif.model.StrColumn;
006
007import java.util.ArrayList;
008import java.util.HashMap;
009import java.util.List;
010import java.util.Map;
011
012/**
013 * Created by andreas on 6/9/16.
014 */
015public class MetalBondConsumerImpl implements MetalBondConsumer {
016    private final Map<String, List<MetalBondDistance>> definitions = new HashMap<>();
017
018    @Override
019    public void prepare() {
020        definitions.clear();
021    }
022
023    @Override
024    public void finish() {
025        // minimize memory consumption
026        for (List<MetalBondDistance> d : definitions.values()){
027            ((ArrayList<MetalBondDistance>) d).trimToSize();
028        }
029    }
030
031    @Override
032    public void consume(Category category) {
033        StrColumn atomType1 = (StrColumn) category.getColumn("atom_type_1");
034        StrColumn atomType2 = (StrColumn) category.getColumn("atom_type_2");
035        StrColumn lowerLimit = (StrColumn) category.getColumn("lower_limit");
036        StrColumn upperLimit = (StrColumn) category.getColumn("upper_limit");
037        for (int i = 0; i < category.getRowCount(); i++) {
038            MetalBondDistance d = new MetalBondDistance();
039
040            d.setAtomType1(atomType1.get(i));
041            d.setAtomType2(atomType2.get(i));
042            d.setLowerLimit(Float.parseFloat(lowerLimit.get(i)));
043            d.setUpperLimit(Float.parseFloat(upperLimit.get(i)));
044
045            List<MetalBondDistance> defs = definitions.computeIfAbsent(d.getAtomType1(), k -> new ArrayList<>());
046            defs.add(d);
047        }
048    }
049
050    @Override
051    public Map<String, List<MetalBondDistance>> getContainer(){
052        return definitions;
053    }
054}