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

com.hfg.units.SubUnit Maven / Gradle / Ivy

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


import com.hfg.util.CompareUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 Container for compound units.
 
@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 SubUnit implements Comparable { private Unit mUnit; private SI_ScalingFactor mScalingFactor; private Integer mPow; //--------------------------------------------------------------------------- public SubUnit(Unit inUnit, SI_ScalingFactor inScalingFactor) { mUnit = inUnit; mScalingFactor = inScalingFactor; } //--------------------------------------------------------------------------- public SubUnit(Unit inUnit, SI_ScalingFactor inScalingFactor, Integer inPow) { mUnit = inUnit; mScalingFactor = inScalingFactor; mPow = inPow; } //--------------------------------------------------------------------------- public SubUnit(Unit inUnit, Integer inPow) { mUnit = inUnit; mPow = inPow; } //--------------------------------------------------------------------------- public SubUnit(Unit inUnit) { mUnit = inUnit; } //--------------------------------------------------------------------------- public Unit getUnit() { return mUnit; } //--------------------------------------------------------------------------- public Integer getPow() { return mPow; } //--------------------------------------------------------------------------- public SI_ScalingFactor getScalingFactor() { return mScalingFactor; } //--------------------------------------------------------------------------- @Override public String toString() { StringBuilderPlus buffer = new StringBuilderPlus(toStringMinusPow()); if (getPow() != null) { buffer.append(StringUtil.toSuperscript(getPow())); } return buffer.toString(); } //--------------------------------------------------------------------------- public String toStringMinusPow() { StringBuilderPlus buffer = new StringBuilderPlus(); if (getScalingFactor() != null) { buffer.append(getScalingFactor().getSymbol()); } buffer.append(mUnit.getAbbrev()); return buffer.toString(); } //--------------------------------------------------------------------------- /** * Test for equivalence. A SubUnit can also equal a Unit in some circumstances. * @param inObj2 the object to compare * @return whether or not the two objects are equivalent */ @Override public boolean equals(Object inObj2) { boolean result = false; if (inObj2 != null) { if (inObj2 instanceof SubUnit) { result = (0 == compareTo((SubUnit) inObj2)); } else if (inObj2 instanceof Unit) { Unit unit2 = (Unit) inObj2; if (unit2.hasSubUnits()) { if (1 == unit2.getSubUnits().size()) { result = (0 == compareTo((SubUnit) unit2.getSubUnits().get(0))); } } else if (null == getScalingFactor() && null == getPow() && getUnit().equals(unit2)) { result = true; } } } return result; } //--------------------------------------------------------------------------- @Override public int hashCode() { int hashCode = mUnit.hashCode(); if (getPow() != null) { hashCode += 31 * getPow().hashCode(); } if (getScalingFactor() != null) { hashCode += 31 * getScalingFactor().hashCode(); } return hashCode; } //--------------------------------------------------------------------------- public int compareTo(Object inObj2) { int result = -1; if (inObj2 != null) { if (inObj2 instanceof SubUnit) { SubUnit subUnit2 = (SubUnit) inObj2; result = CompareUtil.compare(getUnit(), subUnit2.getUnit()); if (0 == result) { result = CompareUtil.compare(getPow(), subUnit2.getPow()); } if (0 == result) { result = CompareUtil.compare(getScalingFactor(), subUnit2.getScalingFactor()); } } } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy