com.hfg.automation.WellRef 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.regex.Matcher;
import java.util.regex.Pattern;
import com.hfg.exception.InvalidValueException;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Represents a well location on a plate (ex: 'B11').
@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 WellRef implements Comparable
{
private Integer mColIndex; // 1-based column index
private Integer mRowIndex; // 1-based row index
private String mRefString;
private static final Pattern WELL_REF_PATTERN = Pattern.compile("([A-Z]{1,3})(\\d+)");
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public WellRef()
{
}
//---------------------------------------------------------------------------
public WellRef(String inValue)
{
if (StringUtil.isSet(inValue))
{
mRefString = inValue.toUpperCase().trim();
Matcher m = WELL_REF_PATTERN.matcher(mRefString);
if (! m.matches())
{
throw new InvalidValueException("The well reference " + StringUtil.singleQuote(inValue) + " is not in a valid format!");
}
mRowIndex = 0;
int colPosition = m.group(1).length();
for (char colChar : m.group(1).toCharArray())
{
mRowIndex += (int) (Math.pow(26, colPosition - 1)) * ((int) colChar - 64);
colPosition--;
}
mColIndex = Integer.parseInt(m.group(2));
mRefString = calcRefString();
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
@Override
public String toString()
{
return mRefString;
}
//---------------------------------------------------------------------------
/**
Specifies the column index as a 1-based number.
*/
public WellRef setColIndex(int inValue)
{
if (inValue <= 0)
{
throw new InvalidValueException("The column index must be a 1-based positive value!");
}
mColIndex = inValue;
mRefString = calcRefString();
return this;
}
//---------------------------------------------------------------------------
/**
Returns the column index as a 1-based number.
*/
public Integer getColIndex()
{
return mColIndex;
}
//---------------------------------------------------------------------------
/**
Specifies the row index as a 1-based number.
*/
public WellRef setRowIndex(int inValue)
{
if (inValue <= 0)
{
throw new InvalidValueException("The row index must be a 1-based positive value!");
}
mRowIndex = inValue;
mRefString = calcRefString();
return this;
}
//---------------------------------------------------------------------------
/**
Returns the row index as a 1-based number.
*/
public Integer getRowIndex()
{
return mRowIndex;
}
//---------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
return (inObj2 != null
&& inObj2 instanceof WellRef
&& 0 == compareTo(inObj2));
}
//--------------------------------------------------------------------------
@Override
public int hashCode()
{
int hashCode = 0;
if (getColIndex() != null)
{
hashCode = getColIndex();
}
if (getRowIndex() != null)
{
hashCode += 31 * getRowIndex();
}
return hashCode;
}
//---------------------------------------------------------------------------
public int compareTo(Object inObj2)
{
int result = 1;
if (inObj2 instanceof WellRef)
{
WellRef wellRef2 = (WellRef) inObj2;
result = CompareUtil.compare(getColIndex(), wellRef2.getColIndex());
if (0 == result)
{
result = CompareUtil.compare(getRowIndex(), wellRef2.getRowIndex());
}
}
return result;
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private String calcRefString()
{
String refString = null;
if (mColIndex != null
&& mRowIndex != null)
{
refString = String.format("%s%02d", rowIndexToString(mRowIndex), mColIndex);
}
return refString;
}
//---------------------------------------------------------------------------
// Recursively turns a row index into an A-Z string reference.
private static String rowIndexToString(int inColIndex)
{
int baseValue = (int) 'A';
int zeroBasedColIndex = inColIndex - 1;
String refString = "";
if (inColIndex > 26)
{
refString = rowIndexToString(zeroBasedColIndex / 26) ;
}
return refString + (char) (baseValue + (zeroBasedColIndex % 26) );
}
}