All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.automation.PlateImpl Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.automation;



import java.io.ByteArrayInputStream;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;

//------------------------------------------------------------------------------
/**
 Abstract base class for plates.
 
@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 abstract class PlateImpl implements Plate, Cloneable { private String mName; private String mBarcode; private PlateType mType; private PlateLayout mLayout; private Map mAttributes; private WellFactory mWellFactory; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- protected PlateImpl() { } //--------------------------------------------------------------------------- public PlateImpl(PlateType inType) { this(inType, null); } //--------------------------------------------------------------------------- public PlateImpl(PlateType inType, String inName) { mType = inType; mLayout = new PlateLayout(inType); mName = inName; } //-------------------------------------------------------------------------- public PlateImpl(XMLNode inXML) { inXML.verifyTagName(AutomationXML.PLATE); setName(inXML.getAttributeValue(AutomationXML.NAME_ATT)); setBarcode(inXML.getAttributeValue(AutomationXML.BARCODE_ATT)); XMLNode layoutTag = inXML.getOptionalSubtagByName(AutomationXML.PLATE_LAYOUT); if (layoutTag != null) { setLayout(new PlateLayout(layoutTag)); } XMLNode attributesTag = inXML.getOptionalSubtagByName(AutomationXML.ATTRIBUTES); if (attributesTag != null) { List attributeTags = attributesTag.getSubtagsByName(AutomationXML.ATTRIBUTE); for (XMLNode attrTag : attributeTags) { String name = attrTag.getAttributeValue(AutomationXML.NAME_ATT); String className = attrTag.getAttributeValue(AutomationXML.CLASS_ATT); if (attrTag.getAttributeValue(AutomationXML.IS_NULL_ATT) != null) { setAttribute(name, null); } else { String value = attrTag.getUnescapedContent(); try { Class clazz = Class.forName(className); if (String.class.getName().equals(className)) { setAttribute(name, value); } else if (Integer.class.getName().equals(className)) { setAttribute(name, Integer.valueOf(value)); } else if (Long.class.getName().equals(className)) { setAttribute(name, Long.valueOf(value)); } else if (Float.class.getName().equals(className)) { setAttribute(name, Float.valueOf(value)); } else if (Double.class.getName().equals(className)) { setAttribute(name, Double.valueOf(value)); } else if (Boolean.class.getName().equals(className)) { setAttribute(name, Boolean.valueOf(value)); } else { try { Constructor constructor = clazz.getConstructor(XMLTag.class); setAttribute(name, constructor.newInstance(new XMLTag(new ByteArrayInputStream(value.getBytes())))); } catch (NoSuchMethodException e) { Constructor constructor = clazz.getConstructor(String.class); setAttribute(name, constructor.newInstance(value)); } } } catch (Exception e) { throw new RuntimeException("Problem extracting the value for plate attribute " + StringUtil.singleQuote(name) + "!", e); } } } } } //########################################################################### // PUBLIC METHODS //########################################################################### //-------------------------------------------------------------------------- @Override public PlateImpl clone() { PlateImpl theClone; try { theClone = (PlateImpl) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException(e); } if (mAttributes != null) { // For right now this is a shallow clone of the attributes theClone.mAttributes = new HashMap<>(mAttributes); } return theClone; } //-------------------------------------------------------------------------- public XMLNode toXMLNode() { XMLNode node = new XMLTag(AutomationXML.PLATE); node.setAttribute(AutomationXML.CLASS_ATT, getClass().getName()); if (StringUtil.isSet(name())) { node.setAttribute(AutomationXML.NAME_ATT, name()); } if (StringUtil.isSet(getBarcode())) { node.setAttribute(AutomationXML.BARCODE_ATT, getBarcode()); } if (getLayout() != null) { node.addSubtag(getLayout().toXMLNode()); } if (mAttributes != null) { XMLNode attributesTag = new XMLTag(AutomationXML.ATTRIBUTES); node.addSubtag(attributesTag); for (String attrName : mAttributes.keySet()) { XMLNode attributeTag = new XMLTag(AutomationXML.ATTRIBUTE); attributeTag.setAttribute(AutomationXML.NAME_ATT, attrName); Object value = mAttributes.get(attrName); if (value != null) { attributeTag.setAttribute(AutomationXML.CLASS_ATT, value.getClass().getName()); if (value instanceof XMLizable) { attributeTag.addContentWithoutEscaping(((XMLizable)value).toXML()); } else { attributeTag.setContent(value.toString()); } } else { attributeTag.setAttribute(AutomationXML.IS_NULL_ATT, 1); } attributesTag.addSubtag(attributeTag); } } return node; } //--------------------------------------------------------------------------- @Override public int hashCode() { int hashCode = getType().hashCode(); hashCode += 31 * getLayout().hashCode(); if (StringUtil.isSet(getBarcode())) { hashCode += 31 * getBarcode().hashCode(); } if (StringUtil.isSet(name())) { hashCode += 31 * name().hashCode(); } if (getLayout() != null) { hashCode += 31 * getLayout().hashCode(); } if (getType() != null) { hashCode += 31 * getType().hashCode(); } return hashCode; } //--------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { return (0 == compareTo(inObj2)); } //--------------------------------------------------------------------------- @Override public int compareTo(Object inObj2) { int result = -1; if (inObj2 != null && inObj2 instanceof Plate) { result = 0; if (this != inObj2) { Plate plate2 = (Plate) inObj2; result = CompareUtil.compare(getBarcode(), plate2.getBarcode()); if (0 == result) { result = CompareUtil.compare(name(), plate2.name()); } if (0 == result) { result = CompareUtil.compare(getLayout(), plate2.getLayout()); } if (0 == result) { result = CompareUtil.compare(getType(), plate2.getType()); } } } return result; } //--------------------------------------------------------------------------- @Override public String toString() { return name(); } //--------------------------------------------------------------------------- @Override public String name() { String name = mName; if (! StringUtil.isSet(name)) { name = getBarcode(); } return name; } //--------------------------------------------------------------------------- public PlateImpl setName(String inValue) { mName = inValue; return this; } //--------------------------------------------------------------------------- public String getBarcode() { return mBarcode; } //--------------------------------------------------------------------------- public PlateImpl setBarcode(String inValue) { mBarcode = inValue; return this; } //--------------------------------------------------------------------------- public PlateType getType() { return mType; } //--------------------------------------------------------------------------- public PlateImpl setLayout(PlateLayout inValue) { // TODO: Check for incompatibility with the PlateType mLayout = inValue; return this; } //--------------------------------------------------------------------------- public PlateLayout getLayout() { return mLayout; } //-------------------------------------------------------------------------- public void setAttribute(String inName, Object inValue) { if (null == mAttributes) { mAttributes = new HashMap<>(); } mAttributes.put(inName, inValue); } //-------------------------------------------------------------------------- public boolean hasAttribute(String inName) { return mAttributes != null && mAttributes.containsKey(inName); } //-------------------------------------------------------------------------- public Object getAttribute(String inName) { Object attr = null; if (mAttributes != null) { attr = mAttributes.get(inName); } return attr; } //-------------------------------------------------------------------------- public Collection getAttributeNames() { Collection attrNames = null; if (mAttributes != null) { attrNames = mAttributes.keySet(); } return attrNames; } //-------------------------------------------------------------------------- public void clearAttributes() { if (mAttributes != null) { mAttributes.clear(); } } //-------------------------------------------------------------------------- public Object removeAttribute(String inName) { Object attr = null; if (mAttributes != null) { attr = mAttributes.remove(inName); } return attr; } //--------------------------------------------------------------------------- public boolean containsVolumes() { boolean containsVolumes = false; Collection occupiedWells = getOccupiedWells(); if (CollectionUtil.hasValues(occupiedWells)) { for (Well well : occupiedWells) { if (well.getVolume() != null) { containsVolumes = true; break; } } } return containsVolumes; } //--------------------------------------------------------------------------- public W allocateWell(String inWellRef) { return allocateWell(new WellRef(inWellRef)); } //--------------------------------------------------------------------------- public W allocateWell(WellRef inWellRef) { if (! getType().isValidWellRef(inWellRef)) { throw new AutomationConfigurationException(inWellRef + " is not a valid well reference for a " + getType() + "!"); } W well = getWellFactory().createWell(inWellRef); setWell(well); return well; } //--------------------------------------------------------------------------- protected PlateImpl setPlateType(PlateType inValue) { mType = inValue; return this; } //--------------------------------------------------------------------------- protected void setWellFactory(WellFactory inValue) { mWellFactory = inValue; } //--------------------------------------------------------------------------- protected WellFactory getWellFactory() { return mWellFactory; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy