svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.i18n / utils / java / src / org / gvsig / i18n / utils / DoubleProperties.java @ 40559
History | View | Annotate | Download (4.29 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 |
/**
|
25 |
*
|
26 |
*/
|
27 |
package org.gvsig.i18n.utils; |
28 |
|
29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
31 |
import java.util.ArrayList; |
32 |
import java.util.HashMap; |
33 |
import java.util.Iterator; |
34 |
|
35 |
/**
|
36 |
* The DoubleProperties class represents a set of properties. It provides the
|
37 |
* same functionality as its parent class, Properties. Besides that, it also
|
38 |
* provides an efficient method to get the key associated with a value.
|
39 |
*
|
40 |
* @author cesar
|
41 |
*
|
42 |
*/
|
43 |
public class DoubleProperties extends OrderedProperties { |
44 |
/**
|
45 |
*
|
46 |
*/
|
47 |
private static final long serialVersionUID = -1738114064256800193L; |
48 |
HashMap reverseMap = new HashMap(); |
49 |
|
50 |
public DoubleProperties() {
|
51 |
super();
|
52 |
} |
53 |
|
54 |
|
55 |
public DoubleProperties(OrderedProperties defaults) {
|
56 |
super(defaults);
|
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 |
|
77 |
public void load(InputStream stream) throws IOException { |
78 |
super.load(stream);
|
79 |
|
80 |
Iterator keysIterator = this.keySet().iterator(); |
81 |
ArrayList keySet;
|
82 |
|
83 |
String key, value;
|
84 |
while (keysIterator.hasNext()) {
|
85 |
key = (String) keysIterator.next();
|
86 |
value = this.getProperty(key);
|
87 |
if (reverseMap.containsKey(value)) {
|
88 |
keySet = (ArrayList) reverseMap.get(value);
|
89 |
keySet.add(key); |
90 |
} |
91 |
else {
|
92 |
keySet = new ArrayList(); |
93 |
keySet.add(key); |
94 |
reverseMap.put(value, keySet); |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
public Object setProperty(String key, String value) { |
100 |
ArrayList keySet;
|
101 |
|
102 |
Object returnValue = super.setProperty(key, value); |
103 |
if (reverseMap.containsKey(value)) {
|
104 |
keySet = (ArrayList) reverseMap.get(value);
|
105 |
keySet.add(key); |
106 |
} |
107 |
else {
|
108 |
keySet = new ArrayList(); |
109 |
keySet.add(key); |
110 |
reverseMap.put(value, keySet); |
111 |
} |
112 |
|
113 |
return returnValue;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Gets the key associated with the provided value. If there
|
118 |
* are several associated keys, returns one of them.
|
119 |
*
|
120 |
* @param value
|
121 |
* @return The key associated with the provided value, or null
|
122 |
* if the value is not present in the dictionary. If there
|
123 |
* are several associated keys, returns one of them.
|
124 |
*/
|
125 |
public String getAssociatedKey(String value) { |
126 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
127 |
if (keySet==null) return null; |
128 |
return (String) keySet.get(0); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Returns the keys associated with the provided value. If there
|
133 |
* are several associated keys, returns one of them.
|
134 |
*
|
135 |
* @param value
|
136 |
* @return An ArrayList containing the keys associated with the
|
137 |
* provided value, or null if the value is not present in the
|
138 |
* dictionary.
|
139 |
*/
|
140 |
public ArrayList getAssociatedKeys(String value) { |
141 |
return (ArrayList) reverseMap.get(value); |
142 |
} |
143 |
|
144 |
public Object remove(Object key) { |
145 |
Object value = super.remove(key); |
146 |
if (value==null) return null; |
147 |
ArrayList keySet = (ArrayList) reverseMap.get(value); |
148 |
if (keySet==null) return null; |
149 |
if (keySet.size()<=1) { |
150 |
//if it's the last key associated wit the value, remove the
|
151 |
// value from the reverseDictionary
|
152 |
reverseMap.remove(value); |
153 |
} |
154 |
else {
|
155 |
// otherwise, remove the key from the list of associated keys
|
156 |
keySet.remove(key); |
157 |
} |
158 |
return value;
|
159 |
} |
160 |
} |