KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportcalculator > SharedFloat


1 package com.calipso.reportgenerator.reportcalculator;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.io.Serializable JavaDoc;
6 import java.text.DecimalFormatSymbols JavaDoc;
7 import java.text.DecimalFormat JavaDoc;
8
9 /**
10  * Administra las distintias instancias de Float
11  */

12
13 public class SharedFloat extends SharedData implements Serializable JavaDoc, Comparable JavaDoc, CubeValue {
14
15   private Float JavaDoc value;
16   static final private HashMap JavaDoc floats = new HashMap JavaDoc(5003, 0.80f);
17
18   private SharedFloat(Float JavaDoc value) {
19     this.value = value;
20     floats.put(value, this);
21   }
22
23
24   public static SharedFloat newFrom(Float JavaDoc aFloat) {
25     Object JavaDoc 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 JavaDoc aFloat = new Float JavaDoc(theFloat);
35     return newFrom(aFloat);
36   }
37
38   public float floatValue() {
39     return value.floatValue();
40   }
41
42   public String JavaDoc toString() {
43     return value.toString();
44   }
45
46   public int compareTo(Object JavaDoc o) {
47     if(o instanceof Float JavaDoc){
48       return value.compareTo((Float JavaDoc)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 JavaDoc 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 JavaDoc 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   /**
73    * Retorna un nuevo shared float que surge de la division del valor almacenado por el valor pasado por parametro.
74    * @param aFloat
75    * @return shared float
76    */

77   public SharedFloat div(SharedFloat aFloat) {
78       return SharedFloat.newFrom(this.value.floatValue() / aFloat.value.floatValue());
79   }
80
81   public String JavaDoc format(String JavaDoc decimalPattern,String JavaDoc decimalSeparator,String JavaDoc groupingSeparator)
82   {
83     if (groupingSeparator.equals(""))
84     {
85       if (decimalSeparator.equals("."))
86       {
87         groupingSeparator = ",";
88       }else
89       {
90         groupingSeparator = ".";
91       }
92     }
93     DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc();
94     dfs.setDecimalSeparator(decimalSeparator.charAt(0));
95     dfs.setGroupingSeparator(groupingSeparator.charAt(0));
96     return new DecimalFormat JavaDoc(decimalPattern,dfs).format(getValue());
97   }
98   
99 }
100
Popular Tags