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;
030
031/**
032 * This class models a search result.
033 * You will find one of this for every query sequence specified in the run.
034 *
035 * Designed by Paolo Pavan.
036 * You may want to find my contacts on Github and LinkedIn for code info
037 * or discuss major changes.
038 * https://github.com/paolopavan
039 *
040 * @author Paolo Pavan
041 */
042
043public abstract class Result implements Iterable<Hit>{
044        private String program;
045        private String version;
046        private String reference;
047        private String dbFile;
048
049        private HashMap<String,String> programSpecificParameters;
050
051        private int iterationNumber;
052        private String queryID;
053        private String queryDef;
054        private int queryLength;
055        private Sequence querySequence;
056        private List<Hit> hits;
057        private int hitCounter = -1;
058
059        public Result(String program, String version, String reference, String dbFile, HashMap<String, String> programSpecificParameters, int iterationNumber, String queryID, String queryDef, int queryLength, List<Hit> hits, Sequence querySequence) {
060                this.program = program;
061                this.version = version;
062                this.reference = reference;
063                this.dbFile = dbFile;
064                this.programSpecificParameters = programSpecificParameters;
065                this.iterationNumber = iterationNumber;
066                this.queryID = queryID;
067                this.queryDef = queryDef;
068                this.queryLength = queryLength;
069                this.hits = hits;
070                this.querySequence = querySequence;
071        }
072
073        @Override
074        public int hashCode() {
075                int hash = 7;
076                hash = 29 * hash + (this.queryID != null ? this.queryID.hashCode() : 0);
077                hash = 29 * hash + (this.queryDef != null ? this.queryDef.hashCode() : 0);
078                hash = 29 * hash + (this.hits != null ? this.hits.hashCode() : 0);
079                return hash;
080        }
081        /**
082         * Experimental.
083         * Wants to return an hashcode designed to allow conceptual comparisons of search results.
084         * Wants to implement conceptual comparisons of search results.
085         * Fields unrelated to search are deliberately not considered.
086         * @return
087         */
088        @Override
089        public boolean equals(Object obj) {
090                if (obj == null) {
091                        return false;
092                }
093                if (getClass() != obj.getClass()) {
094                        return false;
095                }
096                final Result other = (Result) obj;
097                if ((this.queryID == null) ? (other.queryID != null) : !this.queryID.equals(other.queryID)) {
098                        return false;
099                }
100                if ((this.queryDef == null) ? (other.queryDef != null) : !this.queryDef.equals(other.queryDef)) {
101                        return false;
102                }
103                if (this.hits != other.hits && (this.hits == null || !this.hits.equals(other.hits))) {
104                        return false;
105                }
106                return true;
107        }
108
109        public int getIterationNumber() {
110                return iterationNumber;
111        }
112
113        public String getQueryID() {
114                return queryID;
115        }
116
117        public String getQueryDef() {
118                return queryDef;
119        }
120
121        public int getQueryLength() {
122                return queryLength;
123        }
124
125        public int getHitCounter() {
126                return hitCounter;
127        }
128
129        public String getProgram() {
130                return program;
131        }
132
133        public String getVersion() {
134                return version;
135        }
136
137        public String getReference() {
138                return reference;
139        }
140
141        public String getDbFile() {
142                return dbFile;
143        }
144
145        public Set<String> getProgramSpecificParametersList() {
146                return programSpecificParameters.keySet();
147        }
148
149        public String getProgramSpecificParameter(String key) {
150                return programSpecificParameters.get(key);
151        }
152        /**
153         * returns the reference to the original and whole sequence used to query the database.
154         * Available only if the ResultFactory implements setQueryReferences and
155         * it was used before the parsing with SearchIO
156         * @return Sequence object
157         */
158        public Sequence getQuerySequence() {
159                return querySequence;
160        }
161
162        @Override
163        public Iterator<Hit> iterator() {
164                return new Iterator<Hit>() {
165                        int currentResult = 0;
166                        @Override
167                        public boolean hasNext() {
168                                return currentResult < hits.size();
169                        }
170
171                        @Override
172                        public Hit next() {
173                                if(!hasNext()){
174                                        throw new NoSuchElementException();
175                                }
176                                return hits.get(currentResult++);
177                        }
178
179                        @Override
180                        public void remove() {
181                                throw new UnsupportedOperationException("The remove operation is not supported by this iterator");
182                        }
183                };
184        }
185}