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.storage;
012    
013    import com.chemaxon.overlap.io.StructureRecord;
014    import chemaxon.struc.Molecule;
015    import com.google.common.base.Optional;
016    
017    /**
018     * Process queue element data object.
019     *
020     * @param <D>   Represented descriptor type
021     *
022     * @author Gabor Imre
023     */
024    class ProcessQueueItem<D> {
025    
026        /**
027         * Represented imported molecule.
028         */
029        private final StructureRecord mol;
030    
031        /**
032         * Construct new instance.
033         *
034         * @param mol   Import mol representation
035         */
036        public ProcessQueueItem(StructureRecord mol) {
037            this.mol = mol;
038        }
039    
040        /**
041         * Forward molecule parse.
042         *
043         * <p>This methow will not throw Exceptions upon parse failure. Parse failure is registered and later can be
044         * retrieved.</p>
045         *
046         * @return  True when successfull
047         *
048         * TODO: should throw MolFomatException and? report parse errro
049         */
050        public boolean parse() {
051            try {
052                this.parsedMol = this.mol.parseMolecule();
053                return true;
054            } catch (Exception ex) {
055                parseError(ex);
056                return false;
057            }
058        }
059    
060        /**
061         * Parsed molecule.
062         */
063        private Molecule parsedMol = null;
064    
065        /**
066         * Descriptor.
067         */
068        private D d = null;
069    
070        /**
071         * Parse error or process error.
072         */
073        private Throwable t = null;
074    
075        /**
076         * Report that parse error occured.
077         *
078         * @param t     Error
079         */
080        private void parseError(Throwable t) {
081            this.t = t;
082            this.d = null;
083        }
084    
085        /**
086         * Report that process error occured.
087         *
088         * @param t     Error
089         */
090        void processError(Throwable t) {
091            this.t = t;
092            this.d = null;
093        }
094    
095        /**
096         * Report successfull processing.
097         *
098         * @param d     Created descriptor
099         */
100        void ok(D d) {
101            this.t = null;
102            this.d = d;
103        }
104    
105        /**
106         * Represented structure index.
107         *
108         * @return  Represented structure index
109         */
110        public int getReadno() {
111            return this.mol.getReadIndex();
112        }
113    
114        /**
115         * Get represented molecule source if present.
116         *
117         * @return  Molecule source if present
118         */
119        public Optional<String> getMolString() {
120            return this.mol.getStructureSource();
121        }
122    
123        /**
124         * Check if a parse error occured.
125         *
126         * @return  True on parse error
127         */
128        boolean isParseError() {
129            return this.t != null && this.parsedMol == null;
130        }
131    
132        /**
133         * Check if process error occured.
134         *
135         * @return  True on process error
136         */
137        boolean isProcessError() {
138            return this.t != null && this.parsedMol != null;
139        }
140    
141        /**
142         * Parsed molecule or null for parse error.
143         *
144         * @return  Molecule
145         */
146        Molecule getMol() {
147            return this.parsedMol;
148        }
149    
150        /**
151         * Represented error if applicable or null.
152         *
153         * @return  Represented error.
154         */
155        Throwable getError() {
156            return this.t;
157        }
158    
159        /**
160         * Calculated descriptor if applicable or null.
161         *
162         * @return  Calculated descriptor
163         */
164        D getDescriptor() {
165            return this.d;
166        }
167    
168        @Override
169        public String toString() {
170            final StringBuilder ret = new StringBuilder();
171            ret.append(this.mol.toString());
172            ret.append(" isParseError: ").append(isParseError());
173            ret.append(" isProcessError: ").append(isProcessError());
174            ret.append(" Parsed mol set: ").append(this.parsedMol != null);
175            ret.append(" Descriptor set: ").append(this.d != null);
176            ret.append(" Error set: ").append(this.t == null ? "false" : "true (" + this.t.toString() + ")");
177            return ret.toString();
178        }
179    
180    
181    
182    }