root / trunk / libraries / libInternationalization / src-utils / org / gvsig / i18n / utils / DoubleProperties.java @ 11120
History | View | Annotate | Download (3.35 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.i18n.utils; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.util.ArrayList; |
9 |
import java.util.HashMap; |
10 |
import java.util.Iterator; |
11 |
|
12 |
/**
|
13 |
* The DoubleProperties class represents a set of properties. It provides the
|
14 |
* same functionality as its parent class, Properties. Besides that, it also
|
15 |
* provides an efficient method to get the key associated with a value.
|
16 |
*
|
17 |
* @author cesar
|
18 |
*
|
19 |
*/
|
20 |
public class DoubleProperties extends OrderedProperties { |
21 |
/**
|
22 |
*
|
23 |
*/
|
24 |
private static final long serialVersionUID = -1738114064256800193L; |
25 |
HashMap reverseMap = new HashMap(); |
26 |
|
27 |
public DoubleProperties() {
|
28 |
super();
|
29 |
} |
30 |
|
31 |
|
32 |
public DoubleProperties(OrderedProperties defaults) {
|
33 |
super(defaults);
|
34 |
Iterator keysIterator = this.keySet().iterator(); |
35 |
ArrayList keySet;
|
36 |
|
37 |
String key, value;
|
38 |
while (keysIterator.hasNext()) {
|
39 |
key = (String) keysIterator.next();
|
40 |
value = this.getProperty(key);
|
41 |
if (reverseMap.containsKey(value)) {
|
42 |
keySet = (ArrayList) reverseMap.get(value);
|
43 |
keySet.add(key); |
44 |
} |
45 |
else {
|
46 |
keySet = new ArrayList(); |
47 |
keySet.add(key); |
48 |
reverseMap.put(value, keySet); |
49 |
} |
50 |
} |
51 |
} |
52 |
|
53 |
|
54 |
public void load(InputStream stream) throws IOException { |
55 |
super.load(stream);
|
56 |
|
57 |
Iterator keysIterator = this.keySet().iterator(); |
58 |
ArrayList keySet;
|
59 |
|
60 |
String key, value;
|
61 |
while (keysIterator.hasNext()) {
|
62 |
key = (String) keysIterator.next();
|
63 |
value = this.getProperty(key);
|
64 |
if (reverseMap.containsKey(value)) {
|
65 |
keySet = (ArrayList) reverseMap.get(value);
|
66 |
keySet.add(key); |
67 |
} |
68 |
else {
|
69 |
keySet = new ArrayList(); |
70 |
keySet.add(key); |
71 |
reverseMap.put(value, keySet); |
72 |
} |
73 |
} |
74 |
} |
75 |
|
76 |
public Object setProperty(String key, String value) { |
77 |
ArrayList keySet;
|
78 |
|
79 |
Object returnValue = super.setProperty(key, value); |
80 |
if (reverseMap.containsKey(value)) {
|
81 |
keySet = (ArrayList) reverseMap.get(value);
|
82 |
keySet.add(key); |
83 |
} |
84 |
else {
|
85 |
keySet = new ArrayList(); |
86 |
keySet.add(key); |
87 |
reverseMap.put(value, keySet); |
88 |
} |
89 |
|
90 |
return returnValue;
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Gets the key associated with the provided value. If there
|
95 |
* are several associated keys, returns one of them.
|
96 |
*
|
97 |
* @param value
|
98 |
* @return The key associated with the provided value, or null
|
99 |
* if the value is not present in the dictionary. If there
|
100 |
* are several associated keys, returns one of them.
|
101 |
*/
|
102 |
public String getAssociatedKey(String value) { |
103 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
104 |
if (keySet==null) return null; |
105 |
return (String) keySet.get(0); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Returns the keys associated with the provided value. If there
|
110 |
* are several associated keys, returns one of them.
|
111 |
*
|
112 |
* @param value
|
113 |
* @return An ArrayList containing the keys associated with the
|
114 |
* provided value, or null if the value is not present in the
|
115 |
* dictionary.
|
116 |
*/
|
117 |
public ArrayList getAssociatedKeys(String value) { |
118 |
return (ArrayList) reverseMap.get(value); |
119 |
} |
120 |
|
121 |
public Object remove(Object key) { |
122 |
Object value = super.remove(key); |
123 |
if (value==null) return null; |
124 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
125 |
if (keySet==null) return null; |
126 |
if (keySet.size()<=1) { |
127 |
//if it's the last key associated wit the value, remove the
|
128 |
// value from the reverseDictionary
|
129 |
reverseMap.remove(value); |
130 |
} |
131 |
else {
|
132 |
// otherwise, remove the key from the list of associated keys
|
133 |
keySet.remove(key); |
134 |
} |
135 |
return value;
|
136 |
} |
137 |
} |