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

org.opensextant.annotations.DeepEyeData Maven / Gradle / Ivy

There is a newer version: 3.7.3
Show newest version
/*
 * IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII                  
 * 
 * OpenSextant/Xponents sub-project
 *      __                              
 *  ___/ /___  ___  ___  ___  __ __ ___ 
 * / _  // -_)/ -_)/ _ \/ -_)/ // // -_)
 * \_,_/ \__/ \__// .__/\__/ \_, / \__/ 
 *               /_/        /___/
 *               
 * IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII    
 * Copyright 2013, 2019 MITRE Corporation             
 */

package org.opensextant.annotations;

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

import jodd.json.JsonArray;
import jodd.json.JsonObject;

/**
 * A base class for Record, Annotation and other structures. Common fields include: id, value, and
 * attributes, which may be empty.
 * 
 * @author ubaldino
 */
public abstract class DeepEyeData {

    /** A base class to keep all data organized */
    public String id = null;
    public JsonObject attrs = null;
    public String value = null;

    public void addAttribute(String k, Object v) {
        if (attrs == null) {
            this.newAttributes();
        }
        attrs.put(k, v);
    }

    public void newAttributes() {
        if (this.attrs == null) {
            this.attrs = new JsonObject();
        }
    }

    /** Converts internal JSON store to a key/value map.
     */
    public Map getAttributes() {

        if (attrs == null) {
            return null;
        }
        if (attrs.isEmpty()) {
            return null;
        }

        return map(attrs);
    }

    public boolean isValue(Map v) {
        if (v == null) {
            return false;
        }
        return !v.isEmpty();
    }

    public boolean isValue(Collection v) {
        if (v == null) {
            return false;
        }
        return !v.isEmpty();
    }

    /**
     * utility -- get list from jsonarray.
     * 
     * @param arr JSON array
     * @return
     */
    public static List list(JsonArray arr) {
        if (arr != null) {
            return arr.list();
        }
        return null;
    }

    public static List list(JsonObject arr) {
        if (arr != null) {
            ArrayList keys = new ArrayList<>();
            keys.addAll(arr.fieldNames());
            return keys;
        }
        return null;
    }

    /**
     * Convert an array to a trivial map, [i1, i2, i3,...] ==> { i1:"1", i2:"1", ...} UNUSED.
     * 
     * @param obj JSON array
     * @return map representation of array
     */
    public static Map asMap(JsonArray obj) {
        if (obj != null) {
            Map map = new HashMap<>();
            for (Object o : obj) {
                map.put(o.toString(), "1");
            }
            return map;
        }
        return null;
    }

    public static Map map(JsonObject obj) {
        if (obj != null) {
            return obj.map();
        }
        return null;
    }

    public Collection getAttributeNames() {
        if (attrs == null) {
            return null;
        }
        return attrs.fieldNames();
    }

    public abstract Map getMap();
}