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

es.webbeta.serializer.metadata.MetadataConstructor Maven / Gradle / Ivy

The newest version!
package es.webbeta.serializer.metadata;

import es.webbeta.serializer.type.FieldAccessType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class MetadataConstructor {

    private static final String KEY_VIRTUAL_PROPERTIES = "virtual_properties";
    private static final String KEY_PROPERTIES = "properties";
    private static final String KEY_ACCESS_TYPE = "access_type";
    private static final String KEY_ACCESSOR = "accessor";
    private static final String KEY_SERIALIZED_NAME = "serialized_name";

    private static final String KEY_GROUPS = "groups";
    private static final String KEY_GETTER = "getter";

    public static Metadata build(Map map) {
        if (map == null) return null;
        Map.Entry> root =
                (Map.Entry>) map.entrySet().iterator().next();

        String canonicalName = root.getKey();
        Map modifiers = root.getValue();

        Metadata metadata = new Metadata(canonicalName);

        for (Map.Entry modifierEntry : modifiers.entrySet()) {
            String key = modifierEntry.getKey();

            switch (key) {
                case KEY_PROPERTIES: {
                    List properties =
                            buildProperties((Map>) modifierEntry.getValue(), false);
                    metadata.setProperties(properties);
                    break;
                }
                case KEY_ACCESS_TYPE:
                    metadata.setAccessType(FieldAccessType.fromString((String) modifierEntry.getValue()));
                    break;
                case KEY_VIRTUAL_PROPERTIES: {
                    List properties =
                            buildProperties((Map>) modifierEntry.getValue(), true);
                    metadata.setVirtualProperties(properties);
                    break;
                }
            }
        }

        return metadata;
    }

    private static  List buildProperties(Map> map, Boolean asVirtualProperties) {
        List properties = new ArrayList<>();

        for (Map.Entry> propertyEntry : map.entrySet()) {
            String propertyName = propertyEntry.getKey();
            Map args = propertyEntry.getValue();

            IMetadataProperty metadataProperty = asVirtualProperties ?
                    new MetadataVirtualProperty(propertyName) :
                    new MetadataProperty(propertyName);

            for (Map.Entry argEntry : args.entrySet()) {
                String key = argEntry.getKey();

                switch (key) {
                    case KEY_GROUPS:
                        List groups = new ArrayList<>();
                        for (Object rawGroup : (List) argEntry.getValue()) {
                            String group = (String) rawGroup;
                            groups.add(group);
                        }
                        metadataProperty.setGroups(groups);
                        break;
                    case KEY_SERIALIZED_NAME:
                        metadataProperty.setSerializedName((String) argEntry.getValue());
                        break;
                }

                if (!asVirtualProperties) {
                    switch (key) {
                        case KEY_ACCESS_TYPE:
                            metadataProperty.setAccessType(FieldAccessType.fromString((String) argEntry.getValue()));
                            break;
                        case KEY_ACCESSOR:
                            MetadataPropertyAccessor accessor =
                                    buildPropertyAccessor(metadataProperty, (Map) argEntry.getValue());
                            metadataProperty.setAccessor(accessor);
                            break;
                    }
                }
            }

            properties.add(metadataProperty);
        }

        return (List) properties;
    }

    private static MetadataPropertyAccessor buildPropertyAccessor(IMetadataProperty metadataProperty, Map map) {
        if (metadataProperty.getAccessType() != FieldAccessType.PUBLIC_METHOD) return null;

        String getter = null;
        for (Map.Entry argEntry : map.entrySet()) {
            String key = argEntry.getKey();

            if (key.equals(KEY_GETTER))
                getter = argEntry.getValue();
        }
        return (getter == null) ?
                null :
                new MetadataPropertyAccessor(getter);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy