package ethereumjava.solidity.types;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ethereumjava.exception.EthereumJavaException;
import ethereumjava.solidity.SolidityUtils;

/**
 * Created by gunicolas on 08/08/16.
 */
public abstract class SType<T> {

    public static int ENCODED_SIZE = 64;
    T value;

    public SType(T value) {
        this.value = value;
    }

    public static int staticPartLength(String name) {
        return -1;
    }

    public static List<String> nestedTypes(String name) {
        ArrayList<String> matches = new ArrayList<>();
        Matcher m = Pattern.compile("(\\[[0-9]*\\])").matcher(name);
        while (m.find()) matches.add(m.group());
        return matches;
    }

    public static boolean isDynamicArray(String name) {
        List<String> nestedTypes = nestedTypes(name);
        if (nestedTypes == null || nestedTypes.size() == 0) return false;
        String last = nestedTypes.get(nestedTypes.size() - 1);
        return !Pattern.compile("\\[[0-9]+\\]").matcher(last).matches();
    }

    public static boolean isStaticArray(String name) {
        List<String> nestedTypes = nestedTypes(name);
        if (nestedTypes == null || nestedTypes.size() == 0) return false;
        return !isDynamicArray(name);
    }

    public static int staticArrayLength(String name) {
        List<String> nestedTypes = nestedTypes(name);
        if (nestedTypes == null || !isStaticArray(name)) return 1;
        String last = nestedTypes.get(nestedTypes.size() - 1);
        return Integer.parseInt(last.substring(1, last.length() - 1));
    }

    public static String nestedName(String name) {
        List<String> nestedTypes = nestedTypes(name);
        if (nestedTypes == null || nestedTypes.size() == 0) return name;
        String last = nestedTypes.get(nestedTypes.size() - 1);
        return name.substring(0, name.length() - last.length());
    }

    public static boolean isDynamic(Object object) {
        if (!isSType(object))
            throw new EthereumJavaException("Type Error. Expected SType, got " + object.getClass().getSimpleName());
        String typeName = SolidityUtils.extractSolidityTypeName(object);
        return SType.isDynamicArray(typeName) || ((SType) object).isDynamicType();
    }

    public static boolean isSType(Object o) {
        Class clazz = o.getClass();
        if (clazz.isArray()) {
            clazz = clazz.getComponentType();
        }
        return clazz.isAssignableFrom(SType.class);
    }

    public abstract boolean isDynamicType();

    public final T get() {
        return value;
    }

    public abstract String asString();

    public static Class<? extends SType> getClazz(){
        return SType.class;
    }

    @Override
    public boolean equals(Object o) {
        if( !(o instanceof SType) ) return false;
        SType toCompare = (SType) o;
        return get().equals(toCompare.get());
    }
}

@@ -35,2581 +35,203 @@
 s;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0Aimport ethereumjava.exception.EthereumJavaException;%0Aimport ethereumjava.solidity.SolidityUtils;%0A%0A/**%0A * Created by gunicolas on 08/08/16.%0A */%0Apublic abstract class SType%3CT%3E %7B%0A%0A    public static int ENCODED_SIZE = 64;%0A    T value;%0A%0A    public SType(T value) %7B%0A        this.value = value;%0A    %7D%0A%0A    public static int staticPartLength(String name) %7B%0A        return -1;%0A    %7D%0A%0A    public static List%3CString%3E nestedTypes(String name) %7B%0A        ArrayList%3CString%3E matches = new ArrayList%3C%3E();%0A        Matcher m = Pattern.compile(%22(%5C%5C%5B%5B0-9%5D*%5C%5C%5D)%22).matcher(name);%0A        while (m.find()) matches.add(m.group());%0A        return matches;%0A    %7D%0A%0A    public static boolean isDynamicArray(String name) %7B%0A        List%3CString%3E nestedTypes = nestedTypes(name);%0A        if (nestedTypes == null %7C%7C nestedTypes.size() == 0) return false;%0A        String last = nestedTypes.get(nestedTypes.size() - 1);%0A        return !Pattern.compile(%22%5C%5C%5B%5B0-9%5D+%5C%5C%5D%22).matcher(last).matches();%0A    %7D%0A%0A    public static boolean isStaticArray(String name) %7B%0A        List%3CString%3E nestedTypes = nestedTypes(name);%0A        if (nestedTypes == null %7C%7C nestedTypes.size() == 0) return false;%0A        return !isDynamicArray(name);%0A    %7D%0A%0A    public static int staticArrayLength(String name) %7B%0A        List%3CString%3E nestedTypes = nestedTypes(name);%0A        if (nestedTypes == null %7C%7C !isStaticArray(name)) return 1;%0A        String last = nestedTypes.get(nestedTypes.size() - 1);%0A        return Integer.parseInt(last.substring(1, last.length() - 1));%0A    %7D%0A%0A    public static String nestedName(String name) %7B%0A        List%3CString%3E nestedTypes = nestedTypes(name);%0A        if (nestedTypes == null %7C%7C nestedTypes.size() == 0) return name;%0A        String last = nestedTypes.get(nestedTypes.size() - 1);%0A        return name.substring(0, name.length() - last.length());%0A    %7D%0A%0A    public static boolean isDynamic(Object object) %7B%0A        if (!isSType(object))%0A            throw new EthereumJavaException(%22Type Error. Expected SType, got %22 + object.getClass().getSimpleName());%0A        String typeName = SolidityUtils.extractSolidityTypeName(object);%0A        return SType.isDynamicArray(typeName) %7C%7C ((SType) object).isDynamicType();%0A    %7D%0A%0A    public static boolean isSType(Object o) %7B%0A        Class clazz = o.getClass();%0A        if (clazz.isArray()) %7B%0A            clazz = clazz.getComponentType();%0A        %7D%0A        return clazz.isAssignableFrom(SType.class);%0A    %7D%0A%0A    public abstract boolean isDynamicType();
+/**%0A * Created by gunicolas on 08/08/16.%0A */%0Apublic abstract class SType%3CT%3E %7B%0A%0A    public static int ENCODED_SIZE = 64;%0A    T value;%0A%0A    public SType(T value) %7B%0A        this.value = value;%0A    %7D
 %0A%0A  

clear solidity type class from useless functions