Search in sources :

Example 1 with Supplier

use of com.ibm.streamsx.topology.function.Supplier in project streamsx.topology by IBMStreams.

the class BOperatorInvocation method setParameter.

/*
    public BOperatorInvocation(GraphBuilder bt, String kind,
            Map<String, ? extends Object> params) {
        this(bt, Operator.class, params);
        json().put("kind", kind);
    }
    */
public void setParameter(String name, Object value) {
    Object jsonValue = value;
    String jsonType = null;
    if (value instanceof JSONAble) {
        value = ((JSONAble) value).toJSON();
    }
    if (value instanceof JSONObject) {
        JSONObject jo = ((JSONObject) value);
        if (jo.get("type") == null || jo.get("value") == null)
            throw new IllegalArgumentException("Illegal JSONObject " + jo);
        String type = (String) jo.get("type");
        Object val = (JSONObject) jo.get("value");
        if ("__spl_value".equals(type)) {
            /*
                 * The Value object is
                 * <pre><code>
                 * object {
                 *   type : "__spl_value"
                 *   value : object {
                 *     value : any. non-null. type appropriate for metaType
                 *     metaType : com.ibm.streams.operator.Type.MetaType.name() string
                 *   }
                 * }
                 * </code></pre>
                 */
            // unwrap and fall through to handling for the wrapped value
            JSONObject splValue = (JSONObject) val;
            value = splValue.get("value");
            jsonValue = value;
            String metaType = (String) splValue.get("metaType");
            if ("USTRING".equals(metaType) || "UINT8".equals(metaType) || "UINT16".equals(metaType) || "UINT32".equals(metaType) || "UINT64".equals(metaType)) {
                jsonType = metaType;
            }
        // fall through to handle jsonValue as usual 
        } else {
        // other kinds of JSONObject handled below
        }
    } else if (value instanceof Supplier<?>) {
        value = ((Supplier<?>) value).get();
        jsonValue = value;
    }
    if (value instanceof String) {
        op.setStringParameter(name, (String) value);
        if (jsonType == null)
            jsonType = MetaType.RSTRING.name();
    } else if (value instanceof Byte) {
        op.setByteParameter(name, (Byte) value);
        if (jsonType == null)
            jsonType = MetaType.INT8.name();
    } else if (value instanceof Short) {
        op.setShortParameter(name, (Short) value);
        if (jsonType == null)
            jsonType = MetaType.INT16.name();
    } else if (value instanceof Integer) {
        op.setIntParameter(name, (Integer) value);
        if (jsonType == null)
            jsonType = MetaType.INT32.name();
    } else if (value instanceof Long) {
        op.setLongParameter(name, (Long) value);
        if (jsonType == null)
            jsonType = MetaType.INT64.name();
    } else if (value instanceof Float) {
        op.setFloatParameter(name, (Float) value);
        jsonType = MetaType.FLOAT32.name();
    } else if (value instanceof Double) {
        op.setDoubleParameter(name, (Double) value);
        jsonType = MetaType.FLOAT64.name();
    } else if (value instanceof Boolean) {
        op.setBooleanParameter(name, (Boolean) value);
    } else if (value instanceof BigDecimal) {
        op.setBigDecimalParameter(name, (BigDecimal) value);
        // Need to maintain exact value
        jsonValue = value.toString();
    } else if (value instanceof Enum) {
        op.setCustomLiteralParameter(name, (Enum<?>) value);
        jsonValue = ((Enum<?>) value).name();
        jsonType = JParamTypes.TYPE_ENUM;
    } else if (value instanceof StreamSchema) {
        jsonValue = ((StreamSchema) value).getLanguageType();
        jsonType = JParamTypes.TYPE_SPLTYPE;
    } else if (value instanceof String[]) {
        String[] sa = (String[]) value;
        JSONArray a = new JSONArray(sa.length);
        for (String vs : sa) a.add(vs);
        jsonValue = a;
        op.setStringParameter(name, sa);
    } else if (value instanceof Attribute) {
        Attribute attr = (Attribute) value;
        jsonValue = attr.getName();
        jsonType = JParamTypes.TYPE_ATTRIBUTE;
        op.setAttributeParameter(name, attr.getName());
    } else if (value instanceof JSONObject) {
        JSONObject jo = (JSONObject) value;
        jsonType = (String) jo.get("type");
        jsonValue = (JSONObject) jo.get("value");
    } else {
        throw new IllegalArgumentException("Type for parameter " + name + " is not supported:" + value.getClass());
    }
    // Set the value as JSON
    JSONObject param = new JSONObject();
    param.put("value", jsonValue);
    if (jsonType != null)
        param.put("type", jsonType);
    jparams.put(name, param);
}
Also used : Attribute(com.ibm.streams.operator.Attribute) JSONArray(com.ibm.json.java.JSONArray) StreamSchema(com.ibm.streams.operator.StreamSchema) BigDecimal(java.math.BigDecimal) JSONObject(com.ibm.json.java.JSONObject) JSONAble(com.ibm.streamsx.topology.tuple.JSONAble) JSONObject(com.ibm.json.java.JSONObject) Supplier(com.ibm.streamsx.topology.function.Supplier)

Aggregations

JSONArray (com.ibm.json.java.JSONArray)1 JSONObject (com.ibm.json.java.JSONObject)1 Attribute (com.ibm.streams.operator.Attribute)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 Supplier (com.ibm.streamsx.topology.function.Supplier)1 JSONAble (com.ibm.streamsx.topology.tuple.JSONAble)1 BigDecimal (java.math.BigDecimal)1