Skip to content

Instantly share code, notes, and snippets.

@porcelli
Created September 9, 2011 22:22
Show Gist options
  • Save porcelli/1207482 to your computer and use it in GitHub Desktop.
Save porcelli/1207482 to your computer and use it in GitHub Desktop.
public class GenerateSignatures {
public static void main(String... args) {
Parameter[] values = new Parameter[]{new Parameter("A", "int"), new Parameter("B", "String"), new Parameter("C", "long")};
Parameter[][] result = multiply(values);
for (int i = 0; i < result.length; i++) {
for (int k = 0; k < result[i].length; k++) {
System.out.print(result[i][k].toString() + " ");
}
System.out.println("");
}
}
public static Parameter[][] multiply(Parameter[] params) {
Parameter[][] result = new Parameter[1][params.length];
for (int i = 0; i < params.length; i++) {
result[0][i] = new Parameter(params[i].name);
}
for (Parameter param : params) {
result = multiply(result, param, params.length);
}
return result;
}
private static Parameter[][] multiply(Parameter[][] generate, Parameter placeholder, int length) {
String[] types = new String[]{"MyCustomType", placeholder.type};
Parameter[][] results = new Parameter[types.length * generate.length][length];
int n = 0;
for (int i = 0; i < types.length; i++) {
for (int j = 0; j < generate.length; j++) {
for (int k = 0; k < length; k++) {
results[n][k] = replace(generate[j][k], placeholder, types[i]);
}
n++;
}
}
return results;
}
private static Parameter replace(Parameter result, Parameter placeholder, String type) {
if (!placeholder.name.equals(result.name)) {
return new Parameter(result);
}
if (result.hasType()) {
return new Parameter(result);
} else {
return new Parameter(result.name, type);
}
}
public static class Parameter {
private final String name;
private final String type;
public Parameter(String name) {
this.name = name;
this.type = null;
}
public Parameter(String name, String type) {
this.name = name;
this.type = type;
}
public Parameter(Parameter base) {
this(base.name, base.type);
}
public boolean hasType() {
return this.type != null;
}
@Override
public String toString() {
return type + " " + name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment