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 */ 021 022 023package org.biojavax.ga.impl; 024 025import java.util.HashMap; 026import java.util.HashSet; 027import java.util.Iterator; 028import java.util.Map; 029import java.util.Set; 030 031import org.biojavax.ga.Organism; 032import org.biojavax.ga.exception.IllegalOrganismException; 033 034/** 035 * <p>Simple concrete implementation of the <code>Population</code> interface</p> 036 * <p>Internally the SimplePopulation store Organisms in a HashMap</p> 037 * 038 * @author Mark Schreiber 039 * @version 1.0 040 * @since 1.5 041 */ 042 043public final class SimplePopulation extends AbstractPopulation{ 044 private Map orgs; 045 046 public SimplePopulation(String name) { 047 super(name); 048 this.orgs = new HashMap(); 049 } 050 051 public SimplePopulation(){ 052 this(""); 053 } 054 055 protected void addOrganismImpl(Organism orgToAdd) throws IllegalOrganismException{ 056 if(orgs.containsKey(orgToAdd.getName())) 057 throw new IllegalOrganismException("All organisms in a population must have a unique name"); 058 orgs.put(orgToAdd.getName(),orgToAdd); 059 } 060 061 protected void removeOrganismImpl(Organism orgToRemove){ 062 orgs.remove(orgToRemove.getName()); 063 } 064 065 protected void removeAllOrganismsImpl(){ 066 orgs = new HashMap(); 067 } 068 069 public Organism getOrganismByName(String name){ 070 return (Organism)orgs.get(name); 071 } 072 073 public int size() { 074 return orgs.size(); 075 } 076 public Iterator organisms() { 077 return orgs.values().iterator(); 078 } 079 public Set getOrganisms() { 080 return new HashSet(orgs.values()); 081 } 082 083}