Created
November 6, 2018 14:10
-
-
Save tobysmith568/7306c4e3f37fd8fad1a18ebe05817a9a to your computer and use it in GitHub Desktop.
Java Builder Pattern Creator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.Calendar; | |
public class BuilderMaker | |
{ | |
public static void main(String args[]) | |
{ | |
if (args.length < 1) | |
args = new String[] {"ClassName"}; | |
String line; | |
Scanner scanner = new Scanner(System.in); | |
List<String> result = new ArrayList<String>(); | |
result.add("/*"); | |
result.add(" * Copyright (c) " + Calendar.getInstance().get(Calendar.YEAR) + ". All Rights Reserved."); | |
result.add(" */"); | |
result.add("package com.....;"); | |
result.add(""); | |
result.add("/**"); | |
result.add(" * Builder class for {@link " + args[0] + "}"); | |
result.add(" * "); | |
result.add(" * @author "); | |
result.add(" */"); | |
result.add("public class " + args[0] + "Builder implements IBuilder<" + args[0] + ">"); | |
result.add("{"); | |
result.add(" private " + args[0] + " result = new " + args[0] + "();"); | |
try | |
{ | |
while(true) | |
{ | |
line = scanner.nextLine(); | |
String[] parts = line.replaceFirst("^\\s+", "").split("\\s+"); | |
if (parts.length < 3) | |
continue; | |
parts[2] = parts[2].replaceFirst(";$", ""); | |
if (parts[2].length() >= 4 && "old_".equals(parts[2].substring(0, 4))) | |
continue; | |
result.add(" "); | |
result.add(" public " + args[0] + "Builder " + parts[2] + "(" + parts[1] + " " + parts[2] + ")"); | |
result.add(" {"); | |
result.add(" result." + parts[2] + " = " + parts[2] + ";"); | |
result.add(" return this;"); | |
result.add(" }"); | |
} | |
} | |
catch (java.util.NoSuchElementException e) | |
{ | |
} | |
finally | |
{ | |
scanner.close(); | |
} | |
result.add(" "); | |
result.add(" public " + args[0] + " build()"); | |
result.add(" {"); | |
result.add(" return result;"); | |
result.add(" }"); | |
result.add("}"); | |
for (String string : result) | |
{ | |
System.out.println(string); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment