|
| 1 | +/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon) |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License.*/ |
| 14 | + |
| 15 | +package zuo.biao.apijson; |
| 16 | + |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +/**type:value |
| 22 | + * @author Lemon |
| 23 | + */ |
| 24 | +public class TypeValueKeyEntry extends Entry<String, String> { |
| 25 | + |
| 26 | + private static Map<String, Class<?>> classMap; |
| 27 | + static { |
| 28 | + classMap = new HashMap<>(); |
| 29 | + classMap.put(boolean.class.getSimpleName(), boolean.class); |
| 30 | + classMap.put(int.class.getSimpleName(), int.class); |
| 31 | + classMap.put(long.class.getSimpleName(), long.class); |
| 32 | + classMap.put(float.class.getSimpleName(), float.class); |
| 33 | + classMap.put(double.class.getSimpleName(), double.class); |
| 34 | + classMap.put(Boolean.class.getSimpleName(), Boolean.class); |
| 35 | + classMap.put(Integer.class.getSimpleName(), Integer.class); |
| 36 | + classMap.put(Long.class.getSimpleName(), Long.class); |
| 37 | + classMap.put(Float.class.getSimpleName(), Float.class); |
| 38 | + classMap.put(Double.class.getSimpleName(), Double.class); |
| 39 | + |
| 40 | + classMap.put(Object.class.getSimpleName(), Object.class); |
| 41 | + classMap.put(String.class.getSimpleName(), String.class); |
| 42 | + // classMap.put(JSONArray.class.getSimpleName(), JSONArray.class);//JSONArray implements Map |
| 43 | + classMap.put(Collection.class.getSimpleName(), Collection.class);//不允许指定<T> |
| 44 | + classMap.put(Map.class.getSimpleName(), Map.class);//不允许指定<T> |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public TypeValueKeyEntry() { |
| 49 | + super(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * @param <K> |
| 55 | + * @param typeValue |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public static <K, V> boolean isCorrect(Entry<K, V> typeValue) { |
| 59 | + return typeValue != null && StringUtil.isNotEmpty(typeValue.getValue(), true); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param typeValue |
| 64 | + * @return |
| 65 | + */ |
| 66 | + public String toEntryString() { |
| 67 | + return toEntryString(getKey(), getValue()); |
| 68 | + } |
| 69 | + /** |
| 70 | + * @param typeValue |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public static String toEntryString(String typeKey, String valueKey) { |
| 74 | + return (typeKey == null ? "" : typeKey + ":") + valueKey; |
| 75 | + } |
| 76 | + /** |
| 77 | + * @param type |
| 78 | + * @param value |
| 79 | + * @return |
| 80 | + */ |
| 81 | + public static String toEntryString(Class<?> type, Object value) { |
| 82 | + return toEntryString(type == null ? null : type.getSimpleName(), StringUtil.getString(value)); |
| 83 | + } |
| 84 | + /** |
| 85 | + * @param typeValue |
| 86 | + * @return |
| 87 | + */ |
| 88 | + public static Entry<String, String> parseKeyEntry(String typeValue) { |
| 89 | + typeValue = StringUtil.getString(typeValue);//让客户端去掉所有空格 getNoBlankString(typeValue); |
| 90 | + if (typeValue.isEmpty()) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + int index = typeValue.contains(":") ? typeValue.indexOf(":") : -1; |
| 94 | + |
| 95 | + Entry<String, String> entry = new Entry<String, String>(); |
| 96 | + entry.setKey(index < 0 ? Object.class.getSimpleName() : typeValue.substring(0, index)); |
| 97 | + entry.setValue(typeValue.substring(index + 1, typeValue.length())); |
| 98 | + |
| 99 | + return entry; |
| 100 | + } |
| 101 | + /** |
| 102 | + * @param valueMap |
| 103 | + * @param typeValue |
| 104 | + * @return |
| 105 | + */ |
| 106 | + public static Entry<Class<?>, Object> parseEntry(Map<String, Object> valueMap, String typeValue) { |
| 107 | + typeValue = StringUtil.getString(typeValue);//让客户端去掉所有空格 getNoBlankString(typeValue); |
| 108 | + if (typeValue.isEmpty()) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + int index = typeValue.contains(":") ? typeValue.indexOf(":") : -1; |
| 112 | + |
| 113 | + Entry<Class<?>, Object> entry = new Entry<>(); |
| 114 | + entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : typeValue.substring(0, index))); |
| 115 | + entry.setValue(valueMap == null ? null : valueMap.get(typeValue.substring(index + 1, typeValue.length()))); |
| 116 | + |
| 117 | + return entry; |
| 118 | + } |
| 119 | +} |
0 commit comments