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

net.intelie.pipes.util.LiteralRepresentation Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes.util;

import net.intelie.pipes.Row;
import net.intelie.pipes.types.*;

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

public abstract class LiteralRepresentation {
    public static String toString(Type type, Object value) {
        value = type.cast(value);
        if (Type.NULL.equals(type) && value == null)
            return "null";

        Type inferred = Type.infer(value);
        if (inferred.isAssignableTo(type)) type = inferred;

        if (value == null)
            return type.makeString(null);

        if (Type.STRING.equals(type))
            return "" + Escapes.formatString((String) value);
        if ((Type.NUMBER.equals(type) || Type.BOOLEAN.equals(type) || Type.PERIOD.equals(type)))
            return "" + Type.STRING.cast(value);

        String answer;

        answer = tryRow(type, value);
        if (answer != null) return answer;

        answer = trySequence(type, value);
        if (answer != null) return answer;

        answer = tryMap(type, value);
        if (answer != null) return answer;

        return type.makeString(value);
    }


    private static String trySequence(Type type, Object value) {
        SeqType extracted = Type.extract(type, SeqType.class);
        if (extracted == null || !(value instanceof Iterable))
            return null;

        int count = 0;
        List list = new ArrayList();
        for (Object obj : (Iterable) value) {
            if (count++ >= 8)
                return "(" + Iterables.join(", ", list) + ", ...)";
            list.add(toString(extracted.type(), obj));
        }
        return "(" + Iterables.join(", ", list) + maybeComma(list) + ")";
    }

    private static String tryMap(Type type, Object value) {
        MapType extracted = Type.extract(type, MapType.class);
        if (extracted == null || !(value instanceof Map))
            return null;

        int count = 0;
        List list = new ArrayList();
        for (Map.Entry obj : ((Map) value).entrySet()) {
            if (count++ >= 8) {
                list.add("...");
                break;
            }
            list.add(toString(extracted.keyType(), obj.getKey()));
            list.add(toString(extracted.valueType(), obj.getValue()));
        }
        return "newmap(" + Iterables.join(", ", list) + maybeComma(list) + ")";
    }

    private static String maybeComma(List list) {
        return list.size() == 1 ? "," : "";
    }

    private static String tryRow(Type type, Object value) {
        RowType extracted = Type.extract(type, RowType.class);

        if (extracted == null || extracted.fields() == null || !(value instanceof Row))
            return null;

        Row row = (Row) value;
        RowFields fields = extracted.fields();
        List list = new ArrayList();
        for (int i = 0; i < fields.size(); i++) {
            String name = fields.name(i);
            Object obj = i < row.size() ? row.get(i) : null;
            String expr = toString(fields.type(i), obj);
            String expected = Escapes.safeIdentifier(expr);
            list.add(expr + (!expected.equals(name) ? " as " + name : ""));
        }
        return "(" + Iterables.join(", ", list) + maybeComma(list) + ")";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy