All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.chem.Atom Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.chem;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.hfg.util.CompareUtil;

//------------------------------------------------------------------------------
/**
 Represents an atom - an element plus coordinates.

 @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 Atom implements Comparable
{
   private Element mElement;
   private Float   mXCoordinate;
   private Float   mYCoordinate;
   private Float   mZCoordinate;

   private Integer   mCharge;
   private Boolean   mIsAromatic;
   private Integer   mHCount;
   private Integer   mImplicitHCount;
   private Integer   mAtomClass;
   private ChiralityOrder mChiralityOrder;
   private List mBonds;

   private final int mId = sIdSrc++;

   private static int sIdSrc = Integer.MIN_VALUE;

   public enum ChiralityOrder
   {
      clockwise,
      anticlockwise
   }

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public Atom(Element inElement)
   {
      mElement = inElement;
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      return mElement != null ? mElement.getSymbol() : "*";
   }

   //--------------------------------------------------------------------------
   @Override
   public int hashCode()
   {
      return mId;
   }

   //--------------------------------------------------------------------------
   @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 Atom)
      {
         result = CompareUtil.compare(mId, ((Atom) inObj).mId);
      }

      return result;
   }

   //--------------------------------------------------------------------------
   public Element getElement()
   {
      return mElement;
   }


   //--------------------------------------------------------------------------
   public Float getXCoordinate()
   {
      return mXCoordinate;
   }

   //--------------------------------------------------------------------------
   public Atom setXCoordinate(Float inValue)
   {
      mXCoordinate = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public Float getYCoordinate()
   {
      return mYCoordinate;
   }

   //--------------------------------------------------------------------------
   public Atom setYCoordinate(Float inValue)
   {
      mYCoordinate = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public Float getZCoordinate()
   {
      return mZCoordinate;
   }

   //--------------------------------------------------------------------------
   public Atom setZCoordinate(Float inValue)
   {
      mZCoordinate = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public Integer getCharge()
   {
      return mCharge;
   }

   //--------------------------------------------------------------------------
   public Atom setCharge(Integer inValue)
   {
      mCharge = inValue != null && inValue != 0 ? inValue : null;
      return this;
   }


   //--------------------------------------------------------------------------
   public Atom addBond(CovalentBond inValue)
   {
      if (inValue != null)
      {
         if (null == mBonds)
         {
            mBonds = new ArrayList<>(3);
         }
         else
         {
            // Has an equivalent bond already been added?
            for (CovalentBond bond : mBonds)
            {
               if (bond.equals(inValue)
                   && inValue.getSecondAtom() != null)
               {
                  throw new MolecularStructureException("There cannot be two bonds between one pair of Atoms!");
               }
            }
         }

         // Make sure that the bond involves this Atom
         if (inValue.getFirstAtom() != this
             && inValue.getSecondAtom() != this)
         {
            throw new MolecularStructureException("Can't add a bond to an Atom that doesn't involve the Atom!");
         }

         mBonds.add(inValue);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public boolean removeBond(CovalentBond inValue)
   {
      boolean result = false;
      if (mBonds != null)
      {
         result = mBonds.remove(inValue);
      }

      return result;
   }

   //--------------------------------------------------------------------------
   public List getBonds()
   {
      return mBonds != null ? Collections.unmodifiableList(mBonds) : null;
   }


   //--------------------------------------------------------------------------
   public ChiralityOrder getChiralityOrder()
   {
      return mChiralityOrder;
   }

   //--------------------------------------------------------------------------
   public Atom setChiralityOrder(ChiralityOrder inValue)
   {
      mChiralityOrder = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public Integer getAtomClass()
   {
      return mAtomClass;
   }

   //--------------------------------------------------------------------------
   public Atom setAtomClass(Integer inValue)
   {
      mAtomClass = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public Atom setIsAromatic(boolean inValue)
   {
      mIsAromatic = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public boolean isAromatic()
   {
      return mIsAromatic != null ? mIsAromatic : false;
   }


   //--------------------------------------------------------------------------
   public Atom setHCount(Integer inValue)
   {
      mHCount = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Integer getHCount()
   {
      return mHCount;
   }


   //--------------------------------------------------------------------------
   public Atom setImplicitHCount(Integer inValue)
   {
      mImplicitHCount = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Integer getImplicitHCount()
   {
      return mImplicitHCount;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy