001    /*
002     * Copyright (c) 1998-2014 ChemAxon Ltd. All Rights Reserved.
003     *
004     * This software is the confidential and proprietary information of
005     * ChemAxon. You shall not disclose such Confidential Information
006     * and shall use it only in accordance with the terms of the agreements
007     * you entered into with ChemAxon.
008     *
009     */
010    package com.chemaxon.overlap.io;
011    
012    import chemaxon.marvin.io.MPropHandler;
013    import chemaxon.struc.Molecule;
014    import com.google.common.base.Optional;
015    
016    /**
017     * Build storage for molecular properties.
018     *
019     * @author Gabor Imre
020     */
021    public class PropertyCollectorCallback implements MoleculeCallback {
022    
023        /**
024         * Represented property name or null for molecule name.
025         */
026        private final String propname;
027    
028        // private final MasterStorages.MasterStorageBuilder<String> builder;
029        private final CompactStringStorage.Builder builder;
030    
031        /**
032         * Construct builder to represent molecule name.
033         */
034        public PropertyCollectorCallback() {
035            this.propname = null;
036            //this.builder = MasterStorages.createStringStoreBuilder();
037            this.builder = new CompactStringStorage.Builder(100000, 0);
038        }
039    
040        /**
041         * Construct builder to store a given property.
042         *
043         * @param propname Property name to store
044         */
045        public PropertyCollectorCallback(String propname) {
046            this.propname = propname;
047            //this.builder = MasterStorages.createStringStoreBuilder();
048            this.builder = new CompactStringStorage.Builder(100000, 0);
049        }
050    
051        /**
052         * Extract represented property/name from an input molecule.
053         *
054         * @param readno Master ID
055         * @param m Parsed molecule
056         */
057        private void addMolecule(int readno, Molecule m) {
058            final String s;
059            if (this.propname == null) {
060                s = m.getName();
061            } else {
062                s = MPropHandler.convertToString(m.properties(), this.propname);
063            }
064            if ("".equals(s) || s == null) {
065                this.builder.addMissing(readno);
066            } else {
067                this.builder.add(readno, s);
068            }
069        }
070    
071    
072        @Override
073        public void notifyMolecule(int readno, Optional<String> molString, Molecule m, int index) {
074            addMolecule(readno, m);
075        }
076    
077        @Override
078        public void notifyProcessingError(int readno, Optional<String> molString, Molecule m, Throwable t) {
079            // Molecule is parsed successfully
080            // this.builder.addMissing(readno);
081            addMolecule(readno, m);
082        }
083    
084        @Override
085        public void notifyParseError(int readno, Optional<String> molString, Throwable t) {
086            this.builder.addMissing(readno);
087        }
088    
089        /**
090         * Create the master storage from the current state.
091         *
092         * @return Master storage from current state
093         */
094        public CompactStringStorage build() {
095            return this.builder.build();
096        }
097    
098        @Override
099        public String toString() {
100            if (this.propname == null) {
101                return "Molecule name collector callback";
102            } else {
103                return "Property collector callback forn property " + this.propname;
104            }
105        }
106    }