forked from Tencent/APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPair.java
More file actions
executable file
·150 lines (133 loc) · 4.92 KB
/
Pair.java
File metadata and controls
executable file
·150 lines (133 loc) · 4.92 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
139
140
141
142
143
144
145
146
147
148
149
150
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import apijson.StringUtil;
/**key:value
* @author Lemon
*/
public class Pair extends Entry<String, String> {
private static final Map<String, Class<?>> CLASS_MAP;
static {
CLASS_MAP = new HashMap<String, Class<?>>();
CLASS_MAP.put(boolean.class.getSimpleName(), boolean.class);
CLASS_MAP.put(int.class.getSimpleName(), int.class);
CLASS_MAP.put(long.class.getSimpleName(), long.class);
CLASS_MAP.put(float.class.getSimpleName(), float.class);
CLASS_MAP.put(double.class.getSimpleName(), double.class);
CLASS_MAP.put(Boolean.class.getSimpleName(), Boolean.class);
CLASS_MAP.put(Integer.class.getSimpleName(), Integer.class);
CLASS_MAP.put(Long.class.getSimpleName(), Long.class);
CLASS_MAP.put(Float.class.getSimpleName(), Float.class);
CLASS_MAP.put(Double.class.getSimpleName(), Double.class);
CLASS_MAP.put(Object.class.getSimpleName(), Object.class);
CLASS_MAP.put(String.class.getSimpleName(), String.class);
CLASS_MAP.put(Collection.class.getSimpleName(), Collection.class);//不允许指定<T>
CLASS_MAP.put(Map.class.getSimpleName(), Map.class);//不允许指定<T>
CLASS_MAP.put(JSONObject.class.getSimpleName(), JSONObject.class);//必须有,Map中没有getLongValue等方法
CLASS_MAP.put(JSONArray.class.getSimpleName(), JSONArray.class);//必须有,Collection中没有getJSONObject等方法
}
public Pair() {
super();
}
public boolean isEmpty(boolean trim) {
return StringUtil.isNotEmpty(key, trim) == false && StringUtil.isNotEmpty(value, trim) == false;
}
/**
* @param <K>
* @param pair
* @return
*/
public static <K, V> boolean isCorrect(Entry<K, V> pair) {
return pair != null && StringUtil.isNotEmpty(pair.getValue(), true);
}
/**
* @param pair
* @return
*/
public String toPairString() {
return toPairString(getKey(), getValue());
}
/**
* @param pair
* @return
*/
public static String toPairString(String typeKey, String valueKey) {
return (typeKey == null ? "" : typeKey + ":") + valueKey;
}
/**
* @param type
* @param value
* @return
*/
public static String toPairString(Class<?> type, Object value) {
return toPairString(type == null ? null : type.getSimpleName(), StringUtil.getString(value));
}
/**
* isRightValueDefault = false;
* "key":null不应该出现?因为FastJSON内默认不存null
* @param pair leftKey:rightValue
* @return {@link #parseEntry(String, boolean)}
*/
public static Entry<String, String> parseEntry(String pair) {
return parseEntry(pair, false);
}
/**
* isRightValueDefault = false;
* "key":null不应该出现?因为FastJSON内默认不存null
* @param pair leftKey:rightValue
* @param isRightValueDefault 右边值缺省,当pair不包含 : 时默认整个pair为leftKey;false-相反
* @return {@link #parseEntry(String, boolean, String)}
*/
public static Entry<String, String> parseEntry(String pair, boolean isRightValueDefault) {
return parseEntry(pair, isRightValueDefault, null);
}
/**
* "key":null不应该出现?因为FastJSON内默认不存null
* @param pair leftKey:rightValue
* @param isRightValueDefault 右边值缺省,当pair不包含 : 时默认整个pair为leftKey;false-相反
* @param defaultValue 缺省值
* @return @NonNull
*/
public static Entry<String, String> parseEntry(String pair, boolean isRightValueDefault, String defaultValue) {
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
Entry<String, String> entry = new Entry<String, String>();
if (pair.isEmpty() == false) {
int index = pair.indexOf(":");
if (index < 0) {
entry.setKey(isRightValueDefault ? pair : defaultValue);
entry.setValue(isRightValueDefault ? defaultValue : pair);
} else {
entry.setKey(pair.substring(0, index));
entry.setValue(pair.substring(index + 1, pair.length()));
}
}
return entry;
}
/**
* @param pair
* @return
*/
public static Entry<String, String> parseVariableEntry(String pair) {
return parseEntry(pair, false, Object.class.getSimpleName());
}
/**
* @param pair
* @param valueMap
* @return
*/
public static Entry<Class<?>, Object> parseVariableEntry(String pair, Map<String, Object> valueMap) {
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
Entry<Class<?>, Object> entry = new Entry<Class<?>, Object>();
if (pair.isEmpty() == false) {
int index = pair.contains(":") ? pair.indexOf(":") : -1;
entry.setKey(CLASS_MAP.get(index < 0 ? Object.class.getSimpleName() : pair.substring(0, index)));
entry.setValue(valueMap == null ? null : valueMap.get(pair.substring(index + 1, pair.length())));
}
return entry;
}
}