KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.reportgenerator.reportcalculator;
2
3 import java.util.*;
4 import java.io.Serializable JavaDoc;
5
6 /**
7  * Implementación de un Flyweight de String, para hacer mejor uso de la memoria
8  */

9
10 public class SharedString extends SharedData implements Serializable JavaDoc, Comparable JavaDoc, CubeValue {
11   private final String JavaDoc value;
12   // Se sugería un número primo para el tamaño
13
static final private HashMap strings = new HashMap(5003, 0.80f);
14
15   public SharedString(String JavaDoc aValue) {
16     value = aValue;
17     strings.put(aValue, this);
18   }
19
20   static public SharedString newFrom(String JavaDoc aValue) {
21     Object JavaDoc string;
22
23     string = strings.get(aValue);
24     if (string == null) {
25       return new SharedString(aValue);
26     }
27     else {
28       return (SharedString) string;
29     }
30   }
31
32   public String JavaDoc value() {
33     return value;
34   }
35
36   public boolean equals(Object JavaDoc o) {
37     final String JavaDoc string;
38     if (this == o) return true;
39
40     if (o instanceof String JavaDoc) {
41       string = (String JavaDoc) o;
42     }
43     else {
44       string = ((SharedString) o).value;
45     }
46     if (!value.equals(string)) return false;
47     return true;
48   }
49
50   public int hashCode() {
51     return value.hashCode();
52   }
53
54   public String JavaDoc toString(){
55     return value;
56   }
57
58   public int compareTo(Object JavaDoc o) {
59     if ((toString()== null )&&(o.toString()==null)){
60       return 0;
61     }else if (toString()== null ){
62       return -1;
63     }else if ((o.toString()==null)){
64       return 1;
65     }else {
66       return toString().compareTo(o.toString());
67     }
68   }
69
70   public Object JavaDoc getValue() {
71     return value;
72   }
73
74 }
75
Popular Tags