Search in sources :

Example 1 with OtpErlangMap

use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.

the class TypeConverter method java2erlang.

@SuppressWarnings("boxing")
public static OtpErlangObject java2erlang(final Object obj, final Signature type) throws SignatureException {
    if (type.kind == 'x') {
        return TypeConverter.java2erlang(obj);
    }
    if (obj instanceof String) {
        return TypeConverter.cvtString(obj, type);
    }
    if (obj instanceof Character) {
        if (type.kind == 'i') {
            return new OtpErlangChar((Character) obj);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Number) {
        return TypeConverter.cvtNumber(obj, type);
    }
    if (obj instanceof Boolean) {
        if (type.kind == 'o') {
            return new OtpErlangAtom((Boolean) obj ? "true" : "false");
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Collection<?>) {
        if (type.kind == 'l') {
            final Object[] v = ((Collection<?>) obj).toArray(new Object[] {});
            final OtpErlangObject[] vv = new OtpErlangObject[v.length];
            for (int i = 0; i < v.length; i++) {
                vv[i] = TypeConverter.java2erlang(v[i], type.content[0]);
            }
            return new OtpErlangList(vv);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Map<?, ?>) {
        if (type.kind == 'm') {
            @SuppressWarnings("unchecked") final Map<OtpErlangObject, OtpErlangObject> map = (Map<OtpErlangObject, OtpErlangObject>) obj;
            final int size = map.keySet().size();
            final OtpErlangObject[] keys = map.keySet().toArray(new OtpErlangObject[size]);
            final OtpErlangObject[] values = new OtpErlangObject[size];
            for (int i = 0; i < size; i++) {
                values[i] = map.get(keys[i]);
            }
            return new OtpErlangMap(keys, values);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof OtpErlangPid) {
        return (OtpErlangPid) obj;
    }
    if (obj instanceof OtpErlangRef) {
        return (OtpErlangObject) obj;
    }
    if (obj instanceof OtpErlangBinary) {
        return (OtpErlangObject) obj;
    }
    if (obj instanceof OtpErlangObject) {
        TypeConverter.checkConversion(obj);
        return (OtpErlangObject) obj;
    }
    if (obj instanceof IConvertible) {
        return ((IConvertible) obj).toErlangObject();
    }
    if (obj != null && obj.getClass().isArray()) {
        final int len = Array.getLength(obj);
        // Class<?> component = obj.getClass().getComponentType();
        if (type.kind == 'b') {
            // TODO we can convert more things to binaries
            return new OtpErlangBinary(obj);
        }
        if (type.kind == 'l') {
            final OtpErlangObject[] vv = new OtpErlangObject[len];
            for (int i = 0; i < len; i++) {
                vv[i] = TypeConverter.java2erlang(Array.get(obj, i), type.content[0]);
            }
            return new OtpErlangList(vv);
        } else if (type.kind == 't') {
            final OtpErlangObject[] vv = new OtpErlangObject[len];
            for (int i = 0; i < len; i++) {
                vv[i] = TypeConverter.java2erlang(Array.get(obj, i), type.content[i]);
            }
            return new OtpErlangTuple(vv);
        } else {
            TypeConverter.failConversion(obj, type);
        }
    }
    if (type.kind == 's' && obj != null) {
        return new OtpErlangString(obj.toString());
    }
    if (type.kind == 'b' && obj != null) {
        return new OtpErlangBinary(obj.toString().getBytes());
    }
    TypeConverter.failConversion(obj, type);
    return null;
}
Also used : OtpErlangChar(com.ericsson.otp.erlang.OtpErlangChar) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangPid(com.ericsson.otp.erlang.OtpErlangPid) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) Collection(java.util.Collection) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangRef(com.ericsson.otp.erlang.OtpErlangRef) Map(java.util.Map) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 2 with OtpErlangMap

use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.

the class TermParserTest method map_2a.

@Test(expected = OtpParserException.class)
public void map_2a() throws OtpParserException {
    final OtpErlangMap r = (OtpErlangMap) termParser.parse("#{a=>2 \"b\"=>[4]}");
    Assert.assertEquals("#{a => 2,\"b\" => [4]}", r.toString());
}
Also used : OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) Test(org.junit.Test)

Example 3 with OtpErlangMap

use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.

the class TermParserTest method map_2.

@Test
public void map_2() throws OtpParserException {
    final OtpErlangMap r = (OtpErlangMap) termParser.parse("#{a=>2,\"b\"=>[4]}");
    Assert.assertEquals("#{a => 2,\"b\" => [4]}", r.toString());
}
Also used : OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) Test(org.junit.Test)

Example 4 with OtpErlangMap

use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.

the class TypeConverterTest method cvtMapOk_2.

@Test
public void cvtMapOk_2() throws SignatureException {
    final Map<OtpErlangObject, OtpErlangObject> map = new HashMap<>();
    map.put(new OtpErlangAtom("foo"), new OtpErlangAtom("bar"));
    test(map, "m", new OtpErlangMap(new OtpErlangObject[] { new OtpErlangAtom("foo") }, new OtpErlangObject[] { new OtpErlangAtom("bar") }));
}
Also used : HashMap(java.util.HashMap) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) Test(org.junit.Test)

Example 5 with OtpErlangMap

use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.

the class TermParserTest method map_1.

@Test
public void map_1() throws OtpParserException {
    final OtpErlangMap r = (OtpErlangMap) termParser.parse("#{}");
    Assert.assertEquals("#{}", r.toString());
}
Also used : OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) Test(org.junit.Test)

Aggregations

OtpErlangMap (com.ericsson.otp.erlang.OtpErlangMap)7 Test (org.junit.Test)5 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)4 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)3 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)2 OtpErlangRef (com.ericsson.otp.erlang.OtpErlangRef)2 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)2 HashMap (java.util.HashMap)2 OtpErlangBinary (com.ericsson.otp.erlang.OtpErlangBinary)1 OtpErlangByte (com.ericsson.otp.erlang.OtpErlangByte)1 OtpErlangChar (com.ericsson.otp.erlang.OtpErlangChar)1 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)1 OtpErlangPid (com.ericsson.otp.erlang.OtpErlangPid)1 OtpErlangShort (com.ericsson.otp.erlang.OtpErlangShort)1 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)1 Method (java.lang.reflect.Method)1 BigInteger (java.math.BigInteger)1 Collection (java.util.Collection)1 Map (java.util.Map)1