001package org.biojava.bio.annodb;
002
003import java.util.HashSet;
004import java.util.Iterator;
005import java.util.Set;
006
007import org.biojava.bio.Annotation;
008import org.biojava.bio.AnnotationTools;
009import org.biojava.bio.AnnotationType;
010
011/**
012 * <p>A no-frills implementation of AnnotationDB.</p>
013 *
014 * @author Matthew Pocock
015 * @since 1.3
016 */
017public class SimpleAnnotationDB implements AnnotationDB {
018  private final String name;
019  private final Set anns;
020  private final AnnotationType schema;
021
022  /**
023   * Create a no-frills AnnotationDB instancec.
024   *
025   * @param name    the name of this Annotation DB
026   * @param anns    a Set of Annotation instances it contains
027   * @param schema  an AnnotationType schema that applies to them
028   */
029  public SimpleAnnotationDB(String name, Set anns, AnnotationType schema) {
030    this.name = name;
031    this.anns = anns;
032    this.schema = schema;
033  }
034  
035  public String getName() {
036    return this.name;
037  }
038  
039  public int size() {
040    return anns.size();
041  }
042  
043  public AnnotationType getSchema() {
044    return schema;
045  }
046
047  public Iterator iterator() {
048    return anns.iterator();
049  }
050  
051  public AnnotationDB filter(AnnotationType at) {
052    Set hits = new HashSet();
053    
054    AnnotationType intersection = AnnotationTools.intersection(schema, at);
055    if(intersection != AnnotationType.NONE) {
056      for(Iterator i = anns.iterator(); i.hasNext(); ) {
057        Annotation ann = (Annotation) i.next();
058        if(at.instanceOf(ann)) {
059          hits.add(ann);
060        }
061      }
062    }
063    
064    if(hits.isEmpty()) {
065      return AnnotationDB.EMPTY;
066    } else {
067      return new SimpleAnnotationDB("", hits, intersection);
068    }
069  }
070  
071  public AnnotationDB search(AnnotationType at) {
072    Set hits = new HashSet();
073    
074    for(Iterator i = anns.iterator(); i.hasNext(); ) {
075      Annotation ann = (Annotation) i.next();
076      hits.addAll(AnnotationTools.searchAnnotation(ann, at));
077    }
078    
079    if(hits.isEmpty()) {
080      return AnnotationDB.EMPTY;
081    } else {
082      return new SimpleAnnotationDB("", hits, at);
083    }
084  }
085}
086