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.bio.taxa; 022 023import java.util.HashMap; 024import java.util.Iterator; 025import java.util.Map; 026 027import org.biojava.utils.SmallSet; 028 029/** 030 * A no-frills implementation of TaxaFactory that builds an in-memory Taxa tree. 031 * 032 * @author Matthew Pocock 033 * @deprecated replaced by classes in {@link org.biojavax.bio.taxa org.biojavax.bio.taxa} 034 */ 035public class SimpleTaxonFactory implements TaxonFactory { 036 /** 037 * The TaxonFactory that the biojava system should use for storing 038 * the taxonomy used by swissprot and embl as in-memory objects. 039 */ 040 public static final SimpleTaxonFactory GLOBAL 041 = new SimpleTaxonFactory("GLOBAL"); 042 043 private final Taxon root; 044 private final String name; 045 private final Map taxonBySciName = new HashMap(); 046 047 public SimpleTaxonFactory(String name) { 048 this.name = name; 049 this.root = createTaxon("ROOT", ""); 050 } 051 052 public Taxon getRoot() { 053 return root; 054 } 055 056 public String getName() { 057 return name; 058 } 059 060 public Taxon importTaxon(Taxon taxon) { 061 SimpleTaxon can = canonicalize(taxon); 062 if(can == null) { 063 can = new SimpleTaxon(taxon.getScientificName(), taxon.getCommonName()); 064 065 for(Iterator i = taxon.getChildren().iterator(); i.hasNext(); ) { 066 Taxon child = (Taxon) i.next(); 067 addChild(can, child); 068 } 069 070 return can; 071 } else { 072 return can; 073 } 074 } 075 076 public Taxon createTaxon(String scientificName, String commonName) { 077 Taxon taxon = new SimpleTaxon(scientificName, commonName); 078 taxonBySciName.put(scientificName, taxon); 079 return taxon; 080 } 081 082 public Taxon addChild(Taxon parent, Taxon child) { 083 if(canonicalize(parent) == null) { 084 throw new IllegalArgumentException("Parent taxon not owned by this TaxonFactory"); 085 } 086 087 SimpleTaxon sparent = (SimpleTaxon) parent; 088 SimpleTaxon schild = (SimpleTaxon) importTaxon(child); 089 090 if(sparent.children == null) { 091 sparent.children = new SmallSet(); 092 } 093 094 sparent.children.add(schild); 095 schild.setParent(sparent); 096 097 return schild; 098 } 099 100 public Taxon removeChild(Taxon parent, Taxon child) { 101 SimpleTaxon sparent = canonicalize(parent); 102 SimpleTaxon schild = canonicalize(child); 103 104 if(sparent == null) { 105 throw new IllegalArgumentException("Don't know about parent Taxon"); 106 } 107 108 if( 109 (schild != null) && 110 (sparent.children != null) && 111 (sparent.children.remove(schild)) 112 ) { 113 return schild; 114 } else { 115 return null; 116 } 117 } 118 119 public Taxon search(Object id) { 120 return (Taxon) taxonBySciName.get(id); 121 } 122 123 private SimpleTaxon canonicalize(Taxon taxon) { 124 return (SimpleTaxon) search(taxon.getScientificName()); 125 } 126}