001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.core.search.io;
022
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Set;
027import java.util.NoSuchElementException;
028
029import org.biojava.nbio.core.sequence.template.Sequence;
030import java.util.Map;
031
032/**
033 * This class models a search result.
034 * You will find one of this for every query sequence specified in the run.
035 *
036 * Designed by Paolo Pavan.
037 * You may want to find my contacts on Github and LinkedIn for code info
038 * or discuss major changes.
039 * https://github.com/paolopavan
040 *
041 * @author Paolo Pavan
042 */
043
044public abstract class Result implements Iterable<Hit>{
045        private String program;
046        private String version;
047        private String reference;
048        private String dbFile;
049
050        private Map<String, String> programSpecificParameters;
051
052        private int iterationNumber;
053        private String queryID;
054        private String queryDef;
055        private int queryLength;
056        private Sequence querySequence;
057        private List<Hit> hits;
058        private int hitCounter = -1;
059
060        public Result(String program, String version, String reference, String dbFile, Map<String, String> programSpecificParameters, int iterationNumber, String queryID, String queryDef, int queryLength, List<Hit> hits, Sequence querySequence) {
061                this.program = program;
062                this.version = version;
063                this.reference = reference;
064                this.dbFile = dbFile;
065                this.programSpecificParameters = programSpecificParameters;
066                this.iterationNumber = iterationNumber;
067                this.queryID = queryID;
068                this.queryDef = queryDef;
069                this.queryLength = queryLength;
070                this.hits = hits;
071                this.querySequence = querySequence;
072        }
073
074        @Override
075        public int hashCode() {
076                int hash = 7;
077                hash = 29 * hash + (this.queryID != null ? this.queryID.hashCode() : 0);
078                hash = 29 * hash + (this.queryDef != null ? this.queryDef.hashCode() : 0);
079                hash = 29 * hash + (this.hits != null ? this.hits.hashCode() : 0);
080                return hash;
081        }
082        /**
083         * Experimental.
084         * Wants to return an hashcode designed to allow conceptual comparisons of search results.
085         * Wants to implement conceptual comparisons of search results.
086         * Fields unrelated to search are deliberately not considered.
087         * @return
088         */
089        @Override
090        public boolean equals(Object obj) {
091                if (obj == null) {
092                        return false;
093                }
094                if (getClass() != obj.getClass()) {
095                        return false;
096                }
097                final Result other = (Result) obj;
098                if ((this.queryID == null) ? (other.queryID != null) : !this.queryID.equals(other.queryID)) {
099                        return false;
100                }
101                if ((this.queryDef == null) ? (other.queryDef != null) : !this.queryDef.equals(other.queryDef)) {
102                        return false;
103                }
104                if (this.hits != other.hits && (this.hits == null || !this.hits.equals(other.hits))) {
105                        return false;
106                }
107                return true;
108        }
109
110        public int getIterationNumber() {
111                return iterationNumber;
112        }
113
114        public String getQueryID() {
115                return queryID;
116        }
117
118        public String getQueryDef() {
119                return queryDef;
120        }
121
122        public int getQueryLength() {
123                return queryLength;
124        }
125
126        public int getHitCounter() {
127                return hitCounter;
128        }
129
130        public String getProgram() {
131                return program;
132        }
133
134        public String getVersion() {
135                return version;
136        }
137
138        public String getReference() {
139                return reference;
140        }
141
142        public String getDbFile() {
143                return dbFile;
144        }
145
146        public Set<String> getProgramSpecificParametersList() {
147                return programSpecificParameters.keySet();
148        }
149
150        public String getProgramSpecificParameter(String key) {
151                return programSpecificParameters.get(key);
152        }
153        /**
154         * returns the reference to the original and whole sequence used to query the database.
155         * Available only if the ResultFactory implements setQueryReferences and
156         * it was used before the parsing with SearchIO
157         * @return Sequence object
158         */
159        public Sequence getQuerySequence() {
160                return querySequence;
161        }
162
163        @Override
164        public Iterator<Hit> iterator() {
165                return new Iterator<Hit>() {
166                        int currentResult = 0;
167                        @Override
168                        public boolean hasNext() {
169                                return currentResult < hits.size();
170                        }
171
172                        @Override
173                        public Hit next() {
174                                if(!hasNext()){
175                                        throw new NoSuchElementException();
176                                }
177                                return hits.get(currentResult++);
178                        }
179
180                        @Override
181                        public void remove() {
182                                throw new UnsupportedOperationException("The remove operation is not supported by this iterator");
183                        }
184                };
185        }
186}