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

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

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


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

import com.hfg.util.CompareUtil;

//------------------------------------------------------------------------------
/**
 * Enumerated charge types.
 * @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 Charge implements Comparable
{
   private int    mValue;
   private int    mIndex;

   private static List sList = new ArrayList<>(3);

   public static final Charge POSITIVE = new Charge(1);
   public static final Charge NEUTRAL  = new Charge(0);
   public static final Charge NEGATIVE = new Charge(-1);

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

   //---------------------------------------------------------------------------
   private Charge(int inValue)
   {
      mValue = inValue;
      mIndex = sList.size();
      sList.add(this);
   }

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

   //---------------------------------------------------------------------------
   public static Charge valueOf(String inString)
   {
      Charge value = null;
      if (inString != null)
      {
         if (inString.equals("1")
             || inString.equalsIgnoreCase("POSITIVE")
             || inString.equals("+"))
         {
            value = POSITIVE;
         }
         else if (inString.equals("0")
             || inString.equalsIgnoreCase("NEUTRAL"))
         {
            value = NEUTRAL;
         }
         else if (inString.equals("-1")
             || inString.equalsIgnoreCase("NEGATIVE")
             || inString.equals("-"))
         {
            value = NEGATIVE;
         }
         else
         {
            throw new RuntimeException("Cannot convert the String '" + inString + "' to a Charge obj!");
         }
      }

      return value;
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean equals(Object inObj2)
   {
      boolean result = false;

      if (inObj2 != null
            && inObj2 instanceof Charge)
      {
         result = (0 == compareTo((Charge) inObj2));
      }

      return result;
   }

   //--------------------------------------------------------------------------
   @Override
   public int compareTo(Charge inObj2)
   {
      int result = -1;

      if (inObj2 != null)
      {
         result = 0;

         if (this != inObj2)
         {
            result = CompareUtil.compare(mValue, inObj2.mValue);
         }
      }

      return result;
   }

   //---------------------------------------------------------------------------
   @Override
   public final String toString()
   {
      return mValue + "";
   }
   
   //--------------------------------------------------------------------------
   @Override
   public final int hashCode()
   {
      return mIndex;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy