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