com.hfg.chem.OrganicMatterImpl 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.chem;
import java.util.Map;
import com.hfg.util.collection.CollectionUtil;
//------------------------------------------------------------------------------
/**
Elemental composition and mass tracking object.
@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 OrganicMatterImpl extends MatterImpl implements OrganicMatter, Cloneable
{
//##########################################################################
// PRIVATE FIELDS
//##########################################################################
private Double mOrganicAverageMass;
private boolean mOrganicAverageMassIsUserSet;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public OrganicMatterImpl()
{
super();
}
//--------------------------------------------------------------------------
public OrganicMatterImpl(Map inMap)
{
super(inMap);
}
//--------------------------------------------------------------------------
public OrganicMatterImpl(OrganicMatter inInitialValue)
{
super(inInitialValue);
if (inInitialValue != null)
{
setOrganicAverageMass(inInitialValue.getOrganicAverageMass());
}
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public boolean equals(Object inObj)
{
boolean result = false;
if (inObj != null
&& inObj instanceof OrganicMatterImpl)
{
result = (0 == compareTo((OrganicMatterImpl) inObj));
}
return result;
}
//--------------------------------------------------------------------------
@Override
public OrganicMatterImpl add(Matter inValue, int inCount)
{
if (inValue != null)
{
super.add(inValue, inCount);
if (mOrganicAverageMassIsUserSet
|| (null == inValue.getElementalComposition()
&& ((inValue instanceof OrganicMatter
&& ((OrganicMatter)inValue).getOrganicAverageMass() != null)
|| inValue.getAverageMass() != null)))
{
setOrganicAverageMass((mOrganicAverageMassIsUserSet ? mOrganicAverageMass : CollectionUtil.hasValues(getElementalComposition()) ? getOrganicAverageMass() : 0.0)
+ (inValue instanceof OrganicMatter ?
((((OrganicMatter)inValue).getOrganicAverageMass() != null ? ((OrganicMatter)inValue).getOrganicAverageMass() : inValue.getAverageMass()) * inCount) :
inValue.getAverageMass() * inCount));
}
}
return this;
}
//--------------------------------------------------------------------------
@Override
public OrganicMatterImpl remove(Matter inValue)
{
return remove(inValue, 1);
}
//--------------------------------------------------------------------------
@Override
public OrganicMatterImpl remove(Matter inValue, int inCount)
{
// This will call add() with a negative count
return (OrganicMatterImpl) super.remove(inValue, inCount);
}
//--------------------------------------------------------------------------
/**
If the elemental composition is known, use setElementalComposition() and
the masses will be derived automatically; this method is for use in those
(hopefully) rare times when the mass is known but not the elemental composition.
@param inValue the mass to use as the organic average mass for this object
@return this OrganicMatterImpl object to enable method chaining
*/
public OrganicMatterImpl setOrganicAverageMass(Double inValue)
{
mOrganicAverageMass = inValue;
mOrganicAverageMassIsUserSet = (inValue != null && calculateOrganicMassFromElementalComposition() != mOrganicAverageMass);
return this;
}
//--------------------------------------------------------------------------
public Double getOrganicAverageMass()
{
if (null == mOrganicAverageMass)
{
calculateMassFromElementalComposition();
}
return mOrganicAverageMass;
}
//--------------------------------------------------------------------------
@Override
public OrganicMatterImpl clone()
{
return (OrganicMatterImpl) super.clone();
}
//--------------------------------------------------------------------------
@Override
public void clearCalculatedProperties()
{
super.clearCalculatedProperties();
if (! mOrganicAverageMassIsUserSet) mOrganicAverageMass = null;
}
//##########################################################################
// PROTECTED METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
protected void calculateMassFromElementalComposition()
{
super.calculateMassFromElementalComposition();
if (! mOrganicAverageMassIsUserSet)
{
mOrganicAverageMass = calculateOrganicMassFromElementalComposition();
}
}
//--------------------------------------------------------------------------
@Override
protected boolean massesAreUserSet()
{
return (super.massesAreUserSet() || mOrganicAverageMassIsUserSet);
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private double calculateOrganicMassFromElementalComposition()
{
double organicAvg = 0.0;
Map elementalCompositionMap = getElementalComposition();
if (elementalCompositionMap != null)
{
for (Element element : elementalCompositionMap.keySet())
{
float count = elementalCompositionMap.get(element);
Double elementalAvgMass = element.getAverageMass();
Double elementalOrgAvgMass = element.getOrganicAverageMass();
organicAvg += count * (elementalOrgAvgMass != null ? elementalOrgAvgMass : elementalAvgMass != null ? elementalAvgMass : element.getMonoisotopicMass());
}
}
return organicAvg;
}
}