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