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

com.blazebit.reflection.ExpressionUtils Maven / Gradle / Ivy

package com.blazebit.reflection;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public final class ExpressionUtils {

    private static final ConcurrentMap> cache = new ConcurrentHashMap>();

	/* Without value class */

    public static  PropertyPathExpression getExpression(
            Class source, String propertyPath) {
        return getExpression(source, propertyPath, (Class) null);
    }

    public static  PropertyPathExpressionValueHolder getValueHolder(
            Class source, X target, String propertyPath) {
        return getValueHolder(source, target, propertyPath, (Class) null);
    }

    public static  PropertyPathExpressionValueHolder getValueHolder(
        Class source, X target, String propertyPath, boolean allowFieldAccess) {
        return getValueHolder(source, target, propertyPath, (Class) null, allowFieldAccess);
    }

	/* With value class */

    @SuppressWarnings("unchecked")
    public static  PropertyPathExpression getExpression(
            Class source, String propertyPath, Class valueClass) {
        return getExpression(source, propertyPath, valueClass, false);
    }

    @SuppressWarnings("unchecked")
    public static  PropertyPathExpression getExpression(
        Class source, String propertyPath, Class valueClass, boolean allowFieldAccess) {
        final PropertyPathExpressionKey key = new PropertyPathExpressionKey(
            source, propertyPath, allowFieldAccess);
        PropertyPathExpression expression = cache.get(key);

        if (expression == null) {
            expression = new PropertyPathExpression(
                    (Class) source, propertyPath, allowFieldAccess);
            final PropertyPathExpression oldExpression = cache
                    .putIfAbsent(key, expression);

            if (oldExpression != null) {
                expression = oldExpression;
            }
        }

        return (PropertyPathExpression) expression;
    }

    public static  PropertyPathExpressionValueHolder getValueHolder(
            Class source, X target, String propertyPath, Class valueClass) {
        return new PropertyPathExpressionValueHolder(target, getExpression(source, propertyPath, valueClass));
    }

    public static  PropertyPathExpressionValueHolder getValueHolder(
        Class source, X target, String propertyPath, Class valueClass, boolean allowFieldAccess) {
        return new PropertyPathExpressionValueHolder(target, getExpression(source, propertyPath, valueClass, allowFieldAccess));
    }

	/* With source class */

    public static  Y getValue(Class source, X target,
                                    String propertyPath) {
        return getValue(source, target, propertyPath, (Class) null);
    }

    public static  Y getNullSafeValue(Class source, X target,
                                            String propertyPath) {
        return getNullSafeValue(source, target, propertyPath, (Class) null);
    }

    public static  void setValue(Class source, X target,
                                       String propertyPath, Y value) {
        getExpression(source, propertyPath).setValue(target, value);
    }

    public static  Y getValue(Class source, X target,
                                    String propertyPath, boolean allowFieldAccess) {
        return getValue(source, target, propertyPath, (Class) null, allowFieldAccess);
    }

    public static  Y getNullSafeValue(Class source, X target,
                                            String propertyPath, boolean allowFieldAccess) {
        return getNullSafeValue(source, target, propertyPath, (Class) null, allowFieldAccess);
    }

    public static  void setValue(Class source, X target,
                                       String propertyPath, Y value, boolean allowFieldAccess) {
        getExpression(source, propertyPath, null, allowFieldAccess).setValue(target, value);
    }

	/* With value class */

    public static  Y getValue(Class source, X target,
                                    String propertyPath, Class valueClass) {
        return getExpression(source, propertyPath, valueClass).getValue(target);
    }

    public static  Y getNullSafeValue(Class source, X target,
                                            String propertyPath, Class valueClass) {
        return getExpression(source, propertyPath, valueClass)
                .getNullSafeValue(target);
    }

    public static  Y getValue(Class source, X target,
                                    String propertyPath, Class valueClass, boolean allowFieldAccess) {
        return getExpression(source, propertyPath, valueClass, allowFieldAccess).getValue(target);
    }

    public static  Y getNullSafeValue(Class source, X target,
                                            String propertyPath, Class valueClass, boolean allowFieldAccess) {
        return getExpression(source, propertyPath, valueClass, allowFieldAccess)
            .getNullSafeValue(target);
    }

	/* Without source class */

    public static  Y getValue(X target, String propertyPath) {
        return getValue(target, propertyPath, null);
    }

    public static  Y getNullSafeValue(X target, String propertyPath) {
        return getNullSafeValue(target, propertyPath, null);
    }

    @SuppressWarnings("unchecked")
    public static  void setValue(X target, String propertyPath, Y value) {
        getExpression((Class) target.getClass(), propertyPath).setValue(
                target, value);
    }

    public static  Y getValue(X target, String propertyPath, boolean allowFieldAccess) {
        return getValue(target, propertyPath, null, allowFieldAccess);
    }

    public static  Y getNullSafeValue(X target, String propertyPath, boolean allowFieldAccess) {
        return getNullSafeValue(target, propertyPath, null, allowFieldAccess);
    }

    @SuppressWarnings("unchecked")
    public static  void setValue(X target, String propertyPath, Y value, boolean allowFieldAccess) {
        getExpression((Class) target.getClass(), propertyPath, null, allowFieldAccess).setValue(
            target, value);
    }

	/* With value class */

    @SuppressWarnings("unchecked")
    public static  Y getValue(X target, String propertyPath,
                                    Class valueClass) {
        return getExpression((Class) target.getClass(), propertyPath,
                valueClass).getValue(target);
    }

    @SuppressWarnings("unchecked")
    public static  Y getNullSafeValue(X target, String propertyPath,
                                            Class valueClass) {
        return target == null ? null : getExpression(
                (Class) target.getClass(), propertyPath, valueClass)
                .getNullSafeValue(target);
    }

    @SuppressWarnings("unchecked")
    public static  Y getValue(X target, String propertyPath,
                                    Class valueClass, boolean allowFieldAccess) {
        return getExpression((Class) target.getClass(), propertyPath,
                             valueClass, allowFieldAccess).getValue(target);
    }

    @SuppressWarnings("unchecked")
    public static  Y getNullSafeValue(X target, String propertyPath,
                                            Class valueClass, boolean allowFieldAccess) {
        return target == null ? null : getExpression(
            (Class) target.getClass(), propertyPath, valueClass, allowFieldAccess)
            .getNullSafeValue(target);
    }

    private static class PropertyPathExpressionKey {
        final Class source;
        final String propertyPath;
        final boolean allowField;

        public PropertyPathExpressionKey(Class source, String propertyPath, boolean allowField) {
            this.source = source;
            this.propertyPath = propertyPath;
            this.allowField = allowField;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((propertyPath == null) ? 0 : propertyPath.hashCode());
            result = prime * result
                    + ((source == null) ? 0 : source.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            PropertyPathExpressionKey other = (PropertyPathExpressionKey) obj;
            if (propertyPath == null) {
                if (other.propertyPath != null)
                    return false;
            } else if (!propertyPath.equals(other.propertyPath))
                return false;
            if (source == null) {
                if (other.source != null)
                    return false;
            } else if (!source.equals(other.source))
                return false;
            return true;
        }
    }

    private ExpressionUtils() {
    }
}