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

com.activitystream.aspects.AspectBase Maven / Gradle / Ivy

package com.activitystream.aspects;

import com.activitystream.Aspect;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AspectBase implements Aspect {
    protected Map aspectPropertyMap = new HashMap<>();

    @Override
    public void addToObject(Map aspectJson) {
        for (Map.Entry aspect : aspectPropertyMap.entrySet()) {
            if (aspect.getValue().required && aspect.getValue().value == null)
                throw new RuntimeException("Property " + aspect.getValue() + " is required ");
            String[] levels = aspect.getKey().split("\\.");
            String aspectPropertyKey = levels[levels.length - 1];
            levels = Arrays.copyOf(levels, levels.length - 1);
            Map propertyParent = aspectJson;
            for (String level : levels) {
                Map drill = (Map) propertyParent.get(level);
                if (drill == null) {
                    drill = new HashMap();
                    propertyParent.put(level, drill);
                }
                propertyParent = drill;
            }
            propertyParent.put(aspectPropertyKey, aspect.getValue().value);
        }
    }

    protected class AspectProperty {
        public Object value;
        public boolean required;

        public AspectProperty(boolean required) {
            this.required = required;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy