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