001package com.hfg.units; 002 003import java.util.Map; 004import java.util.Set; 005 006import com.hfg.util.CompareUtil; 007import com.hfg.util.StringUtil; 008import com.hfg.util.collection.OrderedSet; 009 010//------------------------------------------------------------------------------ 011/** 012 Base class for systems of units. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public abstract class MeasurementSystem implements Comparable 039{ 040 private String mName; 041 private String mAbbrev; 042 043 public static final MeasurementSystem SI = SI_System.getInstance(); 044 public static final MeasurementSystem Metric = MetricSystem.getInstance(); 045 public static final MeasurementSystem BritishImperial = BritishImperialSystem.getInstance(); 046 047 //--------------------------------------------------------------------------- 048 protected MeasurementSystem(String inName, String inAbbrev) 049 { 050 mName = inName; 051 mAbbrev = inAbbrev; 052 } 053 054 //--------------------------------------------------------------------------- 055 @Override 056 public boolean equals(Object inObj2) 057 { 058 return (inObj2 != null 059 && inObj2 instanceof MeasurementSystem 060 && 0 == compareTo((MeasurementSystem) inObj2)); 061 } 062 063 //--------------------------------------------------------------------------- 064 @Override 065 public int hashCode() 066 { 067 int hashCode = name().hashCode(); 068 069 return hashCode; 070 } 071 072 //--------------------------------------------------------------------------- 073 public int compareTo(Object inObj2) 074 { 075 int result = -1; 076 if (inObj2 != null) 077 { 078 if (inObj2 instanceof MeasurementSystem) 079 { 080 MeasurementSystem system2 = (MeasurementSystem) inObj2; 081 082 result = CompareUtil.compare(name(), system2.name()); 083 } 084 } 085 086 return result; 087 } 088 089 //--------------------------------------------------------------------------- 090 public Unit getBaseUnit(QuantityType inQuantityType) 091 { 092 Unit baseUnit = getBaseUnitMap().get(inQuantityType); 093 094 if (null == baseUnit) 095 { 096 throw new RuntimeException("Unrecognized quantity " + StringUtil.singleQuote(inQuantityType) + "!"); 097 } 098 099 return baseUnit; 100 } 101 102 //--------------------------------------------------------------------------- 103 public Set<Unit> getBaseUnits() 104 { 105 return new OrderedSet<>(getBaseUnitMap().values()); 106 } 107 108 //--------------------------------------------------------------------------- 109 public String name() 110 { 111 return mName; 112 } 113 114 //--------------------------------------------------------------------------- 115 public String getAbbrev() 116 { 117 return mAbbrev; 118 } 119 120 //--------------------------------------------------------------------------- 121 @Override 122 public String toString() 123 { 124 return getAbbrev(); 125 } 126 127 protected abstract Map<QuantityType, Unit> getBaseUnitMap(); 128}