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

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

Go to download

For a description please see https://github.com/activitystream/as-sdk-java

There is a newer version: 0.5.8
Show newest version
package com.activitystream.aspects;

import com.activitystream.Aspect;
import com.activitystream.underware.Factories;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

public class AspectBase implements Aspect {
    protected Map aspectPropertyMap = Factories.getMap();

    @Override
    public void addToObject(Map aspectJson, Set processed) {
        for (Map.Entry aspect : aspectPropertyMap.entrySet()) {
            if (aspect.getValue().required && aspect.getValue().value == null)
                throw new RuntimeException("Property " + aspect.getValue() + " is required ");
            if (aspect.getValue().value != null) {
                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 = Factories.getMap();
                        propertyParent.put(level, drill);
                    }
                    propertyParent = drill;
                }
                propertyParent.put(aspectPropertyKey, aspect.getValue().value);
            }
        }
    }

    public enum IsRequired {
        True, False
    }

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

        public AspectProperty(IsRequired required) {
            this.required = required == IsRequired.True;
        }
        public AspectProperty(IsRequired required, Object value) {
            this(required);
            this.value = value;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy