// $gleason: trafficmon/Converter.java,v 1.5 2003/06/26 21:33:15 clash Exp $ // Property of Joseph Gleason // Copyright 2003 package cc.glsn.trafficmon; import java.util.HashMap; import java.util.Map; import java.text.NumberFormat; public class Converter { private Map ConvMap; public Converter() { ConvMap=new HashMap(101,0.5f); String Key; Double CValue; Key=new String("B/s"); CValue=new Double(1.0); ConvMap.put(Key,CValue); Key=new String("kB/s"); CValue=new Double(1.0*1024.0); ConvMap.put(Key,CValue); Key=new String("KB/s"); CValue=new Double(1.0*1024.0); ConvMap.put(Key,CValue); Key=new String("mB/s"); CValue=new Double(1.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("MB/s"); CValue=new Double(1.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("gB/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("GB/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("tB/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("TB/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0*1024.0); ConvMap.put(Key,CValue); Key=new String("b/s"); CValue=new Double(1.0/8.0); ConvMap.put(Key,CValue); Key=new String("kb/s"); CValue=new Double(1.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("Kb/s"); CValue=new Double(1.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("mb/s"); CValue=new Double(1.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("Mb/s"); CValue=new Double(1.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("gb/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("Gb/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("tb/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); Key=new String("Tb/s"); CValue=new Double(1.0*1024.0*1024.0*1024.0*1024.0/8.0); ConvMap.put(Key,CValue); } public double Conv(double A, String S) { if (ConvMap.containsKey(S)) { Double Multiplyer=(Double)ConvMap.get(S); return A*Multiplyer.doubleValue(); } else { System.out.println("Unknown bandwidth measurement: " + S ); return 0.0; } } public double ConvTo(double A, String S) { if (ConvMap.containsKey(S)) { Double Multiplyer=(Double)ConvMap.get(S); return A/Multiplyer.doubleValue(); } else { System.out.println("Unknown bandwidth measurement: " + S ); return 0.0; } } public Map getConvMap() { return ConvMap; } public String ConvString(double B) { String S=""; NumberFormat NF=new java.text.DecimalFormat(); NF.setMinimumFractionDigits(2); NF.setMaximumFractionDigits(2); if (ConvTo(B,"kb/s") < 512.0) { S+="" + NF.format(ConvTo(B,"kb/s")) + " kb/s"; return S; } if (ConvTo(B,"mb/s") < 512.0) { S+="" + NF.format(ConvTo(B,"mb/s")) + " mb/s"; return S; } if (ConvTo(B,"gb/s") < 512.0) { S+="" + NF.format(ConvTo(B,"gb/s")) + " gb/s"; return S; } S+="" + NF.format(ConvTo(B,"tb/s")) + " tb/s"; return S; } }