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

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

There is a newer version: 1.10.1
Show newest version
/*
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package com.landawn.abacus.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.landawn.abacus.DirtyMarker;
import com.landawn.abacus.exception.AbacusException;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BiPredicate;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;

/**
 * Note: It's copied from OpenJDK at: http://hg.openjdk.java.net/jdk8u/hs-dev/jdk
 * 
* * Import the useful default Map methods from JDK 1.8 for programming on JDK 7 and Android. * */ public final class Maps { private Maps() { // singleton. } public static Map asMap(Collection c, final Function keyExtractor) { N.requireNonNull(keyExtractor); if (N.isNullOrEmpty(c)) { return new HashMap(); } final Map result = new HashMap<>(N.initHashCapacity(c.size())); for (T e : c) { result.put(keyExtractor.apply(e), e); } return result; } public static Map asLinkedHashMap(Collection c, final Function keyExtractor) { N.requireNonNull(keyExtractor); if (N.isNullOrEmpty(c)) { return new LinkedHashMap(); } final Map result = new LinkedHashMap<>(N.initHashCapacity(c.size())); for (T e : c) { result.put(keyExtractor.apply(e), e); } return result; } 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 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. * * @implSpec * The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key the key whose associated value is to be returned * @param defaultValue the default mapping of the key * @return the value to which the specified key is mapped, or * {@code defaultValue} if this map contains no mapping for the key * @throws ClassCastException if the key is of an inappropriate type for * this map * (optional) * @throws NullPointerException if the specified key is null and this map * does not permit null keys * (optional) * @since 1.8 */ 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; } } 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 new ArrayList<>(Arrays.asList(Array.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; } /** * 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()); } 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); } 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; } public static Map diff(final Map map, final Map map2) { if (N.isNullOrEmpty(map)) { return new LinkedHashMap<>(); } else if (N.isNullOrEmpty(map2)) { return new LinkedHashMap<>(map); } 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 && map2.containsKey(entry.getKey()) == false) || N.equals(val, entry.getValue()) == false) { result.put(entry.getKey(), entry.getValue()); } } return result; } 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; } 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, NullabLe>>() : new LinkedHashMap, NullabLe>>(); 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; } /** * If the specified key is not already associated with a value (or is mapped * to {@code null}) associates it with the given value and returns * {@code null}, else returns the current value. * * @implSpec * The default implementation is equivalent to, for this {@code * map}: * *
 {@code
     * V v = map.get(key);
     * if (v == null)
     *     v = map.put(key, value);
     *
     * return v;
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * {@code null} if there was no mapping for the key. * (A {@code null} return can also indicate that the map * previously associated {@code null} with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (optional) * @throws NullPointerException if the specified map is null, or if the specified key or value is null, * and this map does not permit null keys or values * (optional) * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map * (optional) * @since 1.8 */ 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; } public static boolean remove(final Map map, Map.Entry entry) { return remove(map, entry.getKey(), entry.getValue()); } /** * Removes the entry for the specified key only if it is currently * mapped to the specified value. * * @implSpec * The default implementation is equivalent to, for this {@code map}: * *

 {@code
     * if (map.containsKey(key) && N.equals(map.get(key), value)) {
     *     map.remove(key);
     *     return true;
     * } else
     *     return false;
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key key with which the specified value is associated * @param value value expected to be associated with the specified key * @return {@code true} if the value was removed * @throws UnsupportedOperationException if the {@code remove} operation * is not supported by this map * (optional) * @throws ClassCastException if the key or value is of an inappropriate * type for this map * (optional) * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * (optional) * @since 1.8 */ 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; } /** * * @param map * @param keys * @return true if any key/value was removed, otherwise false. */ public static boolean removeAll(final Map map, final Collection keys) { if (N.isNullOrEmpty(map) || N.isNullOrEmpty(keys)) { return false; } final int originalSize = map.size(); for (Object key : keys) { 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 removeAll(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; } /** * Replaces the entry for the specified key only if currently * mapped to the specified value. * * @implSpec * The default implementation is equivalent to, for this {@code map}: * *

 {@code
     * if (map.containsKey(key) && N.equals(map.get(key), value)) {
     *     map.put(key, newValue);
     *     return true;
     * } else
     *     return false;
     * }
* * The default implementation does not throw NullPointerException * for maps that do not support null values if oldValue is null unless * newValue is also null. * *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key key with which the specified value is associated * @param oldValue value expected to be associated with the specified key * @param newValue value to be associated with the specified key * @return {@code true} if the value was replaced * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of a specified key or value * prevents it from being stored in this map * @throws NullPointerException if a specified key or newValue is null, * and this map does not permit null keys or values * @throws NullPointerException if oldValue is null and this map does not * permit null values * (optional) * @throws IllegalArgumentException if some property of a specified key * or value prevents it from being stored in this map * @since 1.8 */ 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; } /** * Replaces the entry for the specified key only if it is * currently mapped to some value. * * @implSpec * The default implementation is equivalent to, for this {@code map}: * *

 {@code
     * if (map.containsKey(key)) {
     *     return map.put(key, value);
     * } else
     *     return null;
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * {@code null} if there was no mapping for the key. * (A {@code null} return can also indicate that the map * previously associated {@code null} with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map * @since 1.8 */ public static V replace(final Map map, final K key, final V value) { if (N.isNullOrEmpty(map)) { return null; } V curValue = null; if (((curValue = map.get(key)) != null) || map.containsKey(key)) { curValue = map.put(key, value); } return curValue; } /** * Replaces each entry's value with the result of invoking the given * function on that entry until all entries have been processed or the * function throws an exception. Exceptions thrown by the function are * relayed to the caller. * * @implSpec *

The default implementation is equivalent to, for this {@code map}: *

 {@code
     * for (Map.Entry entry : map.entrySet())
     *     entry.setValue(function.apply(entry.getKey(), entry.getValue()));
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param function the function to apply to each entry * @throws UnsupportedOperationException if the {@code set} operation * is not supported by this map's entry set iterator. * @throws ClassCastException if the class of a replacement value * prevents it from being stored in this map * @throws NullPointerException if the specified function is null, or the * specified replacement value is null, and this map does not permit null * values * @throws ClassCastException if a replacement value is of an inappropriate * type for this map * (optional) * @throws NullPointerException if function or a replacement value is null, * and this map does not permit null keys or values * (optional) * @throws IllegalArgumentException if some property of a replacement value * prevents it from being stored in this map * (optional) * @throws ConcurrentModificationException if an entry is found to be * removed during iteration * @since 1.8 */ public static void replaceAll(final Map map, final BiFunction function) { N.requireNonNull(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); } } } /** * If the specified key is not already associated with a value (or is mapped * to {@code null}), attempts to compute its value using the given mapping * function and enters it into this map unless {@code null}. * *

If the function returns {@code null} no mapping is recorded. If * the function itself throws an (unchecked) exception, the * exception is rethrown, and no mapping is recorded. The most * common usage is to construct a new object serving as an initial * mapped value or memoized result, as in: * *

 {@code
     * map.computeIfAbsent(key, k -> new Value(f(k)));
     * }
* *

Or to implement a multi-value map, {@code Map>}, * supporting multiple values per key: * *

 {@code
     * map.computeIfAbsent(key, k -> new HashSet()).add(v);
     * }
* * * @implSpec * The default implementation is equivalent to the following steps for this * {@code map}, then returning the current value or {@code null} if now * absent: * *
 {@code
     * if (map.get(key) == null) {
     *     V newValue = mappingFunction.apply(key);
     *     if (newValue != null)
     *         map.put(key, newValue);
     * }
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. In particular, all implementations of * subinterface {@link java.util.concurrent.ConcurrentMap} must document * whether the function is applied once atomically only if the value is not * present. * * @param key key with which the specified value is to be associated * @param mappingFunction the function to compute a value * @return the current (existing or computed) value associated with * the specified key, or null if the computed value is null * @throws NullPointerException if the specified map is null, or the specified key is null and * this map does not support null keys, or the mappingFunction * is null * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) * @since 1.8 */ public static V computeIfAbsent(final Map map, final K key, final Function mappingFunction) { N.requireNonNull(mappingFunction); V v = null; if ((v = map.get(key)) == null) { V newValue = null; if ((newValue = mappingFunction.apply(key)) != null) { map.put(key, newValue); return newValue; } } return v; } /** * If the value for the specified key is present and non-null, attempts to * compute a new mapping given the key and its current mapped value. * *

If the function returns {@code null}, the mapping is removed. If the * function itself throws an (unchecked) exception, the exception is * rethrown, and the current mapping is left unchanged. * * @implSpec * The default implementation is equivalent to performing the following * steps for this {@code map}, then returning the current value or * {@code null} if now absent: * *

 {@code
     * if (map.get(key) != null) {
     *     V oldValue = map.get(key);
     *     V newValue = remappingFunction.apply(key, oldValue);
     *     if (newValue != null)
     *         map.put(key, newValue);
     *     else
     *         map.remove(key);
     * }
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. In particular, all implementations of * subinterface {@link java.util.concurrent.ConcurrentMap} must document * whether the function is applied once atomically only if the value is not * present. * * @param key key with which the specified value is to be associated * @param remappingFunction the function to compute a value * @return the new value associated with the specified key, or null if none * @throws NullPointerException if the specified map is null, or the specified key is null and * this map does not support null keys, or the * remappingFunction is null * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) * @since 1.8 */ public static V computeIfPresent(final Map map, K key, final BiFunction remappingFunction) { N.requireNonNull(remappingFunction); V oldValue = null; if ((oldValue = map.get(key)) != null) { V newValue = remappingFunction.apply(key, oldValue); if (newValue != null) { map.put(key, newValue); return newValue; } else { map.remove(key); return null; } } else { return null; } } /** * Attempts to compute a mapping for the specified key and its current * mapped value (or {@code null} if there is no current mapping). For * example, to either create or append a {@code String} msg to a value * mapping: * *

 {@code
     * map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))}
* (Method {@link #merge merge()} is often simpler to use for such purposes.) * *

If the function returns {@code null}, the mapping is removed (or * remains absent if initially absent). If the function itself throws an * (unchecked) exception, the exception is rethrown, and the current mapping * is left unchanged. * * @implSpec * The default implementation is equivalent to performing the following * steps for this {@code map}, then returning the current value or * {@code null} if absent: * *

 {@code
     * V oldValue = map.get(key);
     * V newValue = remappingFunction.apply(key, oldValue);
     * if (oldValue != null ) {
     *    if (newValue != null)
     *       map.put(key, newValue);
     *    else
     *       map.remove(key);
     * } else {
     *    if (newValue != null)
     *       map.put(key, newValue);
     *    else
     *       return null;
     * }
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. In particular, all implementations of * subinterface {@link java.util.concurrent.ConcurrentMap} must document * whether the function is applied once atomically only if the value is not * present. * * @param key key with which the specified value is to be associated * @param remappingFunction the function to compute a value * @return the new value associated with the specified key, or null if none * @throws NullPointerException if the specified map is null, or the specified key is null and * this map does not support null keys, or the * remappingFunction is null * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) * @since 1.8 */ public static V compute(final Map map, K key, final BiFunction remappingFunction) { N.requireNonNull(remappingFunction); V oldValue = map.get(key); V newValue = remappingFunction.apply(key, oldValue); if (newValue == null) { // delete mapping if (oldValue != null || map.containsKey(key)) { // something to remove map.remove(key); return null; } else { // nothing to do. Leave things as they were. return null; } } else { // add or replace old mapping map.put(key, newValue); return newValue; } } /** * If the specified key is not already associated with a value or is * associated with null, associates it with the given non-null value. * Otherwise, replaces the associated value with the results of the given * remapping function, or removes if the result is {@code null}. This * method may be of use when combining multiple mapped values for a key. * For example, to either create or append a {@code String msg} to a * value mapping: * *

 {@code
     * map.merge(key, msg, String::concat)
     * }
* *

If the function returns {@code null} the mapping is removed. If the * function itself throws an (unchecked) exception, the exception is * rethrown, and the current mapping is left unchanged. * * @implSpec * The default implementation is equivalent to performing the following * steps for this {@code map}, then returning the current value or * {@code null} if absent: * *

 {@code
     * V oldValue = map.get(key);
     * V newValue = (oldValue == null) ? value :
     *              remappingFunction.apply(oldValue, value);
     * if (newValue == null)
     *     map.remove(key);
     * else
     *     map.put(key, newValue);
     * }
* *

The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. In particular, all implementations of * subinterface {@link java.util.concurrent.ConcurrentMap} must document * whether the function is applied once atomically only if the value is not * present. * * @param key key with which the resulting value is to be associated * @param value the non-null value to be merged with the existing value * associated with the key or, if no existing value or a null value * is associated with the key, to be associated with the key * @param remappingFunction the function to recompute a value if present * @return the new value associated with the specified key, or null if no * value is associated with the key * @throws UnsupportedOperationException if the {@code put} operation * is not supported by this map * (optional) * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * (optional) * @throws NullPointerException if the specified map is null, or the specified key is null and this map * does not support null keys or the value or remappingFunction is * null * @since 1.8 */ public static V merge(final Map map, final K key, final V value, final BiFunction remappingFunction) { N.requireNonNull(remappingFunction); N.requireNonNull(value); V oldValue = map.get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if (newValue == null) { map.remove(key); } else { map.put(key, newValue); } return newValue; } /** * Performs the given action for each entry in this map until all entries * have been processed or the action throws an exception. Unless * otherwise specified by the implementing class, actions are performed in * the order of entry set iteration (if an iteration order is specified.) * Exceptions thrown by the action are relayed to the caller. * * @implSpec * The default implementation is equivalent to, for this {@code map}: *

 {@code
     * for (Map.Entry entry : map.entrySet())
     *     action.accept(entry.getKey(), entry.getValue());
     * }
* * The default implementation makes no guarantees about synchronization * or atomicity properties of this method. Any implementation providing * atomicity guarantees must override this method and document its * concurrency properties. * * @param action The action to be performed for each entry * @throws NullPointerException if the specified action is null * @throws ConcurrentModificationException if an entry is found to be * removed during iteration * @since 1.8 */ public static void forEach(final Map map, final BiConsumer action) { N.requireNonNull(action); 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); } action.accept(k, v); } } public static Map filterByKey(final Map map, final Predicate predicate) { if (N.isNullOrEmpty(map)) { return new LinkedHashMap<>(); } final Map result = map instanceof IdentityHashMap ? new IdentityHashMap() : new LinkedHashMap(); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getKey())) { result.put(entry.getKey(), entry.getValue()); } } return result; } public static Map filterByValue(final Map map, final Predicate predicate) { if (N.isNullOrEmpty(map)) { return new LinkedHashMap<>(); } final Map result = map instanceof IdentityHashMap ? new IdentityHashMap() : new LinkedHashMap(); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getValue())) { result.put(entry.getKey(), entry.getValue()); } } return result; } public static Map filter(final Map map, final BiPredicate predicate) { if (N.isNullOrEmpty(map)) { return new LinkedHashMap<>(); } final Map result = map instanceof IdentityHashMap ? new IdentityHashMap() : new LinkedHashMap(); for (Map.Entry entry : map.entrySet()) { if (predicate.test(entry.getKey(), entry.getValue())) { result.put(entry.getKey(), entry.getValue()); } } return result; } public static T map2Entity(final Class targetClass, final Map m) { return map2Entity(targetClass, m, N.isDirtyMarker(targetClass) == false, true); } @SuppressWarnings("unchecked") public static T map2Entity(final Class targetClass, final Map m, final boolean ignoreNullProperty, final boolean ignoreUnknownProperty) { checkEntityClass(targetClass); final T entity = N.newInstance(targetClass); String propName = null; Object propValue = null; Method propSetMethod = null; Class paramClass = null; for (Map.Entry entry : m.entrySet()) { propName = entry.getKey(); propValue = entry.getValue(); if (ignoreNullProperty && (propValue == null)) { continue; } propSetMethod = RefUtil.getPropSetMethod(targetClass, propName); if (propSetMethod == null) { RefUtil.setPropValue(entity, propName, propValue, ignoreUnknownProperty); } else { paramClass = propSetMethod.getParameterTypes()[0]; if (propValue != null && N.typeOf(propValue.getClass()).isMap() && N.isEntity(paramClass)) { RefUtil.setPropValue(entity, propSetMethod, map2Entity(paramClass, (Map) propValue, ignoreNullProperty, ignoreUnknownProperty)); } else { RefUtil.setPropValue(entity, propSetMethod, propValue); } } } return entity; } public static List map2Entity(final Class targetClass, final Collection> mList) { return map2Entity(targetClass, mList, N.isDirtyMarker(targetClass) == false, true); } public static List map2Entity(final Class targetClass, final Collection> mList, final boolean igoreNullProperty, final boolean ignoreUnknownProperty) { checkEntityClass(targetClass); final List entityList = new ArrayList<>(mList.size()); for (Map m : mList) { entityList.add(map2Entity(targetClass, m, igoreNullProperty, ignoreUnknownProperty)); } return entityList; } private static void checkEntityClass(final Class cls) { if (!N.isEntity(cls)) { throw new IllegalArgumentException("No property getter/setter method is found in the specified class: " + RefUtil.getCanonicalClassName(cls)); } } public static Map entity2Map(final Object entity) { return entity2Map(entity, (entity instanceof DirtyMarker == false)); } public static Map entity2Map(final Object entity, final boolean ignoreNullProperty) { return entity2Map(entity, ignoreNullProperty, null); } public static Map entity2Map(final Object entity, final Collection ignoredPropNames) { return entity2Map(entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } public static Map entity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } @SuppressWarnings("deprecation") public static Map entity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = (entity instanceof DirtyMarker ? ((DirtyMarker) entity).signedPropNames().size() : N.initHashCapacity(RefUtil.getPropGetMethodList(entity.getClass()).size())); final Map resultMap = new LinkedHashMap<>(initCapacity); entity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * * @param resultMap * @param entity * @return the input resultMap */ public static > T entity2Map(final T resultMap, final Object entity) { return entity2Map(resultMap, entity, (entity instanceof DirtyMarker == false)); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @return the input resultMap */ public static > T entity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty) { return entity2Map(resultMap, entity, ignoreNullProperty, null); } /** * * @param resultMap * @param entity * @param ignoredPropNames * @return the input resultMap */ public static > T entity2Map(final T resultMap, final Object entity, final Collection ignoredPropNames) { return entity2Map(resultMap, entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return the input resultMap */ public static > T entity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return the input resultMap */ @SuppressWarnings("deprecation") public static > T entity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, NamingPolicy keyNamingPolicy) { keyNamingPolicy = keyNamingPolicy == null ? NamingPolicy.CAMEL_CASE : keyNamingPolicy; final boolean hasIgnoredPropNames = N.notNullOrEmpty(ignoredPropNames); Set signedPropNames = null; if (entity instanceof DirtyMarker) { final Class entityClass = entity.getClass(); signedPropNames = ((DirtyMarker) entity).signedPropNames(); if (signedPropNames.size() == 0) { // logger.warn("no property is signed in the specified source entity: " // + toString(entity)); return resultMap; } else { final Set tmp = new HashSet<>(N.initHashCapacity(signedPropNames.size())); for (String propName : signedPropNames) { tmp.add(RefUtil.getPropNameByMethod(RefUtil.getPropGetMethod(entityClass, propName))); } signedPropNames = tmp; } } final Map getterMethodList = RefUtil.checkPropGetMethodList(entity.getClass()); String propName = null; Object propValue = null; try { switch (keyNamingPolicy) { case CAMEL_CASE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } resultMap.put(propName, propValue); } break; } case LOWER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } resultMap.put(RefUtil.toLowerCaseWithUnderscore(propName), propValue); } break; } case UPPER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } resultMap.put(RefUtil.toUpperCaseWithUnderscore(propName), propValue); } break; } default: throw new IllegalArgumentException("Unsupported NamingPolicy: " + keyNamingPolicy); } } catch (Exception e) { throw new AbacusException(e); } return resultMap; } public static List> entity2Map(final Collection entityList) { return entity2Map(entityList, null); } public static List> entity2Map(final Collection entityList, final boolean ignoreNullProperty) { return entity2Map(entityList, ignoreNullProperty, null); } public static List> entity2Map(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 entity2Map(entityList, ignoreNullProperty, ignoredPropNames); } public static List> entity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2Map(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } 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; } public static Map deepEntity2Map(final Object entity) { return deepEntity2Map(entity, (entity instanceof DirtyMarker == false)); } public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty) { return deepEntity2Map(entity, ignoreNullProperty, null); } public static Map deepEntity2Map(final Object entity, final Collection ignoredPropNames) { return deepEntity2Map(entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } @SuppressWarnings("deprecation") public static Map deepEntity2Map(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = entity instanceof DirtyMarker ? ((DirtyMarker) entity).signedPropNames().size() : N.initHashCapacity(RefUtil.getPropGetMethodList(entity.getClass()).size()); final Map resultMap = new LinkedHashMap<>(initCapacity); deepEntity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * * @param resultMap * @param entity * @return the input resultMap */ public static > T deepEntity2Map(final T resultMap, final Object entity) { return deepEntity2Map(resultMap, entity, (entity instanceof DirtyMarker == false)); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @return the input resultMap */ public static > T deepEntity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty) { return deepEntity2Map(resultMap, entity, ignoreNullProperty, null); } /** * * @param resultMap * @param entity * @param ignoredPropNames * @return the input resultMap */ public static > T deepEntity2Map(final T resultMap, final Object entity, final Collection ignoredPropNames) { return deepEntity2Map(resultMap, entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return the input resultMap */ public static > T deepEntity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return the input resultMap */ @SuppressWarnings("deprecation") public static > T deepEntity2Map(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, NamingPolicy keyNamingPolicy) { keyNamingPolicy = keyNamingPolicy == null ? NamingPolicy.CAMEL_CASE : keyNamingPolicy; final boolean hasIgnoredPropNames = N.notNullOrEmpty(ignoredPropNames); Set signedPropNames = null; if (entity instanceof DirtyMarker) { final Class entityClass = entity.getClass(); signedPropNames = ((DirtyMarker) entity).signedPropNames(); if (signedPropNames.size() == 0) { // logger.warn("no property is signed in the specified source entity: " // + toString(entity)); return resultMap; } else { final Set tmp = new HashSet<>(N.initHashCapacity(signedPropNames.size())); for (String propName : signedPropNames) { tmp.add(RefUtil.getPropNameByMethod(RefUtil.getPropGetMethod(entityClass, propName))); } signedPropNames = tmp; } } final Map getterMethodList = RefUtil.checkPropGetMethodList(entity.getClass()); String propName = null; Object propValue = null; try { switch (keyNamingPolicy) { case CAMEL_CASE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { resultMap.put(propName, propValue); } else { resultMap.put(propName, deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } } break; } case LOWER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { resultMap.put(RefUtil.toLowerCaseWithUnderscore(propName), propValue); } else { resultMap.put(RefUtil.toLowerCaseWithUnderscore(propName), deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } } break; } case UPPER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (signedPropNames != null && signedPropNames.contains(propName) == false) { continue; } if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { resultMap.put(RefUtil.toUpperCaseWithUnderscore(propName), propValue); } else { resultMap.put(RefUtil.toUpperCaseWithUnderscore(propName), deepEntity2Map(propValue, ignoreNullProperty, null, keyNamingPolicy)); } } break; } default: throw new IllegalArgumentException("Unsupported NamingPolicy: " + keyNamingPolicy); } } catch (Exception e) { throw new AbacusException(e); } return resultMap; } public static List> deepEntity2Map(final Collection entityList) { return deepEntity2Map(entityList, null); } public static List> deepEntity2Map(final Collection entityList, final boolean ignoreNullProperty) { return deepEntity2Map(entityList, ignoreNullProperty, null); } 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); } public static List> deepEntity2Map(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return deepEntity2Map(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } 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; } public static Map entity2FlatMap(final Object entity) { return entity2FlatMap(entity, (entity instanceof DirtyMarker == false)); } public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty) { return entity2FlatMap(entity, ignoreNullProperty, null); } public static Map entity2FlatMap(final Object entity, final Collection ignoredPropNames) { return entity2FlatMap(entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } @SuppressWarnings("deprecation") public static Map entity2FlatMap(final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { final int initCapacity = entity instanceof DirtyMarker ? ((DirtyMarker) entity).signedPropNames().size() : N.initHashCapacity(RefUtil.getPropGetMethodList(entity.getClass()).size()); final Map resultMap = new LinkedHashMap<>(initCapacity); entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy); return resultMap; } /** * * @param resultMap * @param entity * @return the input resultMap */ public static > T entity2FlatMap(final T resultMap, final Object entity) { return entity2FlatMap(resultMap, entity, (entity instanceof DirtyMarker == false)); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @return the input resultMap */ public static > T entity2FlatMap(final T resultMap, final Object entity, final boolean ignoreNullProperty) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, null); } /** * * @param resultMap * @param entity * @param ignoredPropNames * @return the input resultMap */ public static > T entity2FlatMap(final T resultMap, final Object entity, final Collection ignoredPropNames) { return entity2FlatMap(resultMap, entity, (entity instanceof DirtyMarker == false), ignoredPropNames); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @return the input resultMap */ public static > T entity2FlatMap(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @return the input resultMap */ public static > T entity2FlatMap(final T resultMap, final Object entity, final boolean ignoreNullProperty, final Collection ignoredPropNames, final NamingPolicy keyNamingPolicy) { return entity2FlatMap(resultMap, entity, ignoreNullProperty, ignoredPropNames, keyNamingPolicy, null); } /** * * @param resultMap * @param entity * @param ignoreNullProperty * @param ignoredPropNames * @param keyNamingPolicy * @param parentPropName * @return the input resultMap */ @SuppressWarnings("deprecation") 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); if (entity instanceof DirtyMarker) { final Class entityClass = entity.getClass(); final Set signedPropNames = ((DirtyMarker) entity).signedPropNames(); if (signedPropNames.size() == 0) { // logger.warn("no property is signed in the specified source entity: " // + toString(entity)); } else { Method propGetMethod = null; Object propValue = null; try { switch (keyNamingPolicy) { case CAMEL_CASE: { for (String propName : signedPropNames) { propGetMethod = RefUtil.getPropGetMethod(entityClass, propName); propName = RefUtil.getPropNameByMethod(propGetMethod); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = propGetMethod.invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } case LOWER_CASE_WITH_UNDERSCORE: { for (String propName : signedPropNames) { propGetMethod = RefUtil.getPropGetMethod(entityClass, propName); propName = RefUtil.getPropNameByMethod(propGetMethod); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propName = RefUtil.toLowerCaseWithUnderscore(propName); propValue = propGetMethod.invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } case UPPER_CASE_WITH_UNDERSCORE: { for (String propName : signedPropNames) { propGetMethod = RefUtil.getPropGetMethod(entityClass, propName); propName = RefUtil.getPropNameByMethod(propGetMethod); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propName = RefUtil.toUpperCaseWithUnderscore(propName); propValue = propGetMethod.invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } default: throw new IllegalArgumentException("Unsupported NamingPolicy: " + keyNamingPolicy); } } catch (Exception e) { throw new AbacusException(e); } } } else { final Map getterMethodList = RefUtil.checkPropGetMethodList(entity.getClass()); String propName = null; Object propValue = null; try { switch (keyNamingPolicy) { case CAMEL_CASE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } case LOWER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propName = RefUtil.toLowerCaseWithUnderscore(propName); propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } case UPPER_CASE_WITH_UNDERSCORE: { for (Map.Entry entry : getterMethodList.entrySet()) { propName = entry.getKey(); if (hasIgnoredPropNames && ignoredPropNames.contains(propName)) { continue; } propName = RefUtil.toUpperCaseWithUnderscore(propName); propValue = entry.getValue().invoke(entity); if (ignoreNullProperty && (propValue == null)) { continue; } if ((propValue == null) || !N.isEntity(propValue.getClass())) { if (isNullParentPropName) { resultMap.put(propName, propValue); } else { resultMap.put(parentPropName + D.PERIOD + propName, propValue); } } else { if (isNullParentPropName) { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, propName); } else { entity2FlatMap(resultMap, propValue, ignoreNullProperty, null, keyNamingPolicy, parentPropName + D.PERIOD + propName); } } } break; } default: throw new IllegalArgumentException("Unsupported NamingPolicy: " + keyNamingPolicy); } } catch (Exception e) { throw new AbacusException(e); } } return resultMap; } public static List> entity2FlatMap(final Collection entityList) { return entity2FlatMap(entityList, null); } public static List> entity2FlatMap(final Collection entityList, final boolean ignoreNullProperty) { return entity2FlatMap(entityList, ignoreNullProperty, null); } public static List> entity2FlatMap(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 entity2FlatMap(entityList, ignoreNullProperty, ignoredPropNames); } public static List> entity2FlatMap(final Collection entityList, final boolean ignoreNullProperty, final Collection ignoredPropNames) { return entity2FlatMap(entityList, ignoreNullProperty, ignoredPropNames, NamingPolicy.CAMEL_CASE); } 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; } public static String join(final Map m) { return join(m, N.ELEMENT_SEPARATOR); } public static String join(final Map m, final char entryDelimiter) { if (N.isNullOrEmpty(m)) { return N.EMPTY_STRING; } return join(m, 0, m.size(), entryDelimiter); } public static String join(final Map m, final String entryDelimiter) { if (N.isNullOrEmpty(m)) { return N.EMPTY_STRING; } return join(m, 0, m.size(), entryDelimiter); } public static String join(final Map m, final int fromIndex, final int toIndex, final char entryDelimiter) { return join(m, fromIndex, toIndex, entryDelimiter, false); } public static String join(final Map m, final int fromIndex, final int toIndex, final char entryDelimiter, final boolean trim) { return join(m, fromIndex, toIndex, entryDelimiter, D._EQUAL, trim); } public static String join(final Map m, final int fromIndex, final int toIndex, final String entryDelimiter) { return join(m, fromIndex, toIndex, entryDelimiter, false); } public static String join(final Map m, final int fromIndex, final int toIndex, final String entryDelimiter, final boolean trim) { return join(m, fromIndex, toIndex, entryDelimiter, D.EQUAL, trim); } public static String join(final Map m, final char entryDelimiter, final char keyValueDelimiter) { if (N.isNullOrEmpty(m)) { return N.EMPTY_STRING; } return join(m, 0, m.size(), entryDelimiter, keyValueDelimiter); } public static String join(final Map m, final String entryDelimiter, final String keyValueDelimiter) { if (N.isNullOrEmpty(m)) { return N.EMPTY_STRING; } return join(m, 0, m.size(), entryDelimiter, keyValueDelimiter); } public static String join(final Map m, final int fromIndex, final int toIndex, final char entryDelimiter, final char keyValueDelimiter) { return join(m, fromIndex, toIndex, entryDelimiter, keyValueDelimiter, false); } public static String join(final Map m, final int fromIndex, final int toIndex, final char entryDelimiter, final char keyValueDelimiter, final boolean trim) { N.checkFromToIndex(fromIndex, toIndex, m == null ? 0 : m.size()); if ((N.isNullOrEmpty(m) && fromIndex == 0 && toIndex == 0) || (fromIndex == toIndex && fromIndex < m.size())) { return N.EMPTY_STRING; } final StringBuilder sb = ObjectFactory.createStringBuilder(); try { int i = 0; for (Map.Entry entry : m.entrySet()) { if (i++ > fromIndex) { sb.append(entryDelimiter); } if (i > fromIndex) { sb.append(trim ? N.toString(entry.getKey()).trim() : N.toString(entry.getKey())); sb.append(keyValueDelimiter); sb.append(trim ? N.toString(entry.getValue()).trim() : N.toString(entry.getValue())); } if (i >= toIndex) { break; } } return sb.toString(); } finally { ObjectFactory.recycle(sb); } } public static String join(final Map m, final int fromIndex, final int toIndex, final String entryDelimiter, final String keyValueDelimiter) { return join(m, fromIndex, toIndex, entryDelimiter, keyValueDelimiter, false); } public static String join(final Map m, final int fromIndex, final int toIndex, final String entryDelimiter, final String keyValueDelimiter, final boolean trim) { N.checkFromToIndex(fromIndex, toIndex, m == null ? 0 : m.size()); if ((N.isNullOrEmpty(m) && fromIndex == 0 && toIndex == 0) || (fromIndex == toIndex && fromIndex < m.size())) { return N.EMPTY_STRING; } final StringBuilder sb = ObjectFactory.createStringBuilder(); try { int i = 0; for (Map.Entry entry : m.entrySet()) { if (i++ > fromIndex) { sb.append(entryDelimiter); } if (i > fromIndex) { sb.append(trim ? N.toString(entry.getKey()).trim() : N.toString(entry.getKey())); sb.append(keyValueDelimiter); sb.append(trim ? N.toString(entry.getValue()).trim() : N.toString(entry.getValue())); } if (i >= toIndex) { break; } } return sb.toString(); } finally { ObjectFactory.recycle(sb); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy