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.biojava.stats.svm;
024
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.Iterator;
029import java.util.Map;
030import java.util.Set;
031
032/**
033 * No-frills implementation of SVMTarget.
034 *
035 * @author Matthew Pocock
036 */
037public class SimpleSVMTarget implements SVMTarget {
038  private Set itemTargetSet;
039  private Map itemToItemTarget;
040
041  public SimpleSVMTarget() {
042    itemTargetSet = new HashSet();
043    itemToItemTarget = new HashMap();
044  }
045  
046  public SimpleSVMTarget(Collection items) {
047    this();
048    for(Iterator i = items.iterator(); i.hasNext(); ) {
049      addItem(i.next());
050    }
051  }
052  
053  public Set items() {
054    return itemToItemTarget.keySet();
055  }
056  
057  public Set itemTargets() {
058    return itemTargetSet;
059  }
060
061  public double getTarget(Object item) {
062    return ((ItemValue) itemToItemTarget.get(item)).getValue();
063  }
064  
065  public void setTarget(Object item, double target) {
066    ItemValue iv = (ItemValue) itemToItemTarget.get(item);
067    iv.setValue(target);
068  }
069  
070  public void addItem(Object item) {
071    ItemValue iv = new SimpleItemValue(item, 0.0);
072    itemToItemTarget.put(item, iv);
073    itemTargetSet.add(iv);
074  }
075  
076  public void addItemTarget(Object item, double target) {
077    ItemValue iv = new SimpleItemValue(item, target);
078    itemToItemTarget.put(item, iv);
079    itemTargetSet.add(iv);
080  }
081  
082  public void removeItem(Object item) {
083    itemToItemTarget.remove(item);
084    itemTargetSet.remove(item);
085  }
086  
087  public void clear() {
088    itemToItemTarget.clear();
089    itemTargetSet.clear();
090  }
091}