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

com.fasterxml.jackson.databind.util.IgnorePropertiesUtil Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
package com.fasterxml.jackson.databind.util;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @since 2.12
 */
public class IgnorePropertiesUtil
{
    /**
     * Decide if we need to ignore a property or not, given a set of field to ignore and a set of field to include.
     */
    public static boolean shouldIgnore(Object value, Collection toIgnore, Collection toInclude) {
        if (toIgnore == null && toInclude ==null) {
            return false;
        }

        if (toInclude == null) {
            return toIgnore.contains(value);
        }

        if (toIgnore == null) {
            return !toInclude.contains(value);
        }

        // NOTE: conflict between both, JsonIncludeProperties will take priority.
        return !toInclude.contains(value) || toIgnore.contains(value);
    }

    /**
     * Factory method for creating and return a {@link Checker} instance if (and only if)
     * one needed.
     *
     * @param toIgnore Set of property names to ignore (may be null)
     * @param toInclude Set of only property names to include (if null, undefined)
     *
     * @return Checker, if validity checks are needed; {@code null} otherwise
     */
    public static Checker buildCheckerIfNeeded(Set toIgnore, Set toInclude) {
        // First: no-op case
        if ((toInclude == null) && ((toIgnore == null) || toIgnore.isEmpty())) {
            return null;
        }
        return Checker.construct(toIgnore, toInclude);
    }

    /**
     * Helper that encapsulates logic for combining two sets of "included names":
     * default logic is to do intersection (name must be in both to be included
     * in result)
     *
     * @param prevToInclude Existing set of names to include, if defined; null means "not defined"
     * @param newToInclude New set of names to included, if defined; null means "not defined"
     *
     * @return Resulting set of names, using intersection if neither {@code null}; or the
     *    non-null one (if only one is {@code null}); or {@code null} if both arguments {@code null}.
     */
    public static Set combineNamesToInclude(Set prevToInclude,
            Set newToInclude) {
        if (prevToInclude == null) {
            return newToInclude;
        }
        if (newToInclude == null) {
            return prevToInclude;
        }
        final Set result = new HashSet<>();

        // Make the intersection with the previously included properties
        for (String prop : newToInclude) {
            if (prevToInclude.contains(prop)) {
                result.add(prop);
            }
        }
        return result;
    }

    /**
     * Helper class to encapsulate logic from static {@code shouldIgnore} method
     * of util class.
     */
    public final static class Checker
        implements java.io.Serializable
    {
        private static final long serialVersionUID = 1L;

        private final Set _toIgnore;
        private final Set _toInclude;

        private Checker(Set toIgnore, Set toInclude) {
            if (toIgnore == null) {
                toIgnore = Collections.emptySet();
            }
            _toIgnore = toIgnore;
            _toInclude = toInclude;
        }

        public static Checker construct(Set toIgnore, Set toInclude) {
            return new Checker(toIgnore, toInclude);
        }

        // May seem odd but during serialization key is not cast up to String:
        public boolean shouldIgnore(Object propertyName) {
            return ((_toInclude != null) && !_toInclude.contains(propertyName))
                    || _toIgnore.contains(propertyName);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy