001package com.hfg.chem; 002 003 004import java.util.ArrayList; 005import java.util.List; 006 007import com.hfg.util.CompareUtil; 008 009//------------------------------------------------------------------------------ 010/** 011 * Enumerated charge types. 012 * @author J. Alex Taylor, hairyfatguy.com 013 */ 014//------------------------------------------------------------------------------ 015// com.hfg XML/HTML Coding Library 016// 017// This library is free software; you can redistribute it and/or 018// modify it under the terms of the GNU Lesser General Public 019// License as published by the Free Software Foundation; either 020// version 2.1 of the License, or (at your option) any later version. 021// 022// This library is distributed in the hope that it will be useful, 023// but WITHOUT ANY WARRANTY; without even the implied warranty of 024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025// Lesser General Public License for more details. 026// 027// You should have received a copy of the GNU Lesser General Public 028// License along with this library; if not, write to the Free Software 029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030// 031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 032// jataylor@hairyfatguy.com 033//------------------------------------------------------------------------------ 034 035public class Charge implements Comparable<Charge> 036{ 037 private int mValue; 038 private int mIndex; 039 040 private static List<Charge> sList = new ArrayList<>(3); 041 042 public static final Charge POSITIVE = new Charge(1); 043 public static final Charge NEUTRAL = new Charge(0); 044 public static final Charge NEGATIVE = new Charge(-1); 045 046 //########################################################################### 047 // CONSTRUCTORS 048 //########################################################################### 049 050 //--------------------------------------------------------------------------- 051 private Charge(int inValue) 052 { 053 mValue = inValue; 054 mIndex = sList.size(); 055 sList.add(this); 056 } 057 058 //########################################################################### 059 // PUBLIC METHODS 060 //########################################################################### 061 062 //--------------------------------------------------------------------------- 063 public static Charge valueOf(String inString) 064 { 065 Charge value = null; 066 if (inString != null) 067 { 068 if (inString.equals("1") 069 || inString.equalsIgnoreCase("POSITIVE") 070 || inString.equals("+")) 071 { 072 value = POSITIVE; 073 } 074 else if (inString.equals("0") 075 || inString.equalsIgnoreCase("NEUTRAL")) 076 { 077 value = NEUTRAL; 078 } 079 else if (inString.equals("-1") 080 || inString.equalsIgnoreCase("NEGATIVE") 081 || inString.equals("-")) 082 { 083 value = NEGATIVE; 084 } 085 else 086 { 087 throw new RuntimeException("Cannot convert the String '" + inString + "' to a Charge obj!"); 088 } 089 } 090 091 return value; 092 } 093 094 //-------------------------------------------------------------------------- 095 @Override 096 public boolean equals(Object inObj2) 097 { 098 boolean result = false; 099 100 if (inObj2 != null 101 && inObj2 instanceof Charge) 102 { 103 result = (0 == compareTo((Charge) inObj2)); 104 } 105 106 return result; 107 } 108 109 //-------------------------------------------------------------------------- 110 @Override 111 public int compareTo(Charge inObj2) 112 { 113 int result = -1; 114 115 if (inObj2 != null) 116 { 117 result = 0; 118 119 if (this != inObj2) 120 { 121 result = CompareUtil.compare(mValue, inObj2.mValue); 122 } 123 } 124 125 return result; 126 } 127 128 //--------------------------------------------------------------------------- 129 @Override 130 public final String toString() 131 { 132 return mValue + ""; 133 } 134 135 //-------------------------------------------------------------------------- 136 @Override 137 public final int hashCode() 138 { 139 return mIndex; 140 } 141 142}