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

org.aksw.jena_sparql_api.gson.ExclusionStrategyClassAndFields Maven / Gradle / Ivy

There is a newer version: 3.17.0-1
Show newest version
package org.aksw.jena_sparql_api.gson;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class ExclusionStrategyClassAndFields implements ExclusionStrategy {

    private Multimap, String> classToFieldName = HashMultimap.create();
    private boolean whitelist;

    public ExclusionStrategyClassAndFields(Multimap, String> classToFieldName) {
        this(classToFieldName, false);
    }

    /**
     * 
     * @param classToFieldName
     *            The fields to be excluded
     * @param whitelist
     *            Skip the fields for which there is NO entry in the
     *            classToFieldName map
     */
    public ExclusionStrategyClassAndFields(
            Multimap, String> classToFieldName, boolean whitelist) {
        this.classToFieldName = classToFieldName;
        this.whitelist = whitelist;
    }

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        String fieldName = f.getName();
        Class fieldClass = f.getDeclaringClass();

        Map, Collection> map = classToFieldName.asMap();

        boolean result = false;

        for (Entry, Collection> entry : map.entrySet()) {

            Class entryClass = entry.getKey();
            Collection entryFields = entry.getValue();

            if (entryClass.isAssignableFrom(fieldClass)) {
                if (entryFields.contains(fieldName)) {
                    result = true;
                    break;
                }
            }
        }

        // Negate the result if in whitelist mode
        if (whitelist) {
            result = !result;
        }

        return result;
    }

    @Override
    public boolean shouldSkipClass(Class clazz) {
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy