001package com.hfg.units; 002 003 004import java.util.Arrays; 005import java.util.HashSet; 006import java.util.Map; 007import java.util.Set; 008 009import com.hfg.util.CompareUtil; 010import com.hfg.util.StringUtil; 011import com.hfg.util.collection.OrderedMap; 012 013//------------------------------------------------------------------------------ 014/** 015 Standard SI / Metric scaling factors. 016 <div> 017 @author J. Alex Taylor, hairyfatguy.com 018 </div> 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041public class SI_ScalingFactor implements Comparable 042{ 043 private static Map<String, SI_ScalingFactor> sInstanceMap = new OrderedMap<>(); 044 private static Set<SI_ScalingFactor> sInstances = new HashSet<>(); 045 046 private String mPrefix; 047 private String mSymbol; 048 private String[] mAlternateSymbols; 049 private int mNumAlternateSymbols; 050 private double mScalingFactor; 051 052 public static final SI_ScalingFactor yocto = new SI_ScalingFactor("yocto", "y", 1E-24); 053 public static final SI_ScalingFactor zepto = new SI_ScalingFactor("zepto", "z", 1E-21); 054 public static final SI_ScalingFactor atto = new SI_ScalingFactor("atto", "a", 1E-18); 055 public static final SI_ScalingFactor femto = new SI_ScalingFactor("femto", "f", 1E-15); 056 public static final SI_ScalingFactor pico = new SI_ScalingFactor("pico", "p", 1E-12); 057 public static final SI_ScalingFactor nano = new SI_ScalingFactor("nano", "n", 1E-9); 058 public static final SI_ScalingFactor micro = new SI_ScalingFactor("micro", "μ", 1E-6).addAlternateSymbol("µ") // There are 2 different unicode characters for 'µ' 059 .addAlternateSymbol("u"); // For lazy people 060 public static final SI_ScalingFactor milli = new SI_ScalingFactor("milli", "m", 1E-3); 061 public static final SI_ScalingFactor centi = new SI_ScalingFactor("centi", "c", 1E-2); 062 public static final SI_ScalingFactor deci = new SI_ScalingFactor("deci", "d", 1E-1); 063 064 public static final SI_ScalingFactor one = new SI_ScalingFactor("", "", 1); 065 066 public static final SI_ScalingFactor deca = new SI_ScalingFactor("deca", "da", 10); 067 public static final SI_ScalingFactor hecto = new SI_ScalingFactor("hecto", "h", 1E2); 068 public static final SI_ScalingFactor kilo = new SI_ScalingFactor("kilo", "k", 1E3); 069 public static final SI_ScalingFactor mega = new SI_ScalingFactor("mega", "M", 1E6); 070 public static final SI_ScalingFactor giga = new SI_ScalingFactor("giga", "G", 1E9); 071 public static final SI_ScalingFactor tera = new SI_ScalingFactor("tera", "T", 1E12); 072 public static final SI_ScalingFactor peta = new SI_ScalingFactor("peta", "P", 1E15); 073 public static final SI_ScalingFactor exa = new SI_ScalingFactor("exa", "E", 1E18); 074 public static final SI_ScalingFactor zetta = new SI_ScalingFactor("zetta", "Z", 1E21); 075 public static final SI_ScalingFactor yotta = new SI_ScalingFactor("yotta", "Y", 1E24); 076 077 //--------------------------------------------------------------------------- 078 private SI_ScalingFactor(String inPrefix, String inSymbol, double inScalingFactor) 079 { 080 mPrefix = inPrefix; 081 mSymbol = inSymbol; 082 mScalingFactor = inScalingFactor; 083 084 sInstances.add(this); 085 sInstanceMap.put(mSymbol, this); 086 sInstanceMap.put(mPrefix, this); 087 } 088 089 //--------------------------------------------------------------------------- 090 public static SI_ScalingFactor valueOf(String inString) 091 { 092 return sInstanceMap.get(inString); 093 } 094 095 //--------------------------------------------------------------------------- 096 public static SI_ScalingFactor[] values() 097 { 098 return sInstances.toArray(new SI_ScalingFactor[] {}); 099 } 100 101 //--------------------------------------------------------------------------- 102 public String getPrefix() 103 { 104 return mPrefix; 105 } 106 107 //--------------------------------------------------------------------------- 108 public String getSymbol() 109 { 110 return mSymbol; 111 } 112 113 //--------------------------------------------------------------------------- 114 private SI_ScalingFactor addAlternateSymbol(String inValue) 115 { 116 if (null == mAlternateSymbols) 117 { 118 mAlternateSymbols = new String[3]; 119 } 120 121 mAlternateSymbols[mNumAlternateSymbols++] = inValue; 122 123 sInstanceMap.put(inValue, this); 124 125 return this; 126 } 127 128 //--------------------------------------------------------------------------- 129 public String[] getAlternateSymbols() 130 { 131 return Arrays.copyOf(mAlternateSymbols, mNumAlternateSymbols); 132 } 133 134 //--------------------------------------------------------------------------- 135 public double getScalingFactor() 136 { 137 return mScalingFactor; 138 } 139 140 //--------------------------------------------------------------------------- 141 public BaseSIUnitConverter getBaseSIUnitConverter() 142 { 143 return new BaseSIUnitConverter(getScalingFactor()); 144 } 145 146 //--------------------------------------------------------------------------- 147 @Override 148 public String toString() 149 { 150 String prefix = getPrefix(); 151 return StringUtil.isSet(prefix) ? prefix : "" + (int) getScalingFactor(); 152 } 153 154 155 //--------------------------------------------------------------------------- 156 @Override 157 public boolean equals(Object inObj2) 158 { 159 return (inObj2 != null 160 && inObj2 instanceof SI_ScalingFactor 161 && 0 == compareTo((SI_ScalingFactor) inObj2)); 162 } 163 164 //--------------------------------------------------------------------------- 165 @Override 166 public int hashCode() 167 { 168 int hashCode = getPrefix().hashCode(); 169 170 hashCode += 31 * getSymbol().hashCode(); 171 hashCode += 31 * getScalingFactor(); 172 173 return hashCode; 174 } 175 176 //--------------------------------------------------------------------------- 177 public int compareTo(Object inObj2) 178 { 179 int result = -1; 180 if (inObj2 != null) 181 { 182 if (inObj2 instanceof SI_ScalingFactor) 183 { 184 SI_ScalingFactor scalingFactor2 = (SI_ScalingFactor) inObj2; 185 186 result = CompareUtil.compare(getPrefix(), scalingFactor2.getPrefix()); 187 188 if (0 == result) 189 { 190 result = CompareUtil.compare(getSymbol(), scalingFactor2.getSymbol()); 191 } 192 193 if (0 == result) 194 { 195 result = CompareUtil.compare(getScalingFactor(), scalingFactor2.getScalingFactor()); 196 } 197 } 198 } 199 200 return result; 201 } 202 203}