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    
011    package com.chemaxon.overlap.io;
012    
013    import chemaxon.formats.MolFormatException;
014    import chemaxon.formats.MolImporter;
015    import chemaxon.struc.Molecule;
016    import com.google.common.annotations.Beta;
017    import com.google.common.base.Optional;
018    
019    /**
020     * Simple structure record implementation.
021     *
022     * @author Gabor Imre
023     */
024    @Beta
025    class StructureRecordImpl implements StructureRecord {
026        /**
027         * Represented record index.
028         */
029        private final int currReadno;
030    
031        /**
032         * Represented structure source.
033         */
034        private final Optional<String> molString;
035    
036        /**
037         * Structure format.
038         */
039        private final String format;
040    
041        /**
042         * Successfully parsed structure or null upon non parse / error.
043         */
044        private Molecule parsed = null;
045    
046        /**
047         * Construct.
048         *
049         * @param format        Structure format
050         * @param currReadno    Represented 0-based record index
051         * @param molString     Represented structure source or null
052         */
053        StructureRecordImpl(String format, int currReadno, String molString) {
054            this.format = format;
055            this.currReadno = currReadno;
056            this.molString = Optional.fromNullable(molString);
057        }
058    
059        @Override
060        public int getReadIndex() {
061            return this.currReadno;
062        }
063    
064        @Override
065        public Optional<String> getStructureSource() {
066            return this.molString;
067        }
068    
069        @Override
070        public Molecule parseMolecule() throws MolFormatException {
071            if (this.parsed != null) {
072                return this.parsed;
073            }
074            this.parsed = MolImporter.importMol(this.molString.get(), this.format);
075            return parsed;
076        }
077    
078        @Override
079        public String toString() {
080            return "Readno: " + this.currReadno + " Format: " + this.format + " Source:" + this.molString;
081        }
082    
083    }