package com.cognitect.transit.impl;

import java.lang.reflect.Array;
import java.util.Collection;

/**
 * Created by fogus on 4/2/14.
 */
public class Util {

    public static long numberToPrimitiveLong(Object o) throws Exception {
        long i;

        if(o instanceof Long)
            i = ((Long)o).longValue();
        else if(o instanceof Integer)
            i = ((Integer)o).longValue();
        else if(o instanceof Short)
            i = ((Short)o).longValue();
        else if(o instanceof Byte)
            i = ((Byte)o).longValue();
        else
            throw new Exception("Unknown integer type: " + o.getClass());

        return i;
    }

	static String maybePrefix(String prefix, String tag, String s){
		if(prefix == null && tag == null)
			return s;
		prefix = (prefix == null) ? "" : prefix;
		tag = (tag == null) ? "" : tag;
		StringBuilder sb = new StringBuilder(prefix.length()+tag.length()+s.length());
		return sb.append(prefix).append(tag).append(s).toString();
	}

	public static long arraySize(Object a) {
	    if(a instanceof Collection)
	        return ((Collection)a).size();
	    else if (a.getClass().isArray())
	        return Array.getLength(a);
	    else if (a instanceof Iterable) {
	        int i = 0;
	        for (Object o : (Iterable) a) {
	            i++;
	        }
	        return i;
	    }
	    else
	        throw new UnsupportedOperationException("arraySize not supported on this type " + a.getClass().getSimpleName());

	}

	public static long mapSize(Object m) {
	    if(m instanceof Collection)
	        return ((Collection) m).size();
	    else
	        throw new UnsupportedOperationException("mapSize not supported on this type " + m.getClass().getSimpleName());
	}
}

@@ -90,16 +90,61 @@
 lection;
+%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0A/**%0A *
@@ -1376,24 +1376,101 @@
 rn i;%0A%09    %7D
+ else if (a instanceof List) %7B%0A            return ((List)a).size();%0A        %7D
 %0A%09    else%0A%09
@@ -1701,32 +1701,103 @@
 ion) m).size();%0A
+        else if (m instanceof Map)%0A            return ((Map)m).size();%0A
 %09    else%0A%09     

Add Map and List support to mapSize and arraySize, respectively