01: package com.calipso.reportgenerator.reportcalculator;
02:
03: import java.util.*;
04: import java.io.Serializable;
05:
06: /**
07: * Implementación de un Flyweight de String, para hacer mejor uso de la memoria
08: */
09:
10: public class SharedString extends SharedData implements Serializable,
11: Comparable, CubeValue {
12: private final String value;
13: // Se sugería un número primo para el tamaño
14: static final private HashMap strings = new HashMap(5003, 0.80f);
15:
16: public SharedString(String aValue) {
17: value = aValue;
18: strings.put(aValue, this );
19: }
20:
21: static public SharedString newFrom(String aValue) {
22: Object string;
23:
24: string = strings.get(aValue);
25: if (string == null) {
26: return new SharedString(aValue);
27: } else {
28: return (SharedString) string;
29: }
30: }
31:
32: public String value() {
33: return value;
34: }
35:
36: public boolean equals(Object o) {
37: final String string;
38: if (this == o)
39: return true;
40:
41: if (o instanceof String) {
42: string = (String) o;
43: } else {
44: string = ((SharedString) o).value;
45: }
46: if (!value.equals(string))
47: return false;
48: return true;
49: }
50:
51: public int hashCode() {
52: return value.hashCode();
53: }
54:
55: public String toString() {
56: return value;
57: }
58:
59: public int compareTo(Object o) {
60: if ((toString() == null) && (o.toString() == null)) {
61: return 0;
62: } else if (toString() == null) {
63: return -1;
64: } else if ((o.toString() == null)) {
65: return 1;
66: } else {
67: return toString().compareTo(o.toString());
68: }
69: }
70:
71: public Object getValue() {
72: return value;
73: }
74:
75: }
|