svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / DoubleProperties.java @ 40769
History | View | Annotate | Download (4.27 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.i18n.utils; |
25 |
|
26 |
import java.io.IOException; |
27 |
import java.io.InputStream; |
28 |
import java.util.ArrayList; |
29 |
import java.util.HashMap; |
30 |
import java.util.Iterator; |
31 |
|
32 |
/**
|
33 |
* The DoubleProperties class represents a set of properties. It provides the
|
34 |
* same functionality as its parent class, Properties. Besides that, it also
|
35 |
* provides an efficient method to get the key associated with a value.
|
36 |
*
|
37 |
* @author cesar
|
38 |
*
|
39 |
*/
|
40 |
public class DoubleProperties extends OrderedProperties { |
41 |
/**
|
42 |
*
|
43 |
*/
|
44 |
private static final long serialVersionUID = -1738114064256800193L; |
45 |
HashMap reverseMap = new HashMap(); |
46 |
|
47 |
public DoubleProperties() {
|
48 |
super();
|
49 |
} |
50 |
|
51 |
|
52 |
public DoubleProperties(OrderedProperties defaults) {
|
53 |
super(defaults);
|
54 |
Iterator keysIterator = this.keySet().iterator(); |
55 |
ArrayList keySet;
|
56 |
|
57 |
String key, value;
|
58 |
while (keysIterator.hasNext()) {
|
59 |
key = (String) keysIterator.next();
|
60 |
value = this.getProperty(key);
|
61 |
if (reverseMap.containsKey(value)) {
|
62 |
keySet = (ArrayList) reverseMap.get(value);
|
63 |
keySet.add(key); |
64 |
} |
65 |
else {
|
66 |
keySet = new ArrayList(); |
67 |
keySet.add(key); |
68 |
reverseMap.put(value, keySet); |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
|
74 |
public void load(InputStream stream) throws IOException { |
75 |
super.load(stream);
|
76 |
|
77 |
Iterator keysIterator = this.keySet().iterator(); |
78 |
ArrayList keySet;
|
79 |
|
80 |
String key, value;
|
81 |
while (keysIterator.hasNext()) {
|
82 |
key = (String) keysIterator.next();
|
83 |
value = this.getProperty(key);
|
84 |
if (reverseMap.containsKey(value)) {
|
85 |
keySet = (ArrayList) reverseMap.get(value);
|
86 |
keySet.add(key); |
87 |
} |
88 |
else {
|
89 |
keySet = new ArrayList(); |
90 |
keySet.add(key); |
91 |
reverseMap.put(value, keySet); |
92 |
} |
93 |
} |
94 |
} |
95 |
|
96 |
public Object setProperty(String key, String value) { |
97 |
ArrayList keySet;
|
98 |
|
99 |
Object returnValue = super.setProperty(key, value); |
100 |
if (reverseMap.containsKey(value)) {
|
101 |
keySet = (ArrayList) reverseMap.get(value);
|
102 |
keySet.add(key); |
103 |
} |
104 |
else {
|
105 |
keySet = new ArrayList(); |
106 |
keySet.add(key); |
107 |
reverseMap.put(value, keySet); |
108 |
} |
109 |
|
110 |
return returnValue;
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Gets the key associated with the provided value. If there
|
115 |
* are several associated keys, returns one of them.
|
116 |
*
|
117 |
* @param value
|
118 |
* @return The key associated with the provided value, or null
|
119 |
* if the value is not present in the dictionary. If there
|
120 |
* are several associated keys, returns one of them.
|
121 |
*/
|
122 |
public String getAssociatedKey(String value) { |
123 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
124 |
if (keySet==null) return null; |
125 |
return (String) keySet.get(0); |
126 |
} |
127 |
|
128 |
/**
|
129 |
* Returns the keys associated with the provided value. If there
|
130 |
* are several associated keys, returns one of them.
|
131 |
*
|
132 |
* @param value
|
133 |
* @return An ArrayList containing the keys associated with the
|
134 |
* provided value, or null if the value is not present in the
|
135 |
* dictionary.
|
136 |
*/
|
137 |
public ArrayList getAssociatedKeys(String value) { |
138 |
return (ArrayList) reverseMap.get(value); |
139 |
} |
140 |
|
141 |
public Object remove(Object key) { |
142 |
Object value = super.remove(key); |
143 |
if (value==null) return null; |
144 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
145 |
if (keySet==null) return null; |
146 |
if (keySet.size()<=1) { |
147 |
//if it's the last key associated wit the value, remove the
|
148 |
// value from the reverseDictionary
|
149 |
reverseMap.remove(value); |
150 |
} |
151 |
else {
|
152 |
// otherwise, remove the key from the list of associated keys
|
153 |
keySet.remove(key); |
154 |
} |
155 |
return value;
|
156 |
} |
157 |
} |