/* * Copyright (C) 2020 the original author or authors. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package we.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.springframework.http.HttpHeaders; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import com.alibaba.fastjson.JSON; /** * * @author Francis Dong * */ public class MapUtil { public static HttpHeaders toHttpHeaders(Map params) { HttpHeaders headers = new HttpHeaders(); if (params == null || params.isEmpty()) { return headers; } for (Entry entry : params.entrySet()) { Object val = entry.getValue(); List list = new ArrayList<>(); if (val instanceof List) { List vals = (List) val; for (Object value : vals) { if (value != null) { list.add(value.toString()); } } } else { if (val != null) { list.add(val.toString()); } } if (list.size() > 0) { headers.put(entry.getKey(), list); } } return headers; } public static MultiValueMap toMultiValueMap(Map params) { MultiValueMap mvmap = new LinkedMultiValueMap<>(); if (params == null || params.isEmpty()) { return mvmap; } for (Entry entry : params.entrySet()) { Object val = entry.getValue(); List list = new ArrayList<>(); if (val instanceof List) { List vals = (List) val; for (Object value : vals) { if (value != null) { list.add(value.toString()); } } } else { if (val != null) { list.add(val.toString()); } } if (list.size() > 0) { mvmap.put(entry.getKey(), list); } } return mvmap; } public static Map toHashMap(MultiValueMap params) { HashMap m = new HashMap<>(); if (params == null || params.isEmpty()) { return m; } for (Entry> entry : params.entrySet()) { List val = entry.getValue(); if (val != null && val.size() > 0) { if (val.size() > 1) { m.put(entry.getKey(), val); } else { m.put(entry.getKey(), val.get(0)); } } } return m; } public static Map headerToHashMap(HttpHeaders headers) { HashMap m = new HashMap<>(); if (headers == null || headers.isEmpty()) { return m; } for (Entry> entry : headers.entrySet()) { List val = entry.getValue(); if (val != null && val.size() > 0) { if (val.size() > 1) { m.put(entry.getKey().toUpperCase(), val); } else { m.put(entry.getKey().toUpperCase(), val.get(0)); } } } return m; } public static Map upperCaseKey(Map m) { HashMap rs = new HashMap<>(); if (m == null || m.isEmpty()) { return rs; } for (Entry entry : m.entrySet()) { rs.put(entry.getKey().toUpperCase(), entry.getValue()); } return rs; } /** * Set value by path,support multiple levels,eg:a.b.c
* Do NOT use this method if field name contains a dot
* @param data * @param path * @param value */ @SuppressWarnings("unchecked") public static void set(Map data, String path, Object value) { String[] fields = path.split("\\."); if(fields.length < 2) { data.put(path, value); }else { Map next = data; for (int i = 0; i < fields.length - 1; i++) { Map val = (Map) next.get(fields[i]); if(val == null) { val = new HashMap<>(); next.put(fields[i], val); } if(i == fields.length - 2) { val.put(fields[i+1], value); break; } next = val; } } } /** * Get value by path, support multiple levels,eg:a.b.c
* Do NOT use this method if field name contains a dot
* @param data * @param path * @return */ public static Object get(Map data, String path) { String[] fields = path.split("\\."); if(fields.length < 2) { return data.get(path); }else { Map next = data; for (int i = 0; i < fields.length - 1; i++) { if(!(next.get(fields[i]) instanceof Map)) { return null; } Map val = (Map) next.get(fields[i]); if(val == null) { return null; } if(i == fields.length - 2) { return val.get(fields[i+1]); } next = val; } } return null; } /** * Merge maps, merge src to target * @param target * @param src * @return */ @SuppressWarnings("unchecked") public static Map merge(Map target, Map src) { if(src == null || src.isEmpty()) { return target; } src.forEach((key, value) -> { if(value != null) { target.merge(key, value, (oldValue, newValue) -> { if (oldValue instanceof Map && newValue instanceof Map) { oldValue = merge((Map) oldValue, (Map) newValue); return oldValue; } return newValue; }); } }); return target; } }