1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
/**
* DataHelper.java
* Created on Aug 21, 2013
*
* Copyright (c) 2013, 2014 Wind River Systems, Inc.
*
* The right to copy, distribute, modify, or otherwise make use
* of this software may be licensed only pursuant to the terms
* of an applicable Wind River license agreement.
*/
package org.eclipse.tcf.te.runtime.persistence.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.persistence.PersistenceManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
/**
* Data helper for de/encoding.
*/
public class DataHelper {
/**
* Encode a properties container to a string.
* @param data The properties container.
* @return String representing the properties container.
*/
public static final String encodePropertiesContainer(IPropertiesContainer data) {
try {
if (data != null && !data.isEmpty()) {
IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(Map.class, String.class);
return (String)delegate.write(data, String.class);
}
}
catch (Exception e) {
}
return null;
}
private static final Map<String, IPropertiesContainer> cache = new LinkedHashMap<String, IPropertiesContainer>();
/**
* Decode a string encoded properties container.
* @param encoded The string encoded properties container.
* @return Properties container.
*/
public static final IPropertiesContainer decodePropertiesContainer(String encoded) {
if (encoded != null && encoded.trim().length() > 0) {
IPropertiesContainer result = cache.remove(encoded);
if (result == null) {
try {
IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(Map.class, String.class);
result = (IPropertiesContainer)delegate.read(IPropertiesContainer.class, encoded);
}
catch (Exception e) {
}
if (cache.size() == 10) cache.remove(cache.keySet().toArray()[0]);
}
cache.put(encoded, result);
return result;
}
return new PropertiesContainer();
}
/**
* Convert all keys of the map to lower case.
* @param list The list of maps to convert.
* @return List with new maps with lowercase keys.
*/
public static List<Map<String,Object>> keysToLowerCase(List<Map<String,Object>> list) {
List<Map<String, Object>> paramListLowerCase = new ArrayList<Map<String,Object>>();
for (Map<String, Object> map : list) {
paramListLowerCase.add(keysToLowerCase(map));
}
return paramListLowerCase;
}
/**
* Convert all keys of the map to lower case.
* @param map The map to convert.
* @return New map with lowercase keys.
*/
@SuppressWarnings("unchecked")
public static Map<String,Object> keysToLowerCase(Map<String,Object> map) {
Map<String, Object> mapLowerCase = new HashMap<String, Object>();
for (String key : map.keySet()) {
Object value = map.get(key);
if (value instanceof Map) {
value = keysToLowerCase((Map<String,Object>)value);
}
else if (value instanceof List) {
value = keysToLowerCase((List<Map<String,Object>>)value);
}
mapLowerCase.put(key.toLowerCase(), value);
}
return mapLowerCase;
}
}
|