com.hfg.bio.proteinproperty.SimpleProteinProperty 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.bio.proteinproperty;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.hfg.bio.seq.Protein;
import com.hfg.exception.ProgrammingException;
import com.hfg.util.DataType;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
* Abstract base Class for single value protein properties.
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg 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 SimpleProteinProperty implements SingleValueProteinProperty
{
private String mName;
private String mDescription;
private String mReference;
private static Map sUniqueMap = new HashMap<>();
private static boolean sInitialized = false;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
protected SimpleProteinProperty(String inName)
{
if (sUniqueMap.containsKey(inName))
{
throw new ProgrammingException("A SimpleProteinProperty already exists with name " + StringUtil.singleQuote(inName) + "!");
}
mName = inName;
sUniqueMap.put(inName, this);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
public abstract String getType();
//---------------------------------------------------------------------------
@Override
public String toString()
{
return name();
}
//---------------------------------------------------------------------------
public String name()
{
return mName;
}
//---------------------------------------------------------------------------
public SimpleProteinProperty setDescription(String inValue)
{
mDescription = inValue;
return this;
}
//---------------------------------------------------------------------------
public String getDescription()
{
return mDescription;
}
//---------------------------------------------------------------------------
public SimpleProteinProperty setReference(String inValue)
{
mReference = inValue;
return this;
}
//---------------------------------------------------------------------------
public String getReference()
{
return mReference;
}
//--------------------------------------------------------------------------
public DataType getDataType()
{
Class clazz = getReturnType();
DataType dataType = null;
if (clazz.equals(Integer.class))
{
dataType = DataType.INTEGER;
}
else if (clazz.equals(BigInteger.class)
|| clazz.equals(Long.class))
{
dataType = DataType.BIGINT;
}
else if (clazz.equals(Float.class))
{
dataType = DataType.FLOAT;
}
else if (clazz.equals(Double.class)
|| clazz.equals(BigDecimal.class))
{
dataType = DataType.DOUBLE;
}
else if (clazz.equals(String.class))
{
dataType = DataType.STRING;
}
return dataType;
}
//--------------------------------------------------------------------------
/**
Returns the Number data type returned by the calculate() method.
*/
// Unfortunately there isn't a simple way to just return T.class
public Class getReturnType()
{
Class dataType = null;
try
{
Method method = this.getClass().getMethod("calculate", new Class[] { Protein.class, SimpleProteinPropertyCalcSettings.class });
dataType = method.getReturnType();
}
catch (Exception e)
{
}
return dataType;
}
//---------------------------------------------------------------------------
public static SimpleProteinProperty valueOf(String inName)
{
ensureExtendingClassesAreInitialized();
return sUniqueMap.get(inName);
}
//---------------------------------------------------------------------------
public static Collection extends SimpleProteinProperty> values()
{
ensureExtendingClassesAreInitialized();
return sUniqueMap.values();
}
//---------------------------------------------------------------------------
// This is done to ensure that calls to values() and valueOf() have registered values to work with.
// The file /META-DATA/services/com.hfg.bio.proteinproperty.SimpleProteinPropery holds the names of
// the classes to be initialized.
// (Had to create a custom ServiceLoader that doesn't try to instantiate the properties but just
// calls Class.forName() to initialize the static definitions.)
private static void ensureExtendingClassesAreInitialized()
{
if (! sInitialized)
{
try
{
new ProteinPropertyServiceLoader().load();
}
catch (Exception e)
{
// Ignore
}
sInitialized = true;
}
}
}