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.Iterator;
024import java.util.List;
025import java.util.NoSuchElementException;
026
027import org.biojava.nbio.core.sequence.template.Sequence;
028
029/**
030 * This class models a search Hit.
031 * You will retrieve a list of this using iterator of a Result
032 * Designed by Paolo Pavan.
033 * You may want to find my contacts on Github and LinkedIn for code info
034 * or discuss major changes.
035 * https://github.com/paolopavan
036 *
037 * @author Paolo Pavan
038 */
039
040public abstract class Hit implements Iterable<Hsp>{
041        private final int hitNum;
042        private final String hitId;
043        private final String hitDef;
044        private final String hitAccession;
045        /**
046         * the length of the hit sequence
047         */
048        private final int hitLen;
049        private final List<Hsp> hsps;
050        private Sequence hitSequence;
051
052
053
054        public Hit(int hitNum, String hitId, String hitDef, String hitAccession, int hitLen, List<Hsp> hsps, Sequence hitSequence) {
055                this.hitNum = hitNum;
056                this.hitId = hitId;
057                this.hitDef = hitDef;
058                this.hitAccession = hitAccession;
059                this.hitLen = hitLen;
060                this.hsps = hsps;
061                this.hitSequence = hitSequence;
062        }
063
064        @Override
065        public int hashCode() {
066                int hash = 3;
067                hash = 89 * hash + this.hitLen;
068                hash = 89 * hash + (this.hsps != null ? this.hsps.hashCode() : 0);
069                return hash;
070        }
071         /**
072         * Implements conceptual comparisons of search results.
073         * Fields unrelated to search are deliberately not considered.
074         * @return
075         */
076        @Override
077        public boolean equals(Object obj) {
078                if (obj == null) {
079                        return false;
080                }
081                if (getClass() != obj.getClass()) {
082                        return false;
083                }
084                final Hit other = (Hit) obj;
085                if (this.hitLen != other.hitLen) {
086                        return false;
087                }
088                if (this.hsps != other.hsps && (this.hsps == null || !this.hsps.equals(other.hsps))) {
089                        return false;
090                }
091                return true;
092        }
093
094        public int getHitNum() {
095                return hitNum;
096        }
097
098        public String getHitId() {
099                return hitId;
100        }
101
102        public String getHitDef() {
103                return hitDef;
104        }
105
106        public String getHitAccession() {
107                return hitAccession;
108        }
109
110        public int getHitLen() {
111                return hitLen;
112        }
113
114        /**
115         * returns the reference to the original and whole sequence hit in the database.
116         * Available only if the ResultFactory implements setHitReferences and
117         * it was used before the parsing with SearchIO
118         * @return Sequence object
119         */
120        public Sequence getHitSequence() {
121                return hitSequence;
122        }
123
124        @Override
125        public Iterator<Hsp> iterator() {
126                return new Iterator<Hsp>() {
127                        int current = 0;
128                        @Override
129                        public boolean hasNext() {
130                                return current < hsps.size();
131                        }
132
133                        @Override
134                        public Hsp next() {
135                                if(!hasNext()){
136                                        throw new NoSuchElementException();
137                                }
138                                return hsps.get(current++);
139                        }
140
141                        @Override
142                        public void remove() {
143                                throw new UnsupportedOperationException("The remove operation is not supported by this iterator");
144                        }
145                };
146        }
147}