All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.Maps Maven / Gradle / Ivy

/*
 * Copyright (C) 2019 HaiYang Li
 *
 * 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package com.landawn.abacus.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.landawn.abacus.DirtyMarker;
import com.landawn.abacus.core.DirtyMarkerUtil;
import com.landawn.abacus.parser.ParserUtil;
import com.landawn.abacus.parser.ParserUtil.EntityInfo;
import com.landawn.abacus.parser.ParserUtil.PropInfo;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.u.Nullable;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.IntFunction;
import com.landawn.abacus.util.function.Supplier;

/**
 * 

* Note: This class includes codes copied from Apache Commons Lang, Google Guava and other open source projects under the Apache License 2.0. * The methods copied from other libraries/frameworks/projects may be modified in this class. *

* */ public final class Maps { private Maps() { // Utility class. } /** * * @param * @param the key type * @param * @param c * @param keyMapper * @return * @throws E the e */ public static Map newMap(Collection c, final Throwables.Function keyMapper) throws E { N.checkArgNotNull(keyMapper); if (N.isNullOrEmpty(c)) { return new HashMap<>(); } final Map result = N.newHashMap(c.size()); for (T e : c) { result.put(keyMapper.apply(e), e); } return result; } /** * * @param * @param the key type * @param the value type * @param * @param * @param c * @param keyMapper * @param valueExtractor * @return * @throws E the e * @throws E2 the e2 */ public static Map newMap(Collection c, final Throwables.Function keyMapper, final Throwables.Function valueExtractor) throws E, E2 { N.checkArgNotNull(keyMapper); N.checkArgNotNull(valueExtractor); if (N.isNullOrEmpty(c)) { return new HashMap<>(); } final Map result = N.newHashMap(c.size()); for (T e : c) { result.put(keyMapper.apply(e), valueExtractor.apply(e)); } return result; } /** * * @param * @param the key type * @param the value type * @param * @param * @param * @param c * @param keyMapper * @param valueExtractor * @param mapSupplier * @return * @throws E the e * @throws E2 the e2 */ public static , E extends Exception, E2 extends Exception> M newMap(Collection c, final Throwables.Function keyMapper, final Throwables.Function valueExtractor, final IntFunction mapSupplier) throws E, E2 { N.checkArgNotNull(keyMapper); N.checkArgNotNull(valueExtractor); if (N.isNullOrEmpty(c)) { return mapSupplier.apply(0); } final M result = mapSupplier.apply(c.size()); for (T e : c) { result.put(keyMapper.apply(e), valueExtractor.apply(e)); } return result; } /** * * @param * @param the key type * @param * @param iter * @param keyMapper * @return * @throws E the e */ public static Map newMap(final Iterator iter, final Throwables.Function keyMapper) throws E { N.checkArgNotNull(keyMapper); if (iter == null) { return new HashMap<>(); } final Map result = new HashMap<>(); T e = null; while (iter.hasNext()) { e = iter.next(); result.put(keyMapper.apply(e), e); } return result; } /** * * @param * @param the key type * @param the value type * @param * @param * @param iter * @param keyMapper * @param valueExtractor * @return * @throws E the e * @throws E2 the e2 */ public static Map newMap(final Iterator iter, final Throwables.Function keyMapper, final Throwables.Function valueExtractor) throws E, E2 { N.checkArgNotNull(keyMapper); N.checkArgNotNull(valueExtractor); if (iter == null) { return new HashMap<>(); } final Map result = new HashMap<>(); T e = null; while (iter.hasNext()) { e = iter.next(); result.put(keyMapper.apply(e), valueExtractor.apply(e)); } return result; } /** * * @param * @param the key type * @param the value type * @param * @param * @param * @param iter * @param keyMapper * @param valueExtractor * @param mapSupplier * @return * @throws E the e * @throws E2 the e2 */ public static , E extends Exception, E2 extends Exception> M newMap(final Iterator iter, final Throwables.Function keyMapper, final Throwables.Function valueExtractor, final Supplier mapSupplier) throws E, E2 { N.checkArgNotNull(keyMapper); N.checkArgNotNull(valueExtractor); if (iter == null) { return mapSupplier.get(); } final M result = mapSupplier.get(); T e = null; while (iter.hasNext()) { e = iter.next(); result.put(keyMapper.apply(e), valueExtractor.apply(e)); } return result; } /** * New target map. * * @param m * @return */ @SuppressWarnings("rawtypes") static Map newTargetMap(Map m) { return newTargetMap(m, m == null ? 0 : m.size()); } /** * New target map. * * @param m * @param size * @return */ @SuppressWarnings("rawtypes") static Map newTargetMap(Map m, int size) { if (m == null) { return new HashMap<>(); } Map res = null; if (HashMap.class.equals(m.getClass())) { res = N.newHashMap(size); } else if (m instanceof SortedMap) { res = new TreeMap<>(((SortedMap) m).comparator()); } else if (m instanceof IdentityHashMap) { res = N.newIdentityHashMap(size); } else if (m instanceof LinkedHashMap) { res = N.newLinkedHashMap(size); } else if (m instanceof ImmutableMap) { res = N.newLinkedHashMap(size); } else { try { res = N.newInstance(m.getClass()); } catch (Exception e) { res = N.newLinkedHashMap(size); } } return res; } /** * New ordering map. * * @param m * @return */ @SuppressWarnings("rawtypes") static Map newOrderingMap(Map m) { if (m == null) { return new HashMap<>(); } Map res = null; if (HashMap.class.equals(m.getClass())) { res = N.newHashMap(m.size()); } else if (m instanceof SortedMap) { res = N.newLinkedHashMap(m.size()); } else if (m instanceof IdentityHashMap) { res = N.newIdentityHashMap(m.size()); } else if (m instanceof LinkedHashMap) { res = N.newLinkedHashMap(m.size()); } else if (m instanceof ImmutableMap) { res = N.newLinkedHashMap(m.size()); } else { try { res = N.newInstance(m.getClass()); } catch (Exception e) { res = N.newLinkedHashMap(m.size()); } } return res; } /** * * @param * @param * @param key * @param value * @return * @deprecated replaced by {@link N#newEntry(Object, Object)} */ @Deprecated public static Map.Entry newEntry(final K key, final V value) { return N.newEntry(key, value); } /** * * @param * @param * @param key * @param value * @return * @deprecated replaced by {@link N#newImmutableEntry(Object, Object)} */ @Deprecated public static ImmutableEntry newImmutableEntry(final K key, final V value) { return N.newImmutableEntry(key, value); } public static Map zip(final Collection keys, final Collection values) { if (N.isNullOrEmpty(keys) || N.isNullOrEmpty(values)) { return new HashMap<>(); } final Iterator keyIter = keys.iterator(); final Iterator valueIter = values.iterator(); final int minLen = N.min(keys.size(), values.size()); final Map result = N.newHashMap(minLen); for (int i = 0; i < minLen; i++) { result.put(keyIter.next(), valueIter.next()); } return result; } public static > Map zip(final Collection keys, final Collection values, final IntFunction mapSupplier) { if (N.isNullOrEmpty(keys) || N.isNullOrEmpty(values)) { return new HashMap<>(); } final Iterator keyIter = keys.iterator(); final Iterator valueIter = values.iterator(); final int minLen = N.min(keys.size(), values.size()); final Map result = mapSupplier.apply(minLen); for (int i = 0; i < minLen; i++) { result.put(keyIter.next(), valueIter.next()); } return result; } public static > Map zip(final Collection keys, final Collection values, final BinaryOperator mergeFunction, final IntFunction mapSupplier) { if (N.isNullOrEmpty(keys) || N.isNullOrEmpty(values)) { return new HashMap<>(); } final Iterator keyIter = keys.iterator(); final Iterator valueIter = values.iterator(); final int minLen = N.min(keys.size(), values.size()); final Map result = mapSupplier.apply(minLen); for (int i = 0; i < minLen; i++) { result.merge(keyIter.next(), valueIter.next(), mergeFunction); } return result; } /** * * @param the key type * @param the value type * @param map * @param key * @return */ public static Nullable get(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return Nullable.empty(); } final V val = map.get(key); if (val != null || map.containsKey(key)) { return Nullable.of(val); } else { return Nullable.empty(); } } /** * Returns a list of values of the keys which exist in the specified Map. * If the key dosn't exist in the Map, No value will be added into the returned list. * * @param the key type * @param the value type * @param map * @param keys * @return */ public static List getIfPresentForEach(final Map map, final Collection keys) { if (N.isNullOrEmpty(map) || N.isNullOrEmpty(keys)) { return new ArrayList<>(0); } final List result = new ArrayList<>(keys.size()); V val = null; for (Object key : keys) { val = map.get(key); if (val != null || map.containsKey(key)) { result.add(val); } } return result; } /** * Returns the value to which the specified key is mapped, or {@code defaultValue} if this map contains no mapping for the key. * * @param the key type * @param the value type * @param map * @param key * @param defaultValue * @return */ public static V getOrDefault(final Map map, final Object key, final V defaultValue) { if (N.isNullOrEmpty(map)) { return defaultValue; } final V val = map.get(key); if (val != null || map.containsKey(key)) { return val; } else { return defaultValue; } } /** * Returns the value to which the specified key is mapped if it's not {@code null}, * or {@code defaultForNull} if this map contains no mapping for the key or it's {@code null}. * * @param * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static V getOrDefaultIfNull(final Map map, final Object key, final V defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final V val = map.get(key); if (val == null) { return defaultForNull; } else { return val; } } public static Boolean getBoolean(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Boolean) { return (Boolean) val; } else { return N.parseBoolean(N.toString(val)); } } /** * Returns the mapped {@code boolean} or a boolean converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static boolean getBoolean(final Map map, final Object key, final boolean defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Boolean) { return (Boolean) val; } else { return N.parseBoolean(N.toString(val)); } } public static Character getChar(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Character) { return (Character) val; } else { return N.parseChar(N.toString(val)); } } /** * Returns the mapped {@code char} or a char converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static char getChar(final Map map, final Object key, final char defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Character) { return (Character) val; } else { return N.parseChar(N.toString(val)); } } public static Byte getByte(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).byteValue(); } else { return Numbers.toByte(N.toString(val)); } } /** * Returns the mapped {@code byte} or a byte converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static byte getByte(final Map map, final Object key, final byte defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).byteValue(); } else { return Numbers.toByte(N.toString(val)); } } public static Short getShort(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).shortValue(); } else { return Numbers.toShort(N.toString(val)); } } /** * Returns the mapped {@code short} or a short converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static short getShort(final Map map, final Object key, final short defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).shortValue(); } else { return Numbers.toShort(N.toString(val)); } } public static Integer getInt(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).intValue(); } else { return Numbers.toInt(N.toString(val)); } } /** * Returns the mapped {@code integer} or an integer converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static int getInt(final Map map, final Object key, final int defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).intValue(); } else { return Numbers.toInt(N.toString(val)); } } public static Long getLong(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).longValue(); } else { return Numbers.toLong(N.toString(val)); } } /** * Returns the mapped {@code long} or a long converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static long getLong(final Map map, final Object key, final long defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).longValue(); } else { return Numbers.toLong(N.toString(val)); } } public static Float getFloat(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).floatValue(); } else { return Numbers.toFloat(N.toString(val)); } } /** * Returns the mapped {@code float} or a float converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static float getFloat(final Map map, final Object key, final float defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).floatValue(); } else { return Numbers.toFloat(N.toString(val)); } } public static Double getDouble(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return null; } final Object val = map.get(key); if (val == null) { return null; } else if (val instanceof Number) { return ((Number) val).doubleValue(); } else { return Numbers.toDouble(N.toString(val)); } } /** * Returns the mapped {@code double} or a double converted from String. * {@code defaultForNull} is returned if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * * @param * @param map * @param key * @param defaultForNull to return if the specified {@code map} doesn't contain the specified {@code key} or the mapped value is {@code null}. * @return */ public static double getDouble(final Map map, final Object key, final double defaultForNull) { if (N.isNullOrEmpty(map)) { return defaultForNull; } final Object val = map.get(key); if (val == null) { return defaultForNull; } else if (val instanceof Number) { return ((Number) val).doubleValue(); } else { return Numbers.toDouble(N.toString(val)); } } /** * Returns the value to which the specified key is mapped, or * an empty immutable {@code List} if this map contains no mapping for the key. * * @param the key type * @param * @param the value type * @param map * @param key * @return */ public static > List getOrEmptyList(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return N. emptyList(); } final V val = map.get(key); if (val != null || map.containsKey(key)) { return val; } else { return N.emptyList(); } } /** * Returns the value to which the specified key is mapped, or * an empty immutable {@code Set} if this map contains no mapping for the key. * * @param the key type * @param * @param the value type * @param map * @param key * @return */ public static > Set getOrEmptySet(final Map map, final Object key) { if (N.isNullOrEmpty(map)) { return N. emptySet(); } final V val = map.get(key); if (val != null || map.containsKey(key)) { return val; } else { return N.emptySet(); } } /** * Gets the or default for each. * * @param the key type * @param the value type * @param map * @param keys * @param defaultValue * @return */ public static List getOrDefaultForEach(final Map map, final Collection keys, final V defaultValue) { if (N.isNullOrEmpty(keys)) { return new ArrayList<>(0); } else if (N.isNullOrEmpty(map)) { return N.repeat(defaultValue, keys.size()); } final List result = new ArrayList<>(keys.size()); V val = null; for (Object key : keys) { val = map.get(key); if (val != null || map.containsKey(key)) { result.add(val); } else { result.add(defaultValue); } } return result; } /** * Returns the value associated with the specified {@code key} if it exists in the specified {@code map} contains, or the new put {@code List} if it's absent. * * @param the key type * @param * @param map * @param key * @return */ public static List getAndPutListIfAbsent(final Map> map, final K key) { List v = map.get(key); if (v == null) { v = new ArrayList<>(); v = map.put(key, v); } return v; } /** * Returns the value associated with the specified {@code key} if it exists in the specified {@code map} contains, or the new put {@code Set} if it's absent. * * @param the key type * @param * @param map * @param key * @return */ public static Set getAndPutSetIfAbsent(final Map> map, final K key) { Set v = map.get(key); if (v == null) { v = N.newHashSet(); v = map.put(key, v); } return v; } /** * Returns the value associated with the specified {@code key} if it exists in the specified {@code map} contains, or the new put {@code Set} if it's absent. * * @param the key type * @param * @param map * @param key * @return */ public static Set getAndPutLinkedHashSetIfAbsent(final Map> map, final K key) { Set v = map.get(key); if (v == null) { v = N.newLinkedHashSet(); v = map.put(key, v); } return v; } /** * Returns the value associated with the specified {@code key} if it exists in the specified {@code map} contains, or the new put {@code Map} if it's absent. * * @param the key type * @param * @param * @param map * @param key * @return */ public static Map getAndPutMapIfAbsent(final Map> map, final K key) { Map v = map.get(key); if (v == null) { v = new HashMap<>(); v = map.put(key, v); } return v; } /** * Check if the specified Map contains the specified Entry. * * @param map * @param entry * @return */ public static boolean contains(final Map map, final Map.Entry entry) { return contains(map, entry.getKey(), entry.getValue()); } /** * * @param map * @param key * @param value * @return */ public static boolean contains(final Map map, final Object key, final Object value) { if (N.isNullOrEmpty(map)) { return false; } final Object val = map.get(key); return val == null ? value == null && map.containsKey(key) : N.equals(val, value); } /** * * @param the key type * @param the value type * @param map * @param map2 * @return */ public static Map intersection(final Map map, final Map map2) { if (N.isNullOrEmpty(map) || N.isNullOrEmpty(map2)) { return new LinkedHashMap<>(); } final Map result = map instanceof IdentityHashMap ? new IdentityHashMap<>() : new LinkedHashMap<>(); Object val = null; for (Map.Entry entry : map.entrySet()) { val = map2.get(entry.getKey()); if ((val != null && N.equals(val, entry.getValue())) || (entry.getValue() == null && map.containsKey(entry.getKey()))) { result.put(entry.getKey(), entry.getValue()); } } return result; } /** * * @param the key type * @param the value type * @param map * @param map2 * @return */ public static Map>> difference(final Map map, final Map map2) { if (N.isNullOrEmpty(map)) { return new LinkedHashMap<>(); } final Map>> result = map instanceof IdentityHashMap ? new IdentityHashMap<>() : new LinkedHashMap<>(); if (N.isNullOrEmpty(map2)) { for (Map.Entry entry : map.entrySet()) { result.put(entry.getKey(), Pair.of(entry.getValue(), Nullable. empty())); } } else { V val = null; for (Map.Entry entry : map.entrySet()) { val = map2.get(entry.getKey()); if (val == null && map2.containsKey(entry.getKey()) == false) { result.put(entry.getKey(), Pair.of(entry.getValue(), Nullable. empty())); } else if (N.equals(val, entry.getValue()) == false) { result.put(entry.getKey(), Pair.of(entry.getValue(), Nullable.of(val))); } } } return result; } /** * * @param the key type * @param the value type * @param map * @param map2 * @return */ public static Map, Nullable>> symmetricDifference(final Map map, final Map map2) { final boolean isIdentityHashMap = (N.notNullOrEmpty(map) && map instanceof IdentityHashMap) || (N.notNullOrEmpty(map2) && map2 instanceof IdentityHashMap); final Map, Nullable>> result = isIdentityHashMap ? new IdentityHashMap<>() : new LinkedHashMap<>(); if (N.notNullOrEmpty(map)) { if (N.isNullOrEmpty(map2)) { for (Map.Entry entry : map.entrySet()) { result.put(entry.getKey(), Pair.of(Nullable.of(entry.getValue()), Nullable. empty())); } } else { K key = null; V val2 = null; for (Map.Entry entry : map.entrySet()) { key = entry.getKey(); val2 = map2.get(key); if (val2 == null && map2.containsKey(key) == false) { result.put(key, Pair.of(Nullable.of(entry.getValue()), Nullable. empty())); } else if (N.equals(val2, entry.getValue()) == false) { result.put(key, Pair.of(Nullable.of(entry.getValue()), Nullable.of(val2))); } } } } if (N.notNullOrEmpty(map2)) { if (N.isNullOrEmpty(map)) { for (Map.Entry entry : map2.entrySet()) { result.put(entry.getKey(), Pair.of(Nullable. empty(), Nullable.of(entry.getValue()))); } } else { for (Map.Entry entry : map2.entrySet()) { if (map.containsKey(entry.getKey()) == false) { result.put(entry.getKey(), Pair.of(Nullable. empty(), Nullable.of(entry.getValue()))); } } } } return result; } /** * Put if absent. * * @param the key type * @param the value type * @param map * @param key * @param value * @return */ public static V putIfAbsent(final Map map, K key, final V value) { V v = map.get(key); if (v == null) { v = map.put(key, value); } return v; } /** * Put if absent. * * @param the key type * @param the value type * @param map * @param key * @param supplier * @return */ public static V putIfAbsent(final Map map, K key, final Supplier supplier) { V v = map.get(key); if (v == null) { v = map.put(key, supplier.get()); } return v; } /** * Removes the specified entry. * * @param the key type * @param the value type * @param map * @param entry * @return */ public static boolean remove(final Map map, Map.Entry entry) { return remove(map, entry.getKey(), entry.getValue()); } /** * * @param the key type * @param the value type * @param map * @param key * @param value * @return */ public static boolean remove(final Map map, final Object key, final Object value) { if (N.isNullOrEmpty(map)) { return false; } final Object curValue = map.get(key); if (!N.equals(curValue, value) || (curValue == null && !map.containsKey(key))) { return false; } map.remove(key); return true; } /** * Removes the keys. * * @param map * @param keysToRemove * @return true if any key/value was removed, otherwise false. */ public static boolean removeKeys(final Map map, final Collection keysToRemove) { if (N.isNullOrEmpty(map) || N.isNullOrEmpty(keysToRemove)) { return false; } final int originalSize = map.size(); for (Object key : keysToRemove) { map.remove(key); } return map.size() < originalSize; } /** * The the entries from the specified Map. * * @param map * @param entriesToRemove * @return true if any key/value was removed, otherwise false. */ public static boolean removeEntries(final Map map, final Map entriesToRemove) { if (N.isNullOrEmpty(map) || N.isNullOrEmpty(entriesToRemove)) { return false; } final int originalSize = map.size(); for (Map.Entry entry : entriesToRemove.entrySet()) { if (N.equals(map.get(entry.getKey()), entry.getValue())) { map.remove(entry.getKey()); } } return map.size() < originalSize; } /** * Removes entries from the specified {@code map} by the the specified {@code filter}. * * @param the key type * @param the value type * @param * @param map * @param filter * @return {@code true} if there are one or more than one entries removed from the specified map. * @throws E the e */ public static boolean removeIf(final Map map, final Throwables.Predicate, E> filter) throws E { List keysToRemove = null; for (Map.Entry entry : map.entrySet()) { if (filter.test(entry)) { if (keysToRemove == null) { keysToRemove = new ArrayList<>(7); } keysToRemove.add(entry.getKey()); } } if (N.notNullOrEmpty(keysToRemove)) { for (K key : keysToRemove) { map.remove(key); } return true; } return false; } /** * Removes entries from the specified {@code map} by the the specified {@code filter}. * * @param the key type * @param the value type * @param * @param map * @param filter * @return {@code true} if there are one or more than one entries removed from the specified map. * @throws E the e */ public static boolean removeIfKey(final Map map, final Throwables.Predicate filter) throws E { List keysToRemove = null; for (Map.Entry entry : map.entrySet()) { if (filter.test(entry.getKey())) { if (keysToRemove == null) { keysToRemove = new ArrayList<>(7); } keysToRemove.add(entry.getKey()); } } if (N.notNullOrEmpty(keysToRemove)) { for (K key : keysToRemove) { map.remove(key); } return true; } return false; } /** * Removes entries from the specified {@code map} by the the specified {@code filter}. * * @param the key type * @param the value type * @param * @param map * @param filter * @return {@code true} if there are one or more than one entries removed from the specified map. * @throws E the e */ public static boolean removeIfValue(final Map map, final Throwables.Predicate filter) throws E { List keysToRemove = null; for (Map.Entry entry : map.entrySet()) { if (filter.test(entry.getValue())) { if (keysToRemove == null) { keysToRemove = new ArrayList<>(7); } keysToRemove.add(entry.getKey()); } } if (N.notNullOrEmpty(keysToRemove)) { for (K key : keysToRemove) { map.remove(key); } return true; } return false; } /** * * @param the key type * @param the value type * @param map * @param key * @param oldValue * @param newValue * @return */ public static boolean replace(final Map map, final K key, final V oldValue, final V newValue) { if (N.isNullOrEmpty(map)) { return false; } final Object curValue = map.get(key); if (!N.equals(curValue, oldValue) || (curValue == null && !map.containsKey(key))) { return false; } map.put(key, newValue); return true; } /** * * @param the key type * @param the value type * @param map * @param key * @param newValue * @return */ public static V replace(final Map map, final K key, final V newValue) { if (N.isNullOrEmpty(map)) { return null; } V curValue = null; if (((curValue = map.get(key)) != null) || map.containsKey(key)) { curValue = map.put(key, newValue); } return curValue; } /** * * @param the key type * @param the value type * @param * @param map * @param function * @throws E the e */ public static void replaceAll(final Map map, final Throwables.BiFunction function) throws E { N.checkArgNotNull(function); if (N.isNullOrEmpty(map)) { return; } K k = null; V v = null; for (Map.Entry entry : map.entrySet()) { try { k = entry.getKey(); v = entry.getValue(); } catch (IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } // ise thrown from function is not a cme. v = function.apply(k, v); try { entry.setValue(v); } catch (IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } } } public static void forEach(final Map map, final Throwables.Consumer, E> action) throws E { N.checkArgNotNull(action); if (N.isNullOrEmpty(map)) { return; } for (Map.Entry entry : map.entrySet()) { action.accept(entry); } } /** * * @param the key type * @param the value type * @param * @param map * @param action * @throws E the e */ public static void forEach(final Map map, final Throwables.BiConsumer action) throws E { N.checkArgNotNull(action); if (N.isNullOrEmpty(map)) { return; } for (Map.Entry entry : map.entrySet()) { action.accept(entry.getKey(), entry.getValue()); } } /** * * @param the key type * @param the value type * @param * @param map * @param predicate * @return * @throws E the e */ public static Map filter(final Map map, final Throwables.BiPredicate predicate) throws E { if (map == null) { return new HashMap<>(); } final Map result = newTargetMap(map, 0); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getKey(), entry.getValue())) { result.put(entry.getKey(), entry.getValue()); } } return result; } /** * Filter by key. * * @param the key type * @param the value type * @param * @param map * @param predicate * @return * @throws E the e */ public static Map filterByKey(final Map map, final Throwables.Predicate predicate) throws E { if (map == null) { return new HashMap<>(); } final Map result = newTargetMap(map, 0); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; } /** * Filter by value. * * @param the key type * @param the value type * @param * @param map * @param predicate * @return * @throws E the e */ public static Map filterByValue(final Map map, final Throwables.Predicate predicate) throws E { if (map == null) { return new HashMap<>(); } final Map result = newTargetMap(map, 0); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getValue())) { result.put(entry.getKey(), entry.getValue()); } } return result; } /** * * @param the key type * @param the value type * @param map * @return * @see Multimap#invertFrom(Map, com.landawn.abacus.util.function.Supplier) * @see ListMultimap#invertFrom(Map) * @see ListMultimap#invertFrom(Map) */ public static Map invert(final Map map) { if (map == null) { return new HashMap<>(); } final Map result = newOrderingMap(map); for (Map.Entry entry : map.entrySet()) { result.put(entry.getValue(), entry.getKey()); } return result; } /** * * @param the key type * @param the value type * @param * @param map * @param mergeOp * @return * @throws E the e */ public static Map invert(final Map map, Throwables.BinaryOperator mergeOp) throws E { N.checkArgNotNull(mergeOp, "mergeOp"); if (map == null) { return new HashMap<>(); } final Map result = newOrderingMap(map); K oldVal = null; for (Map.Entry entry : map.entrySet()) { oldVal = result.get(entry.getValue()); if (oldVal != null || result.containsKey(entry.getValue())) { result.put(entry.getValue(), mergeOp.apply(oldVal, entry.getKey())); } else { result.put(entry.getValue(), entry.getKey()); } } return result; } /** * * @param the key type * @param the value type * @param map * @return * @see Multimap#flatInvertFrom(Map, com.landawn.abacus.util.function.Supplier) * @see ListMultimap#flatInvertFrom(Map) * @see SetMultimap#flatInvertFrom(Map) */ public static Map> flatInvert(final Map> map) { if (map == null) { return new HashMap<>(); } final Map> result = newOrderingMap(map); for (Map.Entry> entry : map.entrySet()) { final Collection c = entry.getValue(); if (N.notNullOrEmpty(c)) { for (V v : c) { List list = result.get(v); if (list == null) { list = new ArrayList<>(); result.put(v, list); } list.add(entry.getKey()); } } } return result; } /** * {a=[1, 2, 3], b=[4, 5, 6], c=[7, 8]} -> [{a=1, b=4, c=7}, {a=2, b=5, c=8}, {a=3, b=6}] * * @param * @param * @param map * @return */ public static List> flatToMap(final Map> map) { if (map == null) { return new ArrayList<>(); } int maxValueSize = 0; for (Collection v : map.values()) { maxValueSize = N.max(maxValueSize, N.size(v)); } final List> result = new ArrayList<>(maxValueSize); for (int i = 0; i < maxValueSize; i++) { result.add(newOrderingMap(map)); } K key = null; Iterator iter = null; for (Map.Entry> entry : map.entrySet()) { if (N.isNullOrEmpty(entry.getValue())) { continue; } key = entry.getKey(); iter = entry.getValue().iterator(); for (int i = 0; iter.hasNext(); i++) { result.get(i).put(key, iter.next()); } } return result; } /** * * @param map * @return */ public static Map flatten(Map map) { return flatten(map, Suppliers. ofMap()); } /** * * @param * @param map * @param mapSupplier * @return */ public static > M flatten(Map map, Supplier mapSupplier) { return flatten(map, ".", mapSupplier); } /** * * @param * @param map * @param delimiter * @param mapSupplier * @return */ public static > M flatten(Map map, String delimiter, Supplier mapSupplier) { final M result = mapSupplier.get(); flatten(map, null, delimiter, result); return result; } /** * * @param map * @param prefix * @param delimiter * @param output */ private static void flatten(Map map, String prefix, String delimiter, Map output) { if (N.isNullOrEmpty(map)) { return; } if (N.isNullOrEmpty(prefix)) { for (Map.Entry entry : map.entrySet()) { if (entry.getValue() instanceof Map) { flatten((Map) entry.getValue(), entry.getKey(), delimiter, output); } else { output.put(entry.getKey(), entry.getValue()); } } } else { for (Map.Entry entry : map.entrySet()) { if (entry.getValue() instanceof Map) { flatten((Map) entry.getValue(), prefix + delimiter + entry.getKey(), delimiter, output); } else { output.put(prefix + delimiter + entry.getKey(), entry.getValue()); } } } } /** * * @param map * @return */ public static Map unflatten(Map map) { return unflatten(map, Suppliers. ofMap()); } /** * * @param * @param map * @param mapSupplier * @return */ public static > M unflatten(Map map, Supplier mapSupplier) { return unflatten(map, ".", mapSupplier); } /** * * @param * @param map * @param delimiter * @param mapSupplier * @return */ public static > M unflatten(Map map, String delimiter, Supplier mapSupplier) { final M result = mapSupplier.get(); final Splitter keySplitter = Splitter.with(delimiter); if (N.notNullOrEmpty(map)) { for (Map.Entry entry : map.entrySet()) { if (entry.getKey().indexOf(delimiter) >= 0) { final String[] keys = keySplitter.splitToArray(entry.getKey()); Map lastMap = result; for (int i = 0, to = keys.length - 1; i < to; i++) { Map tmp = (Map) lastMap.get(keys[i]); if (tmp == null) { tmp = mapSupplier.get(); lastMap.put(keys[i], tmp); } lastMap = tmp; } lastMap.put(keys[keys.length - 1], entry.getValue()); } else { result.put(entry.getKey(), entry.getValue()); } } } return result; } /** * Map type 2 supplier. * * @param mapType * @return */ @SuppressWarnings("rawtypes") static Supplier mapType2Supplier(final Class mapType) { if (HashMap.class.equals(mapType)) { return Suppliers.ofMap(); } else if (SortedMap.class.isAssignableFrom(mapType)) { return Suppliers.ofTreeMap(); } else if (IdentityHashMap.class.isAssignableFrom(mapType)) { return Suppliers.ofIdentityHashMap(); } else if (LinkedHashMap.class.isAssignableFrom(mapType)) { return Suppliers.ofLinkedHashMap(); } else if (ImmutableMap.class.isAssignableFrom(mapType)) { return Suppliers.ofLinkedHashMap(); } else { return new Supplier() { @Override public Map get() { try { return N.newInstance(mapType); } catch (Exception e) { return new LinkedHashMap<>(); } } }; } } /** * * @param the key type * @param the value type * @param map * @param function */ static void replaceAll(Map map, BiFunction function) { N.checkArgNotNull(function); try { for (Map.Entry entry : map.entrySet()) { entry.setValue(function.apply(entry.getKey(), entry.getValue())); } } catch (IllegalStateException ise) { throw new ConcurrentModificationException(ise); } } /** * * @param the key type * @param the value type * @param * @param map * @param key * @param value * @param remappingFunction * @throws E the e */ static void merge(Map map, K key, V value, Throwables.BinaryOperator remappingFunction) throws E { final V oldValue = map.get(key); if (oldValue == null && map.containsKey(key) == false) { map.put(key, value); } else { map.put(key, remappingFunction.apply(oldValue, value)); } } /** * Map to entity. * * @param * @param targetClass * @param m * @return */ public static T map2Entity(final Class targetClass, final Map m) { return map2Entity(targetClass, m, false, true); } /** * Map to entity. * * @param * @param targetClass * @param m * @param ignoreNullProperty * @param ignoreUnmatchedProperty * @return */ @SuppressWarnings("unchecked") public static T map2Entity(final Class targetClass, final Map m, final boolean ignoreNullProperty, final boolean ignoreUnmatchedProperty) { checkEntityClass(targetClass); final T entity = N.newInstance(targetClass); final EntityInfo entityInfo = ParserUtil.getEntityInfo(targetClass); PropInfo propInfo = null; String propName = null; Object propValue = null; for (Map.Entry entry : m.entrySet()) { propName = entry.getKey(); propValue = entry.getValue(); if (ignoreNullProperty && (propValue == null)) { continue; } propInfo = entityInfo.getPropInfo(propName); if (propInfo == null) { entityInfo.setPropValue(entity, propName, propValue, ignoreUnmatchedProperty); } else { if (propValue != null && N.typeOf(propValue.getClass()).isMap() && propInfo.type.isEntity()) { propInfo.setPropValue(entity, map2Entity(propInfo.clazz, (Map) propValue, ignoreNullProperty, ignoreUnmatchedProperty)); } else { propInfo.setPropValue(entity, propValue); } } } return entity; } /** * Map to entity. * * @param * @param targetClass * @param m * @param selectPropNames * @return */ public static T map2Entity(final Class targetClass, final Map m, final Collection selectPropNames) { checkEntityClass(targetClass); final T entity = N.newInstance(targetClass); final EntityInfo entityInfo = ParserUtil.getEntityInfo(targetClass); PropInfo propInfo = null; Object propValue = null; for (String propName : selectPropNames) { propValue = m.get(propName); if (propValue == null && m.containsKey(propName) == false) { throw new IllegalArgumentException("Property name: " + propName + " is not found in map with key set: " + m.keySet()); } propInfo = entityInfo.getPropInfo(propName); if (propInfo == null) { entityInfo.setPropValue(entity, propName, propValue, false); } else { if (propValue != null && N.typeOf(propValue.getClass()).isMap() && propInfo.type.isEntity()) { propInfo.setPropValue(entity, map2Entity(propInfo.clazz, (Map) propValue)); } else { propInfo.setPropValue(entity, propValue); } } } return entity; } /** * Map to entity. * * @param * @param targetClass * @param mList * @return */ public static List map2Entity(final Class targetClass, final Collection> mList) { return map2Entity(targetClass, mList, false, true); } /** * Map to entity. * * @param * @param targetClass * @param mList * @param igoreNullProperty * @param ignoreUnmatchedProperty * @return */ public static List map2Entity(final Class targetClass, final Collection> mList, final boolean igoreNullProperty, final boolean ignoreUnmatchedProperty) { checkEntityClass(targetClass); final List entityList = new ArrayList<>(mList.size()); for (Map m : mList) { entityList.add(map2Entity(targetClass, m, igoreNullProperty, ignoreUnmatchedProperty)); } return entityList; } /** * Map to entity. * * @param * @param targetClass * @param mList * @param selectPropNames * @return */ public static List map2Entity(final Class targetClass, final Collection> mList, final Collection selectPropNames) { checkEntityClass(targetClass); final List entityList = new ArrayList<>(mList.size()); for (Map m : mList) { entityList.add(map2Entity(targetClass, m, selectPropNames)); } return entityList; } /** * Entity to map. * * @param entity * @return */ public static Map entity2Map(final Object entity) { return entity2Map(entity, false); } /** * Entity to map. * * @param entity * @param ignoreNullProperty * @return */ public static Map entity2Map(final Object entity, final boolean ignoreNullProperty) { return entity2Map(entity, ignoreNullProperty, null); } /** * Entity to map. * * @param entity * @param ignoredPropNames * @return */ public static Map entity2Map(final Object entity, final Collection ignoredPropNames) { return entity2Map(entity, false, ignoredPropNames); } /** * Entity to map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static Map entity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity to map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static Map entity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = (entity instanceof DirtyMarker ? DirtyMarkerUtil.signedPropNames((DirtyMarker) entity).size() : ClassUtil.getPropNameList(entity.getClass()).size()); final Map resultMap = N.newLinkedHashMap(initCapacity); entity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * Entity to map. * * @param * @param entity * @param mapSupplier * @return */ public static > M entity2Map(final Object entity, final Supplier mapSupplier) { return entity2Map(mapSupplier.get(), entity); } /** * Entity to map. * * @param * @param resultMap * @param entity * @return */ public static > M entity2Map(final M resultMap, final Object entity) { return entity2Map(resultMap, entity, false); } /** * Entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @return */ public static > M entity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty) { return entity2Map(resultMap, entity, ignoreNullProperty, null); } /** * Entity to map. * * @param * @param resultMap * @param entity * @param ignoredPropNames * @return */ public static > M entity2Map(final M resultMap, final Object entity, final Collection ignoredPropNames) { return entity2Map(resultMap, entity, false, ignoredPropNames); } /** * Entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static > M entity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static > M entity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, NamingPolicy keyNamingPolicy) { keyNamingPolicy = keyNamingPolicy == null ? NamingPolicy.LOWER_CAMEL_CASE : keyNamingPolicy; final boolean isLowerCamelCaseOrNoChange = NamingPolicy.LOWER_CAMEL_CASE.equals(keyNamingPolicy) || NamingPolicy.NO_CHANGE.equals(keyNamingPolicy); final boolean hasIgnoredPropNames = N.notNullOrEmpty(ignoredPropNames); final Class entityClass = entity.getClass(); final EntityInfo entityInfo = ParserUtil.getEntityInfo(entityClass); if (entity instanceof DirtyMarker) { final Set signedPropNames = DirtyMarkerUtil.signedPropNames((DirtyMarker) entity); if (N.notNullOrEmpty(signedPropNames)) { Object propValue = null; PropInfo propInfo = null; for (String propName : signedPropNames) { propInfo = entityInfo.getPropInfo(propName); propName = propInfo.name; if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propInfo.getPropValue(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(propName), propValue); } } } } else { String propName = null; Object propValue = null; for (PropInfo propInfo : entityInfo.propInfoList) { propName = propInfo.name; if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propInfo.getPropValue(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(propName), propValue); } } } return resultMap; } /** * Entity to map. * * @param entityList * @return */ public static List> entity2Map(final Collection entityList) { return entity2Map(entityList, false); } /** * Entity to map. * * @param entityList * @param ignoreNullProperty * @return */ public static List> entity2Map(final Collection entityList, final boolean ignoreNullProperty) { return entity2Map(entityList, ignoreNullProperty, null); } /** * Entity to map. * * @param entityList * @param ignoredPropNames * @return */ public static List> entity2Map(final Collection entityList, final Collection ignoredPropNames) { return entity2Map(entityList, false, ignoredPropNames); } /** * Entity to map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static List> entity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity to map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static List> entity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final List> resultList = new ArrayList<>(entityList.size()); for (Object entity : entityList) { resultList.add(entity2Map(entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy)); } return resultList; } /** * Deep entity to map. * * @param entity * @return */ public static Map deepEntity2Map(final Object entity) { return deepEntity2Map(entity, false); } /** * Deep entity to map. * * @param entity * @param ignoreNullProperty * @return */ public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty) { return deepEntity2Map(entity, ignoreNullProperty, null); } /** * Deep entity to map. * * @param entity * @param ignoredPropNames * @return */ public static Map deepEntity2Map(final Object entity, final Collection ignoredPropNames) { return deepEntity2Map(entity, false, ignoredPropNames); } /** * Deep entity to map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Deep entity to map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = entity instanceof DirtyMarker ? DirtyMarkerUtil.signedPropNames((DirtyMarker) entity).size() : ClassUtil.getPropNameList(entity.getClass()).size(); final Map resultMap = N.newLinkedHashMap(initCapacity); deepEntity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * Deep entity to map. * * @param * @param entity * @param mapSupplier * @return */ public static > M deepEntity2Map(final Object entity, final Supplier mapSupplier) { return deepEntity2Map(mapSupplier.get(), entity); } /** * Deep entity to map. * * @param * @param resultMap * @param entity * @return */ public static > M deepEntity2Map(final M resultMap, final Object entity) { return deepEntity2Map(resultMap, entity, false); } /** * Deep entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @return */ public static > M deepEntity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty) { return deepEntity2Map(resultMap, entity, ignoreNullProperty, null); } /** * Deep entity to map. * * @param * @param resultMap * @param entity * @param ignoredPropNames * @return */ public static > M deepEntity2Map(final M resultMap, final Object entity, final Collection ignoredPropNames) { return deepEntity2Map(resultMap, entity, false, ignoredPropNames); } /** * Deep entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static > M deepEntity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Deep entity to map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static > M deepEntity2Map(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, NamingPolicy keyNamingPolicy) { keyNamingPolicy = keyNamingPolicy == null ? NamingPolicy.LOWER_CAMEL_CASE : keyNamingPolicy; final boolean isLowerCamelCaseOrNoChange = NamingPolicy.LOWER_CAMEL_CASE.equals(keyNamingPolicy) || NamingPolicy.NO_CHANGE.equals(keyNamingPolicy); final boolean hasIgnoredPropNames = N.notNullOrEmpty(ignoredPropNames); final Class entityClass = entity.getClass(); final EntityInfo entityInfo = ParserUtil.getEntityInfo(entityClass); if (entity instanceof DirtyMarker) { final Set signedPropNames = DirtyMarkerUtil.signedPropNames((DirtyMarker) entity); if (N.notNullOrEmpty(signedPropNames)) { Object propValue = null; PropInfo propInfo = null; for (String propName : signedPropNames) { propInfo = entityInfo.getPropInfo(propName); propName = propInfo.name; if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propInfo.getPropValue(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !propInfo.jsonXmlType.isEntity()) { if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(propName), propValue); } } else { if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } else { resultMap.put(keyNamingPolicy.convert(propName), deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } } } } } else { Object propValue = null; String propName = null; for (PropInfo propInfo : entityInfo.propInfoList) { propName = propInfo.name; if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propInfo.getPropValue(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !propInfo.jsonXmlType.isEntity()) { if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(propName), propValue); } } else { if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } else { resultMap.put(keyNamingPolicy.convert(propName), deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } } } } return resultMap; } /** * Deep entity to map. * * @param entityList * @return */ public static List> deepEntity2Map(final Collection entityList) { return deepEntity2Map(entityList, false); } /** * Deep entity to map. * * @param entityList * @param ignoreNullProperty * @return */ public static List> deepEntity2Map(final Collection entityList, final boolean ignoreNullProperty) { return deepEntity2Map(entityList, ignoreNullProperty, null); } /** * Deep entity to map. * * @param entityList * @param ignoredPropNames * @return */ public static List> deepEntity2Map(final Collection entityList, final Collection ignoredPropNames) { final boolean ignoreNullProperty = N.isNullOrEmpty(entityList) ? true : (entityList instanceof ArrayList ? ((ArrayList) entityList).get(0) : entityList.iterator().next()) instanceof DirtyMarker == false; return deepEntity2Map(entityList, ignoreNullProperty, ignoredPropNames); } /** * Deep entity to map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static List> deepEntity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Deep entity to map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static List> deepEntity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final List> resultList = new ArrayList<>(entityList.size()); for (Object entity : entityList) { resultList.add(deepEntity2Map(entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy)); } return resultList; } /** * Entity 2 flat map. * * @param entity * @return */ public static Map entity2FlatMap(final Object entity) { return entity2FlatMap(entity, false); } /** * Entity 2 flat map. * * @param entity * @param ignoreNullProperty * @return */ public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty) { return entity2FlatMap(entity, ignoreNullProperty, null); } /** * Entity 2 flat map. * * @param entity * @param ignoredPropNames * @return */ public static Map entity2FlatMap(final Object entity, final Collection ignoredPropNames) { return entity2FlatMap(entity, false, ignoredPropNames); } /** * Entity 2 flat map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity 2 flat map. * * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = entity instanceof DirtyMarker ? DirtyMarkerUtil.signedPropNames((DirtyMarker) entity).size() : ClassUtil.getPropNameList(entity.getClass()).size(); final Map resultMap = N.newLinkedHashMap(initCapacity); entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * Entity 2 flat map. * * @param * @param entity * @param mapSupplier * @return */ public static > M entity2FlatMap(final Object entity, final Supplier mapSupplier) { return entity2FlatMap(mapSupplier.get(), entity); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @return */ public static > M entity2FlatMap(final M resultMap, final Object entity) { return entity2FlatMap(resultMap, entity, false); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @return */ public static > M entity2FlatMap(final M resultMap, final Object entity, final boolean ignoreNullProperty) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, null); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @param ignoredPropNames * @return */ public static > M entity2FlatMap(final M resultMap, final Object entity, final Collection ignoredPropNames) { return entity2FlatMap(resultMap, entity, false, ignoredPropNames); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static > M entity2FlatMap(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static > M entity2FlatMap(final M resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy, null); } /** * Entity 2 flat map. * * @param * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @param parentPropName * @return */ static > T entity2FlatMap(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy, final String parentPropName) { final boolean hasIgnoredPropNames = N.notNullOrEmpty(ignoredPropNames); final boolean isNullParentPropName = (parentPropName == null); final Class entityClass = entity.getClass(); Set signedPropNames = null; if (entity instanceof DirtyMarker) { signedPropNames = DirtyMarkerUtil.signedPropNames((DirtyMarker) entity); if (signedPropNames.size() == 0) { // logger.warn("no property is signed in the specified source entity: " // + toString(entity)); return resultMap; } else { final Set tmp = N.newHashSet(signedPropNames.size()); for (String propName : signedPropNames) { tmp.add(ClassUtil.getPropNameByMethod(ClassUtil.getPropGetMethod(entityClass, propName))); } signedPropNames = tmp; } } final boolean isLowerCamelCaseOrNoChange = NamingPolicy.LOWER_CAMEL_CASE.equals(keyNamingPolicy) || NamingPolicy.NO_CHANGE.equals(keyNamingPolicy); String propName = null; Object propValue = null; for (PropInfo propInfo : ParserUtil.getEntityInfo(entityClass).propInfoList) { propName = propInfo.name; if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propInfo.getPropValue(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !propInfo.jsonXmlType.isEntity()) { if (isNullParentPropName) { if (isLowerCamelCaseOrNoChange) { resultMap.put(propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(propName), propValue); } } else { if (isLowerCamelCaseOrNoChange) { resultMap.put(parentPropName + WD.PERIOD + propName, propValue); } else { resultMap.put(keyNamingPolicy.convert(parentPropName + WD.PERIOD + propName), propValue); } } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + WD.PERIOD + propName); } } } return resultMap; } /** * Entity 2 flat map. * * @param entityList * @return */ public static List> entity2FlatMap(final Collection entityList) { return entity2FlatMap(entityList, false); } /** * Entity 2 flat map. * * @param entityList * @param ignoreNullProperty * @return */ public static List> entity2FlatMap(final Collection entityList, final boolean ignoreNullProperty) { return entity2FlatMap(entityList, ignoreNullProperty, null); } /** * Entity 2 flat map. * * @param entityList * @param ignoredPropNames * @return */ public static List> entity2FlatMap(final Collection entityList, final Collection ignoredPropNames) { return entity2FlatMap(entityList, false, ignoredPropNames); } /** * Entity 2 flat map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @return */ public static List> entity2FlatMap(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.LOWER_CAMEL_CASE); } /** * Entity 2 flat map. * * @param entityList * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return */ public static List> entity2FlatMap(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final List> resultList = new ArrayList<>(entityList.size()); for (Object entity : entityList) { resultList.add(entity2FlatMap(entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy)); } return resultList; } /** * Check entity class. * * @param * @param cls */ private static void checkEntityClass(final Class cls) { if (!ClassUtil.isEntity(cls)) { throw new IllegalArgumentException("No property getter/setter method is found in the specified class: " + ClassUtil.getCanonicalClassName(cls)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy