01: package com.calipso.reportgenerator.reportcalculator;
02:
03: import java.util.HashMap;
04: import java.util.Collections;
05: import java.io.Serializable;
06: import java.text.DecimalFormatSymbols;
07: import java.text.DecimalFormat;
08:
09: /**
10: * Administra las distintias instancias de Float
11: */
12:
13: public class SharedFloat extends SharedData implements Serializable,
14: Comparable, CubeValue {
15:
16: private Float value;
17: static final private HashMap floats = new HashMap(5003, 0.80f);
18:
19: private SharedFloat(Float value) {
20: this .value = value;
21: floats.put(value, this );
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: /**
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()
79: / aFloat.value.floatValue());
80: }
81:
82: public String format(String decimalPattern,
83: String decimalSeparator, String groupingSeparator) {
84: if (groupingSeparator.equals("")) {
85: if (decimalSeparator.equals(".")) {
86: groupingSeparator = ",";
87: } else {
88: groupingSeparator = ".";
89: }
90: }
91: DecimalFormatSymbols dfs = new DecimalFormatSymbols();
92: dfs.setDecimalSeparator(decimalSeparator.charAt(0));
93: dfs.setGroupingSeparator(groupingSeparator.charAt(0));
94: return new DecimalFormat(decimalPattern, dfs)
95: .format(getValue());
96: }
97:
98: }
|