-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGXHashMap.java
More file actions
138 lines (115 loc) · 3.42 KB
/
GXHashMap.java
File metadata and controls
138 lines (115 loc) · 3.42 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.genexus.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.genexus.GXutil;
import org.apache.logging.log4j.Logger;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class GXHashMap<K, V> extends HashMap<K, V> {
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(GXHashMap.class);
private static final ObjectMapper objectMapper = new ObjectMapper();
private boolean isNumberKey = false;
public void setHashMap(GXHashMap<K, V> hashMap) {
putAll(hashMap);
}
public V put(K key, V value) {
if (key instanceof Number)
isNumberKey = true;
return super.put(key, value);
}
public boolean get(K key, V[] value) {
if (containsKey(key)) {
value[0] = get(key);
return true;
}
return false;
}
public boolean removeKey(K key) {
if (containsKey(key)) {
if (remove(key) != null)
return true;
}
return false;
}
public void removeKeys(Vector<K> keys) {
for (int i = 0; i < keys.size(); i++) {
removeKey(keys.get(i));
}
}
public void removeAll(GXHashMap<K, V> hashMap) {
for (Map.Entry<K, V> entry : hashMap.entrySet()) {
removeKey(entry.getKey());
}
}
public String toJson() {
try {
return objectMapper.writeValueAsString(this);
}
catch (JsonProcessingException e) {
log.error("Could not obtain json form Dictionary", e);
return "";
}
}
public void fromJson(String json) {
Type type = new ParameterizedType() {
@Override
public Type[] getActualTypeArguments() {
return ((ParameterizedType) getClass().getEnclosingClass().getGenericSuperclass()).getActualTypeArguments();
}
@Override
public Type getRawType() {
return HashMap.class;
}
@Override
public Type getOwnerType() {
return null;
}
};
try {
this.clear();
HashMap<K, V> fromJsonHashMap = objectMapper.readValue(json, objectMapper.getTypeFactory().constructType(type));
if (!isNumberKey)
this.putAll(fromJsonHashMap);
else {
for (Map.Entry<K, V> entry : fromJsonHashMap.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
this.put((K) java.text.NumberFormat.getInstance().parse((String) key), value);
}
}
}
catch (Exception e) {
log.error("Could not set Dictionary from json", e);
}
}
public GXHashMap<K, String> dateToCharRest() {
return convertToCharRest(this, false);
}
public GXHashMap<K, String> timeToCharRest() {
return convertToCharRest(this, true);
}
private GXHashMap<K, String> convertToCharRest(GXHashMap<K, V> thisHashMap, boolean isDateTime) {
GXHashMap<K, String> chardMap = new GXHashMap<>();
for (Map.Entry<K, V> entry : thisHashMap.entrySet()) {
K key = entry.getKey();
V dateValue = entry.getValue();
String stringValue = isDateTime? GXutil.timeToCharREST((Date)dateValue) : GXutil.dateToCharREST((Date)dateValue);
chardMap.put(key, stringValue);
}
return chardMap;
}
public GXHashMap<K, V> charToDateREST(GXHashMap<K, String> charHashMap, boolean isDateTime) {
clear();
for (Map.Entry<K, String> entry : charHashMap.entrySet()) {
K key = entry.getKey();
String stringValue = entry.getValue();
V dateValue = isDateTime? (V)GXutil.charToTimeREST(stringValue): (V)GXutil.charToDateREST(stringValue);
put(key, dateValue);
}
return this;
}
}