com.hfg.automation.WellRange 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;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Container for a simple rectangular plate well range (ex: 'A1:D10').
@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 WellRange implements Comparable
{
private WellRef mBeginWell;
private WellRef mEndWell;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public WellRange(WellRef inBegin, WellRef inEnd)
{
mBeginWell = inBegin;
mEndWell = inEnd;
}
//---------------------------------------------------------------------------
public WellRange(String inBeginValue, String inEndValue)
{
this(new WellRef(inBeginValue), new WellRef(inEndValue));
}
//---------------------------------------------------------------------------
public WellRange(String inValue)
{
parse(inValue);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public WellRef getBeginWell()
{
return mBeginWell;
}
//---------------------------------------------------------------------------
public WellRef getEndWell()
{
return mEndWell;
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return mBeginWell + ":" + mEndWell;
}
//---------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
return (inObj2 != null
&& inObj2 instanceof WellRange
&& 0 == compareTo(inObj2));
}
//--------------------------------------------------------------------------
@Override
public int hashCode()
{
int hashCode = 0;
if (getBeginWell() != null)
{
hashCode = getBeginWell().hashCode();
}
if (getEndWell() != null)
{
hashCode += 31 * getEndWell().hashCode();
}
return hashCode;
}
//---------------------------------------------------------------------------
public int compareTo(Object inObj2)
{
int result = 1;
if (inObj2 instanceof WellRange)
{
WellRange cellRange2 = (WellRange) inObj2;
result = CompareUtil.compare(getBeginWell(), cellRange2.getBeginWell());
if (0 == result)
{
result = CompareUtil.compare(getEndWell(), cellRange2.getEndWell());
}
}
return result;
}
//---------------------------------------------------------------------------
public int size()
{
return (getEndWell().getRowIndex() - getBeginWell().getRowIndex() + 1) * (getEndWell().getColIndex() - getBeginWell().getColIndex() + 1);
}
//---------------------------------------------------------------------------
public boolean contains(WellRef inValue)
{
return (inValue != null
&& inValue.getColIndex() >= getBeginWell().getColIndex()
&& inValue.getColIndex() <= getEndWell().getColIndex()
&& inValue.getRowIndex() >= getBeginWell().getRowIndex()
&& inValue.getRowIndex() <= getEndWell().getRowIndex());
}
//---------------------------------------------------------------------------
public ListIterator wellRefIterator()
{
List wellRefs = new ArrayList<>(size());
for (int rowIdx = getBeginWell().getRowIndex(); rowIdx <= getEndWell().getRowIndex(); rowIdx++)
{
for (int colIdx = getBeginWell().getColIndex(); colIdx <= getEndWell().getColIndex(); colIdx++)
{
wellRefs.add(new WellRef().setRowIndex(rowIdx).setColIndex(colIdx));
}
}
return wellRefs.listIterator();
}
//---------------------------------------------------------------------------
public WellRef[] toArray()
{
List wellRefs = new ArrayList<>(size());
for (int rowIdx = getBeginWell().getRowIndex(); rowIdx <= getEndWell().getRowIndex(); rowIdx++)
{
for (int colIdx = getBeginWell().getColIndex(); colIdx <= getEndWell().getColIndex(); colIdx++)
{
wellRefs.add(new WellRef().setRowIndex(rowIdx).setColIndex(colIdx));
}
}
return wellRefs.toArray(new WellRef[] {});
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private void parse(String inValue)
{
if (StringUtil.isSet(inValue))
{
try
{
int colonIdx = inValue.indexOf(":");
mBeginWell = new WellRef(inValue.substring(0, colonIdx));
mEndWell = new WellRef(inValue.substring(colonIdx + 1));
}
catch (Exception e)
{
throw new RuntimeException("Problem parsing well range value " + StringUtil.singleQuote(inValue) + "!", e);
}
}
else
{
throw new RuntimeException("No value was specified for the well range!");
}
}
}