com.hfg.chem.CovalentBond 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 com.hfg.util.CompareUtil;
import com.hfg.util.StringBuilderPlus;
//------------------------------------------------------------------------------
/**
Represents a covalent bond between atoms.
@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 CovalentBond implements Comparable
{
private Atom mFirstAtom;
private Integer mBondOrder; // i.e: 1, 2, 3, or 4
private Atom mSecondAtom;
// Indicates if this bond is part of an aromatic ring
private Boolean mAromatic;
// Used for indicating the configuration of atoms around double bonds (cis/trans).
private Boolean mUp;
private Boolean mDown;
private Atom[] mSortedAtoms = new Atom[2];
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public CovalentBond(Atom inAtom1, int inBondOrder)
{
this(inAtom1, inBondOrder, null);
}
//--------------------------------------------------------------------------
public CovalentBond(Atom inAtom1, Atom inAtom2)
{
this(inAtom1, null, inAtom2);
}
//--------------------------------------------------------------------------
public CovalentBond(Atom inAtom1, Integer inBondOrder, Atom inAtom2)
{
mFirstAtom = inAtom1;
mBondOrder = inBondOrder;
mSecondAtom = inAtom2;
orderAtoms();
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public String toString()
{
StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" ");
if (mFirstAtom != null)
{
buffer.append(mFirstAtom);
}
if (mBondOrder != null)
{
char bondChar;
switch (mBondOrder)
{
case 1:
bondChar = '-';
break;
case 2:
bondChar = '=';
break;
case 3:
bondChar = '≡';
break;
default:
bondChar = mBondOrder.toString().charAt(0);
}
buffer.delimitedAppend(bondChar);
}
if (mSecondAtom != null)
{
buffer.delimitedAppend(mSecondAtom);
}
return buffer.toString();
}
//--------------------------------------------------------------------------
@Override
public int hashCode()
{
int hashcode = 31;
if (mFirstAtom != null)
{
hashcode += mFirstAtom.hashCode();
}
if (mSecondAtom != null)
{
hashcode += mSecondAtom.hashCode();
}
return hashcode;
}
//--------------------------------------------------------------------------
@Override
public boolean equals(Object inObj)
{
boolean result = false;
if (inObj != null)
{
result = (0 == compareTo(inObj));
}
return result;
}
//--------------------------------------------------------------------------
@Override
public int compareTo(Object inObj)
{
int result = -1;
if (inObj instanceof CovalentBond)
{
// The order of the atoms shouldn't matter for comparison purposes
// so we'll use our sorted array for the comparison for consistency.
result = CompareUtil.compare(mSortedAtoms[0], ((CovalentBond) inObj).mSortedAtoms[0]);
if (0 == result)
{
result = CompareUtil.compare(mSortedAtoms[1], ((CovalentBond) inObj).mSortedAtoms[1]);
}
}
return result;
}
//--------------------------------------------------------------------------
public Atom getFirstAtom()
{
return mFirstAtom;
}
//--------------------------------------------------------------------------
public Integer getSpecifiedBondOrder()
{
return mBondOrder;
}
//--------------------------------------------------------------------------
public CovalentBond setBondOrder(Integer inValue)
{
mBondOrder = inValue;
return this;
}
//--------------------------------------------------------------------------
public int getBondOrder()
{
return mBondOrder != null ? mBondOrder : 1;
}
//--------------------------------------------------------------------------
public CovalentBond setSecondAtom(Atom inValue)
{
mSecondAtom = inValue;
orderAtoms();
return this;
}
//--------------------------------------------------------------------------
public Atom getSecondAtom()
{
return mSecondAtom;
}
//--------------------------------------------------------------------------
public CovalentBond setIsAromatic()
{
mAromatic = true;
return this;
}
//--------------------------------------------------------------------------
public boolean isAromatic()
{
return mAromatic;
}
//--------------------------------------------------------------------------
public CovalentBond setIsUp()
{
mUp = true;
return this;
}
//--------------------------------------------------------------------------
public boolean isUp()
{
return mUp;
}
//--------------------------------------------------------------------------
public CovalentBond setIsDown()
{
mDown = true;
return this;
}
//--------------------------------------------------------------------------
public boolean isDown()
{
return mDown;
}
//--------------------------------------------------------------------------
public boolean eq()
{
return mDown;
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//--------------------------------------------------------------------------
private void orderAtoms()
{
if (CompareUtil.compare(mFirstAtom, mSecondAtom) < 0)
{
mSortedAtoms[0] = mFirstAtom;
mSortedAtoms[1] = mSecondAtom;
}
else
{
mSortedAtoms[0] = mSecondAtom;
mSortedAtoms[1] = mFirstAtom;
}
}
}