001package com.hfg.units; 002 003 004import com.hfg.util.CompareUtil; 005import com.hfg.util.StringBuilderPlus; 006import com.hfg.util.StringUtil; 007 008//------------------------------------------------------------------------------ 009/** 010 Container for compound units. 011 <div> 012 @author J. Alex Taylor, hairyfatguy.com 013 </div> 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class SubUnit implements Comparable 037{ 038 private Unit mUnit; 039 private SI_ScalingFactor mScalingFactor; 040 private Integer mPow; 041 042 043 //--------------------------------------------------------------------------- 044 public SubUnit(Unit inUnit, SI_ScalingFactor inScalingFactor) 045 { 046 mUnit = inUnit; 047 mScalingFactor = inScalingFactor; 048 } 049 050 //--------------------------------------------------------------------------- 051 public SubUnit(Unit inUnit, SI_ScalingFactor inScalingFactor, Integer inPow) 052 { 053 mUnit = inUnit; 054 mScalingFactor = inScalingFactor; 055 mPow = inPow; 056 } 057 058 //--------------------------------------------------------------------------- 059 public SubUnit(Unit inUnit, Integer inPow) 060 { 061 mUnit = inUnit; 062 mPow = inPow; 063 } 064 065 //--------------------------------------------------------------------------- 066 public SubUnit(Unit inUnit) 067 { 068 mUnit = inUnit; 069 } 070 071 072 //--------------------------------------------------------------------------- 073 public Unit getUnit() 074 { 075 return mUnit; 076 } 077 078 //--------------------------------------------------------------------------- 079 public Integer getPow() 080 { 081 return mPow; 082 } 083 084 //--------------------------------------------------------------------------- 085 public SI_ScalingFactor getScalingFactor() 086 { 087 return mScalingFactor; 088 } 089 090 //--------------------------------------------------------------------------- 091 @Override 092 public String toString() 093 { 094 StringBuilderPlus buffer = new StringBuilderPlus(toStringMinusPow()); 095 096 if (getPow() != null) 097 { 098 buffer.append(StringUtil.toSuperscript(getPow())); 099 } 100 101 return buffer.toString(); 102 } 103 104 //--------------------------------------------------------------------------- 105 public String toStringMinusPow() 106 { 107 StringBuilderPlus buffer = new StringBuilderPlus(); 108 109 if (getScalingFactor() != null) 110 { 111 buffer.append(getScalingFactor().getSymbol()); 112 } 113 114 buffer.append(mUnit.getAbbrev()); 115 116 return buffer.toString(); 117 } 118 119 //--------------------------------------------------------------------------- 120 /** 121 * Test for equivalence. A SubUnit can also equal a Unit in some circumstances. 122 * @param inObj2 the object to compare 123 * @return whether or not the two objects are equivalent 124 */ 125 @Override 126 public boolean equals(Object inObj2) 127 { 128 boolean result = false; 129 if (inObj2 != null) 130 { 131 if (inObj2 instanceof SubUnit) 132 { 133 result = (0 == compareTo((SubUnit) inObj2)); 134 } 135 else if (inObj2 instanceof Unit) 136 { 137 Unit unit2 = (Unit) inObj2; 138 if (mUnit != null 139 && mUnit.equals(unit2) 140 && null == getScalingFactor() 141 && null == getPow()) 142 { 143 result = true; 144 } 145 else if (unit2.hasSubUnits()) 146 { 147 if (1 == unit2.getSubUnits().size()) 148 { 149 result = (0 == compareTo((SubUnit) unit2.getSubUnits().get(0))); 150 } 151 } 152 else if (null == getScalingFactor() 153 && null == getPow() 154 && getUnit().equals(unit2)) 155 { 156 result = true; 157 } 158 } 159 } 160 161 return result; 162 } 163 164 //--------------------------------------------------------------------------- 165 @Override 166 public int hashCode() 167 { 168 int hashCode = mUnit.hashCode(); 169 170 if (getPow() != null) 171 { 172 hashCode += 31 * getPow().hashCode(); 173 } 174 175 if (getScalingFactor() != null) 176 { 177 hashCode += 31 * getScalingFactor().hashCode(); 178 } 179 180 return hashCode; 181 } 182 183 //--------------------------------------------------------------------------- 184 public int compareTo(Object inObj2) 185 { 186 int result = -1; 187 if (inObj2 != null) 188 { 189 if (inObj2 instanceof SubUnit) 190 { 191 SubUnit subUnit2 = (SubUnit) inObj2; 192 193 result = CompareUtil.compare(getUnit(), subUnit2.getUnit()); 194 195 if (0 == result) 196 { 197 result = CompareUtil.compare(getPow(), subUnit2.getPow()); 198 } 199 200 if (0 == result) 201 { 202 result = CompareUtil.compare(getScalingFactor(), subUnit2.getScalingFactor()); 203 } 204 } 205 } 206 207 return result; 208 } 209 210}