-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathCompactMapFactory.java
146 lines (125 loc) · 5.05 KB
/
CompactMapFactory.java
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
139
140
141
142
143
144
145
146
package com.cedarsoftware.io.factory;
import java.util.Map;
import com.cedarsoftware.io.JsonIoException;
import com.cedarsoftware.io.JsonObject;
import com.cedarsoftware.io.JsonReader;
import com.cedarsoftware.io.Resolver;
import com.cedarsoftware.util.ClassUtilities;
import com.cedarsoftware.util.CompactMap;
/**
* Factory for creating CompactMap instances from JSON with shortened configuration format.
*
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
* <br><br>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <br><br>
* <a href="http://www.apache.org/licenses/LICENSE-2.0">License</a>
* <br><br>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public class CompactMapFactory implements JsonReader.ClassFactory {
@Override
@SuppressWarnings("unchecked")
public Map newInstance(Class<?> c, JsonObject jObj, Resolver resolver) {
// Extract config and data sections
Object configObj = jObj.get("config");
Object dataObj = jObj.get("data");
if (!(configObj instanceof String)) {
throw new JsonIoException("CompactMap requires a config string");
}
if (!(dataObj instanceof Map)) {
throw new JsonIoException("CompactMap requires a data section");
}
// Parse config string: mapClass/CS|CI/S{size}/{singleKey}/{order}
String configStr = (String) configObj;
String[] parts = configStr.split("/");
if (parts.length != 5) {
throw new JsonIoException("Invalid CompactMap config format: " + configStr);
}
// Extract config values
String mapClassName = parts[0];
boolean caseSensitive = "CS".equals(parts[1]);
int compactSize = Integer.parseInt(parts[2].substring(1)); // Skip 'S' prefix
String singleKey = "-".equals(parts[3]) ? null : parts[3];
String orderCode = parts[4];
// Map orderCode back to CompactMap constants
String order;
switch (orderCode) {
case "Sort":
order = CompactMap.SORTED;
break;
case "Rev":
order = CompactMap.REVERSE;
break;
case "Ins":
order = CompactMap.INSERTION;
break;
case "Unord":
default:
order = CompactMap.UNORDERED;
break;
}
// Load the map class directly by name (it should be fully qualified)
Class<? extends Map> mapImplClass = null;
Class<?> cls = ClassUtilities.forName(mapClassName, ClassUtilities.getClassLoader());
if (Map.class.isAssignableFrom(cls)) {
mapImplClass = (Class<? extends Map>) cls;
}
// Build CompactMap with the right configuration
CompactMap.Builder<Object, Object> builder = CompactMap.builder()
.caseSensitive(caseSensitive)
.compactSize(compactSize);
if (singleKey != null && !singleKey.isEmpty()) {
builder.singleValueKey(singleKey);
}
switch (order) {
case CompactMap.SORTED:
builder.sortedOrder();
break;
case CompactMap.REVERSE:
builder.reverseOrder();
break;
case CompactMap.INSERTION:
builder.insertionOrder();
break;
default:
builder.noOrder();
break;
}
// Set the map type if we found a valid class
if (mapImplClass != null) {
builder.mapType(mapImplClass);
}
CompactMap<Object, Object> cmap = builder.build();
Map<Object, Object> data = (Map<Object, Object>) dataObj;
JsonReader reader = new JsonReader(resolver);
// Process all entries in the data section
for (Map.Entry<Object, Object> entry : data.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
// Resolve JsonObjects
if (key instanceof JsonObject) {
key = reader.toJava(((JsonObject) key).getType(), key);
}
if (value instanceof JsonObject) {
value = reader.toJava(((JsonObject) value).getType(), value);
}
// Add to the CompactMap
cmap.put(key, value);
}
// Remove config and data from the original JsonObject
jObj.remove("config");
jObj.remove("data");
// Set the target
jObj.setTarget(cmap);
return cmap;
}
}