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

com.github.datalking.beans.PropertyAccessorUtils Maven / Gradle / Ivy

package com.github.datalking.beans;

/**
 * @author yaoo on 5/10/18
 */
public abstract class PropertyAccessorUtils {

    public static String getPropertyName(String propertyPath) {
        int separatorIndex = (propertyPath.endsWith(PropertyAccessor.PROPERTY_KEY_SUFFIX) ?
                propertyPath.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) : -1);
        return (separatorIndex != -1 ? propertyPath.substring(0, separatorIndex) : propertyPath);
    }

    public static boolean isNestedOrIndexedProperty(String propertyPath) {
        if (propertyPath == null) {
            return false;
        }
        for (int i = 0; i < propertyPath.length(); i++) {
            char ch = propertyPath.charAt(i);
            if (ch == PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR ||
                    ch == PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) {
                return true;
            }
        }
        return false;
    }

    public static int getFirstNestedPropertySeparatorIndex(String propertyPath) {
        return getNestedPropertySeparatorIndex(propertyPath, false);
    }

    public static int getLastNestedPropertySeparatorIndex(String propertyPath) {
        return getNestedPropertySeparatorIndex(propertyPath, true);
    }

    private static int getNestedPropertySeparatorIndex(String propertyPath, boolean last) {
        boolean inKey = false;
        int length = propertyPath.length();
        int i = (last ? length - 1 : 0);
        while (last ? i >= 0 : i < length) {
            switch (propertyPath.charAt(i)) {
                case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR:
                case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR:
                    inKey = !inKey;
                    break;
                case PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR:
                    if (!inKey) {
                        return i;
                    }
            }
            if (last) {
                i--;
            } else {
                i++;
            }
        }
        return -1;
    }

    public static boolean matchesProperty(String registeredPath, String propertyPath) {
        if (!registeredPath.startsWith(propertyPath)) {
            return false;
        }
        if (registeredPath.length() == propertyPath.length()) {
            return true;
        }
        if (registeredPath.charAt(propertyPath.length()) != PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) {
            return false;
        }
        return (registeredPath.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR, propertyPath.length() + 1) ==
                registeredPath.length() - 1);
    }

    /**
     * 属性名简化
     * map['key']/map["key"]  ->  map[key]
     */
    public static String canonicalPropertyName(String propertyName) {
        if (propertyName == null) {
            return "";
        }

        StringBuilder sb = new StringBuilder(propertyName);
        int searchIndex = 0;
        while (searchIndex != -1) {
            int keyStart = sb.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX, searchIndex);
            searchIndex = -1;
            if (keyStart != -1) {
                int keyEnd = sb.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX, keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length());
                if (keyEnd != -1) {
                    String key = sb.substring(keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length(), keyEnd);
                    if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) {
                        sb.delete(keyStart + 1, keyStart + 2);
                        sb.delete(keyEnd - 2, keyEnd - 1);
                        keyEnd = keyEnd - 2;
                    }
                    searchIndex = keyEnd + PropertyAccessor.PROPERTY_KEY_SUFFIX.length();
                }
            }
        }
        return sb.toString();
    }

    public static String[] canonicalPropertyNames(String[] propertyNames) {
        if (propertyNames == null) {
            return null;
        }
        String[] result = new String[propertyNames.length];
        for (int i = 0; i < propertyNames.length; i++) {
            result[i] = canonicalPropertyName(propertyNames[i]);
        }
        return result;
    }

}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy