package edu.mit.streamjit.impl.compiler;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;

/**
 * MethodType represents the type of a method, including the types of its
 * arguments and its return type, which must all be RegularTypes.
 * @author Jeffrey Bosboom <jeffreybosboom@gmail.com>
 * @since 3/6/2013
 */
public class MethodType extends Type {
	private final RegularType returnType;
	private final RegularType[] argumentTypes;
	private MethodType(RegularType returnType, RegularType[] argumentTypes) {
		this.returnType = returnType;
		this.argumentTypes = argumentTypes;
	}
	public static MethodType of(RegularType returnType, RegularType... argumentTypes) {
		return new MethodType(returnType, argumentTypes);
	}
	public RegularType getReturnType() {
		return returnType;
	}
	public int getNumArguments() {
		return argumentTypes.length;
	}
	public RegularType getArgumentType(int x) {
		return argumentTypes[x];
	}
	public Iterator<RegularType> argumentTypeIterator() {
		return Arrays.asList(argumentTypes).iterator();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final MethodType other = (MethodType)obj;
		if (!Objects.equals(this.returnType, other.returnType))
			return false;
		if (!Arrays.deepEquals(this.argumentTypes, other.argumentTypes))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		int hash = 3;
		hash = 23 * hash + Objects.hashCode(this.returnType);
		hash = 23 * hash + Arrays.deepHashCode(this.argumentTypes);
		return hash;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(returnType);
		sb.append('(');
		if (argumentTypes.length >= 1)
			sb.append(argumentTypes[0]);
		for (int i = 1; i < argumentTypes.length; ++i)
			sb.append(", ").append(argumentTypes[i]);
		sb.append(')');
		return sb.toString();
	}
}

@@ -35,16 +35,54 @@
 piler;%0A%0A
+import com.google.common.base.Joiner;%0A
 import j
@@ -1688,230 +1688,65 @@
 %7B%0A%09%09
-StringBuilder sb = new StringBuilder();%0A%09%09sb.append(returnType);%0A%09%09sb.append('(');%0A%09%09if (argumentTypes.length %3E= 1)%0A%09%09%09sb.append(argumentTypes%5B0%5D);%0A%09%09for (int i = 1; i %3C argumentTypes.length; ++i)%0A%09%09%09sb.append
+return returnType.toString() + '(' + Joiner.on
 (%22, %22).
-append
+join
 (arg
@@ -1759,54 +1759,15 @@
 ypes
-%5Bi%5D);%0A%09%09sb.append(')');%0A%09%09return sb.toString()
+) + ')'
 ;%0A%09%7D

Simplify MethodType.toString with Guava Joiner