1 package com.calipso.reportgenerator.reportcalculator; 2 3 import java.util.HashMap ; 4 import java.util.Collections ; 5 import java.io.Serializable ; 6 import java.text.DecimalFormatSymbols ; 7 import java.text.DecimalFormat ; 8 9 12 13 public class SharedFloat extends SharedData implements Serializable , Comparable , CubeValue { 14 15 private Float value; 16 static final private HashMap floats = new HashMap (5003, 0.80f); 17 18 private SharedFloat(Float value) { 19 this.value = value; 20 floats.put(value, this); 21 } 22 23 24 public static SharedFloat newFrom(Float aFloat) { 25 Object o = floats.get(aFloat); 26 if(o == null) { 27 return new SharedFloat(aFloat); 28 } else { 29 return (SharedFloat) o; 30 } 31 } 32 33 public static SharedFloat newFrom(float theFloat) { 34 Float aFloat = new Float (theFloat); 35 return newFrom(aFloat); 36 } 37 38 public float floatValue() { 39 return value.floatValue(); 40 } 41 42 public String toString() { 43 return value.toString(); 44 } 45 46 public int compareTo(Object o) { 47 if(o instanceof Float ){ 48 return value.compareTo((Float )o); 49 }else if(o instanceof SharedFloat){ 50 return value.compareTo(((SharedFloat)o).value); 51 }else{ 52 return -1; 53 } 54 } 55 56 public Object getValue() { 57 return value; 58 } 59 60 public SharedFloat add(float aValue) { 61 return SharedFloat.newFrom(floatValue() + aValue); 62 } 63 64 public SharedFloat add(Float aValue) { 65 return SharedFloat.newFrom(floatValue() + aValue.floatValue()); 66 } 67 68 public SharedFloat add(SharedFloat aValue) { 69 return SharedFloat.newFrom(floatValue() + aValue.floatValue()); 70 } 71 72 77 public SharedFloat div(SharedFloat aFloat) { 78 return SharedFloat.newFrom(this.value.floatValue() / aFloat.value.floatValue()); 79 } 80 81 public String format(String decimalPattern,String decimalSeparator,String groupingSeparator) 82 { 83 if (groupingSeparator.equals("")) 84 { 85 if (decimalSeparator.equals(".")) 86 { 87 groupingSeparator = ","; 88 }else 89 { 90 groupingSeparator = "."; 91 } 92 } 93 DecimalFormatSymbols dfs = new DecimalFormatSymbols (); 94 dfs.setDecimalSeparator(decimalSeparator.charAt(0)); 95 dfs.setGroupingSeparator(groupingSeparator.charAt(0)); 96 return new DecimalFormat (decimalPattern,dfs).format(getValue()); 97 } 98 99 } 100 | Popular Tags |