package reaction;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import miscellaneous.CSVList;
import miscellaneous.Copiable;
import recorders.Recorder;
public class SimplePool extends TreeMap<String,Item> implements Copiable<SimplePool> {
private static final long serialVersionUID = 1L;
/**
*
* @param itemName
* @return item with the given item name. if item is new returns a new item.
*
*/
public Item get(String itemName)
{
Item ret = super.get(itemName);
if(ret == null)
{
return new Item(itemName, 0, 0.0);
}
return ret;
}
/**
* changes a list of items
*
* @param newItems
*/
public void change(List<Item> newItems)
{
// Item contains name = Transcript Name and value = number of new transcripts of this name
// For each transcript in the pool add to the current pool count
for (Item item : newItems)
{
this.change(item);
}
}
public void change(SimplePool pool)
{
// Item contains name = Transcript Name and value = number of new transcripts of this name
// For each transcript in the pool add to the current pool count
for (String itemName : pool.keySet())
{
this.change(pool.get(itemName));
}
}
/**
* changes an item. If the item is new than creates it. The given value is added to item's previous value (i.e. amount)
*
* @param item
*/
public void change(Item item)
{
Item i = this.get(item.name).copy();
if(i == null)
{
i = new Item(item.name, item.value, item.sortVal);
}
else
{
i.value = i.value + item.value;
}
if(i.value == Double.NaN)
{
Recorder.log("Warning! NaN calculated for " + item.name, this);
}
this.put(i.name, i);
}
/**
* creates an item with name, amount and delay
*
* @param name
* @param amount
* @param delay
*/
public void change(String name, double amount, double delay)
{
this.change(new Item(name, amount, delay));
}
/**
*
* @param itemName
* @return amount (value) of an item
*/
public Double getCount(String itemName)
{
Item temp = this.get(itemName);
if(temp == null)
{
return new Double(0);
}
return temp.value;
}
/**
* @return comma separated list of item strings?
*/
@Override
public String toString()
{
CSVList ret = new CSVList();
for (String str : this.descendingKeySet())
{
Item i = this.get(str);
ret.add(i.toString());
}
return ret.toString();
}
/**
*
* @return name (key) of item
*/
public Set<String> getNames()
{
return this.keySet();
}
/**
* @return copy of the pool of items
*/
@Override
public SimplePool copy()
{
SimplePool ret = new SimplePool();
for (Item item : this.values())
{
ret.change(item.copy());
}
return ret;
}
/*
* public void multiplyBy(SimplePool pool) { // Item contains name = Transcript Name and value = number of new transcripts of this name // For each transcript in the pool add to the current pool count for(String itemName : pool.keySet()) {
* this.multiplyBy(pool.get(itemName)); } }
*
* public void divideBy(SimplePool pool) { // Item contains name = Transcript Name and value = number of new transcripts of this name // For each transcript in the pool add to the current pool count for(String itemName : pool.keySet()) {
* this.divideBy(pool.get(itemName)); } }
*/
/*
* public void multiplyBy(double factor) { for(Item i : this.values()) { i.value = i.value*factor; } }
*
* public void multiplyBy(Item item) { Item i = this.get(item.name).copy(); if(i == null) { i = new Item(item.name, 0, item.sortVal); } else { i.value = i.value*item.value; } this.put(i.name, i); }
*
* public void divideBy(Item item) { if(item == null) { return; } Item i = this.get(item.name).copy(); // Division by zero should never happen so set result to zero if it does if(i == null || item.value == 0) { i = new Item(item.name, 0,
* item.sortVal); } else { i.value = i.value/item.value; } this.put(i.name, i); }
*/
// public Item remove(String itemName)
// {
// return super.remove(itemName);
// }
// public double subtract(Item item)
// {
// double ret=item.value;
// Item i = this.get(item.name).copy();
//
// if(i == null)
// {
// //if we can't find an item in this pool then
// i = new Item(item.name, item.value, item.sortVal);
// }
// else
// {
// i.value = i.value - item.value;
// }
// if (i.value <= 0)
// {
// ret = item.value+i.value;
// i.value = 0;
// // Recorder.log("Warning! Tried to subtract too many things from pool with item name " + item.name, this);
// }
// this.put(i.name, i);
// return ret;
// }
//
// public Double subtract(String name, Double amount)
// {
// return this.subtract(new Item(name, amount));
// }
//
// public Double getTotal()
// {
// Double ret = 0.0;
// for(Item item : this.values())
// {
// ret = ret + item.value;
// }
// return ret;
// }
}