package com.mebigfatguy.fbcontrib.utils;

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

/**
 * Constructs a method signature.
 */
public class SignatureBuilder {

    public static final String SIG_VOID_TO_VOID = new SignatureBuilder().toString();
    public static final String SIG_VOID_TO_BOOLEAN = new SignatureBuilder().withReturnType(Values.SIG_PRIMITIVE_BOOLEAN).toString();
    public static final String SIG_VOID_TO_INT = new SignatureBuilder().withReturnType(Values.SIG_PRIMITIVE_INT).toString();
    public static final String SIG_INT_TO_VOID = new SignatureBuilder().withParamTypes(Values.SIG_PRIMITIVE_INT).toString();
    public static final String SIG_INT_TO_OBJECT = new SignatureBuilder().withParamTypes(Values.SIG_PRIMITIVE_INT).withReturnType(Values.SLASHED_JAVA_LANG_OBJECT).toString();
    public static final String SIG_OBJECT_TO_OBJECT = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_LANG_OBJECT).withReturnType(Values.SLASHED_JAVA_LANG_OBJECT).toString();
    public static final String SIG_OBJECT_TO_BOOLEAN = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_LANG_OBJECT).withReturnType(Values.SIG_PRIMITIVE_BOOLEAN).toString();
    public static final String SIG_OBJECT_TO_STRING = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_LANG_OBJECT).withReturnType(Values.SLASHED_JAVA_LANG_STRING).toString();
    public static final String SIG_TWO_OBJECTS_TO_OBJECT = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_LANG_OBJECT, Values.SLASHED_JAVA_LANG_OBJECT).withReturnType(Values.SLASHED_JAVA_LANG_OBJECT).toString();
    public static final String SIG_STRING_TO_VOID = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_LANG_STRING).toString();
    public static final String SIG_VOID_TO_STRING = new SignatureBuilder().withReturnType(Values.SLASHED_JAVA_LANG_STRING).toString();
    public static final String SIG_COLLECTION_TO_BOOLEAN = new SignatureBuilder().withParamTypes(Values.SLASHED_JAVA_UTIL_COLLECTION).withReturnType(Values.SIG_PRIMITIVE_BOOLEAN).toString();

    private String methodName;
    private List<String> paramTypes;
    private String returnType;

    /**
     * Defaults assume that the method name is not included,
     * there are no parameters,
     * and the method returns void.
     */
    public SignatureBuilder() {
        methodName = "";
        paramTypes = new ArrayList<>();
        returnType = Values.SIG_VOID;
    }

    public SignatureBuilder withMethodName(String name) {
        methodName = name;
        return this;
    }

    public SignatureBuilder withParamTypes(String... types) {
        for (String type : types) {
            paramTypes.add(SignatureUtils.classToSignature(type));
        }
        return this;
    }

    public SignatureBuilder withParamTypes(Class... types) {
        for (Class type : types) {
            paramTypes.add(SignatureUtils.classToSignature(type.getName()));
        }
        return this;
    }

    public SignatureBuilder withReturnType(String type) {
        if ((type == null) || type.length() == 0) {
            throw new IllegalArgumentException("Missing return type; did you mean 'withoutReturnType'?");
        }
        returnType = SignatureUtils.classToSignature(type);
        return this;
    }

    public SignatureBuilder withReturnType(Class type) {
        if (type == null) {
            throw new IllegalArgumentException("Missing return type; did you mean 'withoutReturnType'?");
        }
        return withReturnType(type.getName());
    }

    public SignatureBuilder withoutReturnType() {
        returnType = "";
        return this;
    }

    @Override
    public String toString() {
        return methodName + '(' + join(paramTypes) + ')' + returnType;
    }

    private static String join(List<String> strings) {
        StringBuilder returnValue = new StringBuilder();
        for (String s : strings) {
            returnValue.append(s);
        }
        return returnValue.toString();
    }
}

@@ -1,8 +1,844 @@
+/*%0A * fb-contrib - Auxiliary detectors for Java programs%0A * Copyright (C) 2005-2016 ThrawnCA%0A *%0A * This library is free software; you can redistribute it and/or%0A * modify it under the terms of the GNU Lesser General Public%0A * License as published by the Free Software Foundation; either%0A * version 2.1 of the License, or (at your option) any later version.%0A *%0A * This library is distributed in the hope that it will be useful,%0A * but WITHOUT ANY WARRANTY; without even the implied warranty of%0A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU%0A * Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public%0A * License along with this library; if not, write to the Free Software%0A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA%0A */%0A
 package 
@@ -1580,16 +1580,29 @@
 IVE_INT)
+%0A            
 .withRet
@@ -1770,32 +1770,45 @@
 AVA_LANG_OBJECT)
+%0A            
 .withReturnType(
@@ -1969,32 +1969,45 @@
 AVA_LANG_OBJECT)
+%0A            
 .withReturnType(
@@ -2164,32 +2164,45 @@
 AVA_LANG_OBJECT)
+%0A            
 .withReturnType(
@@ -2319,32 +2319,45 @@
 gnatureBuilder()
+%0A            
 .withParamTypes(
@@ -2885,16 +2885,29 @@
 LECTION)
+%0A            
 .withRet
@@ -3125,23 +3125,16 @@
 ncluded,
-%0A     *
  there a
@@ -3150,23 +3150,16 @@
 ameters,
-%0A     *
  and the
@@ -3945,16 +3945,17 @@
 ull) %7C%7C 
+(
 type.len
@@ -3965,16 +3965,17 @@
 () == 0)
+)
  %7B%0A     

add license header