com.hfg.automation.platelayer.PlateDataLayer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.automation.platelayer;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.hfg.automation.AutomationXML;
import com.hfg.automation.WellRange;
import com.hfg.automation.WellRef;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.collection.OrderedMap;
import com.hfg.util.collection.OrderedSet;
import com.hfg.xml.HfgXMLSerializable;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Plate layer for specifying per-well volumes of a sample.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class PlateDataLayer extends PlateLayerImpl
{
private Class mDataClass;
private Map mDataMap = new OrderedMap<>();
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public PlateDataLayer(String inName, Class inSampleClass)
{
super(inName);
mDataClass = inSampleClass;
}
//---------------------------------------------------------------------------
public PlateDataLayer(Class inSampleClass)
{
mDataClass = inSampleClass;
}
//--------------------------------------------------------------------------
public PlateDataLayer(XMLNode inXML)
{
super(inXML);
String dataClassname = inXML.getAttributeValue(AutomationXML.DATA_CLASS_ATT);
if (! StringUtil.isSet(dataClassname))
{
throw new RuntimeException("No " + AutomationXML.CLASS_ATT + " specified on the layer's XML tag!");
}
try
{
mDataClass = (Class) Class.forName(dataClassname);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
List wellTags = inXML.getSubtagsByName(AutomationXML.WELL);
if (CollectionUtil.hasValues(wellTags))
{
for (XMLNode wellTag : wellTags)
{
D data;
if (dataClassname.equals(String.class.getName()))
{
data = (D) wellTag.getUnescapedContent();
}
else if (dataClassname.equals(Integer.class.getName()))
{
data = (D) new Integer(wellTag.getContent().trim());
}
else if (dataClassname.equals(Float.class.getName()))
{
data = (D) new Float(wellTag.getContent().trim());
}
else if (dataClassname.equals(Double.class.getName()))
{
data = (D) new Double(wellTag.getContent().trim());
}
else if (dataClassname.equals(Boolean.class.getName()))
{
data = (D) new Boolean(wellTag.getContent().trim());
}
else if (dataClassname.equals(Long.class.getName()))
{
data = (D) new Long(wellTag.getContent().trim());
}
else
{
try
{
Class clazz = Class.forName(dataClassname);
Constructor constructor = clazz.getConstructor(XMLNode.class);
data = (D) constructor.newInstance(wellTag.getSubtags().get(0));
}
catch (Exception e)
{
throw new RuntimeException("Error during Data instantiation from its XML tag!", e);
}
}
addData(new WellRef(wellTag.getAttributeValue(AutomationXML.REF_ATT)), data);
}
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public XMLNode toXMLNode()
{
XMLNode node = super.toXMLNode();
node.setAttribute(AutomationXML.DATA_CLASS_ATT, getDataClass().getName());
if (CollectionUtil.hasValues(mDataMap))
{
for (WellRef wellRef : mDataMap.keySet())
{
XMLNode wellTag = new XMLTag(AutomationXML.WELL);
wellTag.setAttribute(AutomationXML.REF_ATT, wellRef);
D data = mDataMap.get(wellRef);
if (data instanceof String
|| data instanceof Number
|| data instanceof Boolean)
{
wellTag.setContent(data.toString());
}
else if (data instanceof HfgXMLSerializable)
{
wellTag.addSubtag(((HfgXMLSerializable)data).toXMLNode());
}
node.addSubtag(wellTag);
}
}
return node;
}
//--------------------------------------------------------------------------
@Override
public PlateDataLayer clone()
{
PlateDataLayer theClone = (PlateDataLayer) super.clone();
// TODO: Deep clone the data map
theClone.mDataMap = new OrderedMap(mDataMap);
return theClone;
}
//---------------------------------------------------------------------------
public Class getDataClass()
{
return mDataClass;
}
//---------------------------------------------------------------------------
public PlateDataLayer addData(WellRange inWellRange, D inData)
{
for (WellRef wellRef : inWellRange.toArray())
{
addData(wellRef, inData);
}
return this;
}
//---------------------------------------------------------------------------
public PlateDataLayer addData(WellRef inWellRef, D inData)
{
mDataMap.put(inWellRef, inData);
return this;
}
//---------------------------------------------------------------------------
public D getData(WellRef inWellRef)
{
return mDataMap.get(inWellRef);
}
//---------------------------------------------------------------------------
@Override
public Collection getOccupiedWellRefs()
{
Set wellRefs = null;
if (CollectionUtil.hasValues(mDataMap))
{
wellRefs = new OrderedSet<>(mDataMap.keySet());
}
return wellRefs;
}
//---------------------------------------------------------------------------
@Override
public WellDataLayer getWellLayer(WellRef inWellRef)
{
WellDataLayer wellLayer = null;
D data = getData(inWellRef);
if (data != null)
{
wellLayer = new WellDataLayer<>(getDataClass(), getData(inWellRef));
wellLayer.setName(name());
}
return wellLayer;
}
}