Java + Guava 16 (구아바는 그다지 필요하지는 않지만 작성하기가 다소 성가신 것입니다).
알았어, 일하는거야? 실제로 실제 Java 코드를 작성하고 실제로 컴파일하는 프로그램은 어떻습니까?
애니메이션을 시연하기는 어렵지만이 프로그램은 기본 사전 (250 개의 일반 영어 단어) 또는 줄 바꿈으로 구분 된 파일 (명령 줄 인수로 사용)을 사용하여 Java 프로그램을 작성하고 한 번에 한 문자 씩 콘솔 에 입력합니다. 인간처럼 보이는 속도로 . 이 게시물은 정의가 없으므로 직접 실행해야합니다. 완료되면 1 분 정도 기다린 다음 많은 빈 줄을 콘솔에 인쇄하고 다시 시작합니다. 다양한 매개 변수를 합리적으로 조정할 수 있도록 작성하려고했습니다.
또한 일반적 으로이 파일을 둘 이상의 파일에 넣었지만 실행하기 쉽도록 모든 클래스를 함께 밀었습니다.
package org.stackoverflow.ppcg;
import java.io.*;
import java.util.*;
import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import com.google.common.collect.Lists;
public class CodeGenerator {
public static final Converter<String, String> TOUPPER =
CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);
public static final Converter<String, String> TOLOWER =
CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
public static final String[] TYPES = new String[]{
"int", "long", "double", "String"
};
public static final List<String> DEFAULT_LIST = Arrays.asList(new String[]{
"the", "and", "for", "you", "say", "but", "his", "not", "she", "can",
"who", "get", "her", "all", "one", "out", "see", "him", "now", "how",
"its", "our", "two", "way", "new", "day", "use", "man", "one", "her",
"any", "may", "try", "ask", "too", "own", "out", "put", "old", "why",
"let", "big", "few", "run", "off", "all", "lot", "eye", "job", "far",
"have", "that", "with", "this", "they", "from", "that", "what", "make", "know",
"will", "time", "year", "when", "them", "some", "take", "into", "just", "your",
"come", "than", "like", "then", "more", "want", "look", "also", "more", "find",
"here", "give", "many", "well", "only", "tell", "very", "even", "back", "good",
"life", "work", "down", "call", "over", "last", "need", "feel", "when", "high",
"their", "would", "about", "there", "think", "which", "could", "other", "these", "first",
"thing", "those", "woman", "child", "there", "after", "world", "still", "three", "state",
"never", "leave", "while", "great", "group", "begin", "where", "every", "start", "might",
"about", "place", "again", "where", "right", "small", "night", "point", "today", "bring",
"large", "under", "water", "write", "money", "story", "young", "month", "right", "study",
"people", "should", "school", "become", "really", "family", "system", "during", "number", "always",
"happen", "before", "mother", "though", "little", "around", "friend", "father", "member", "almost",
"change", "minute", "social", "follow", "around", "parent", "create", "others", "office", "health",
"person", "within", "result", "change", "reason", "before", "moment", "enough", "across", "second",
"toward", "policy", "appear", "market", "expect", "nation", "course", "behind", "remain", "effect",
"because", "through", "between", "another", "student", "country", "problem", "against", "company", "program",
"believe", "without", "million", "provide", "service", "however", "include", "several", "nothing", "whether",
"already", "history", "morning", "himself", "teacher", "process", "college", "someone", "suggest", "control",
"perhaps", "require", "finally", "explain", "develop", "federal", "receive", "society", "because", "special",
"support", "project", "produce", "picture", "product", "patient", "certain", "support", "century", "culture"
});
private static final int CLASS_NAME_LENGTH = 2;
private final WordList wordList;
private final Appendable out;
private final Random r = new Random();
private CodeGenerator(WordList wordList, Appendable out) {
this.wordList = wordList;
this.out = out;
}
public static void main(String... args) throws Exception {
List<?> wordSource = getWords(args);
WordList list = new WordList(wordSource);
SleepingAppendable out = new SleepingAppendable(System.out);
CodeGenerator generator = new CodeGenerator(list, out);
while(!Thread.interrupted()) {
generator.generate();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
break;
}
out.setSleeping(false);
for(int i = 0; i < 100; i++) {
out.append(System.lineSeparator());
}
out.setSleeping(true);
}
}
private static List<?> getWords(String[] args) {
if(args.length > 0) {
try {
return getListFromFile(args[0]);
} catch(IOException e) { }
}
return DEFAULT_LIST;
}
private static List<Object> getListFromFile(String string) throws IOException {
List<Object> newList = Lists.newArrayList();
File f = new File(string);
Scanner s = new Scanner(f);
while(s.hasNext()) {
newList.add(s.nextLine());
}
return newList;
}
private void generate() throws IOException {
String className = beginClass();
List<Field> finalFields = generateFields(true);
printFields(finalFields);
out.append(System.lineSeparator());
List<Field> mutableFields = generateFields(false);
printFields(mutableFields);
out.append(System.lineSeparator());
printConstructor(className, finalFields);
printGetters(finalFields);
printGetters(mutableFields);
printSetters(mutableFields);
endClass();
}
private void printGetters(List<Field> fields) throws IOException {
for(Field f : fields) {
out.append(System.lineSeparator());
f.printGetter(out);
}
}
private void printSetters(List<Field> fields) throws IOException {
for(Field f : fields) {
out.append(System.lineSeparator());
f.printSetter(out);
}
}
private void printConstructor(String className, List<Field> finalFields) throws IOException {
out.append("\tpublic ").append(className).append('(');
printArgs(finalFields);
out.append(") {").append(System.lineSeparator());
for(Field f : finalFields) {
f.printAssignment(out);
}
out.append("\t}").append(System.lineSeparator());
}
private void printArgs(List<Field> finalFields) throws IOException {
if(finalFields.size() == 0) return;
Iterator<Field> iter = finalFields.iterator();
while(true) {
Field next = iter.next();
next.printTypeAndName(out);
if(!iter.hasNext()) break;
out.append(", ");
}
}
private List<Field> generateFields(boolean isfinal) {
int numFields = r.nextInt(3) + 2;
List<Field> newFields = Lists.newArrayListWithCapacity(numFields);
for(int i = 0; i < numFields; i++) {
String type = TYPES[r.nextInt(4)];
newFields.add(new Field(type, wordList.makeLower(r.nextInt(2) + 1), isfinal));
}
return newFields;
}
private void printFields(List<Field> finalFields) throws IOException {
for(Field f : finalFields) {
f.printFieldDeclaration(out);
}
}
private String beginClass() throws IOException {
out.append("public class ");
String className = wordList.nextClassName(CLASS_NAME_LENGTH);
out.append(className).append(" {").append(System.lineSeparator());
return className;
}
private void endClass() throws IOException {
out.append("}");
}
private static class WordList {
private final Random r = new Random();
private final List<?> source;
private WordList(List<?> source) {
this.source = source;
}
private String makeUpper(int length) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < length; i++) {
sb.append(randomWord());
}
return sb.toString();
}
private String makeLower(int length) {
return TOLOWER.convert(makeUpper(length));
}
private String randomWord() {
int sourceIndex = r.nextInt(source.size());
return TOUPPER.convert(source.get(sourceIndex).toString().toLowerCase());
}
public String nextClassName(int length) {
return makeUpper(length);
}
}
private static class Field {
private final String type;
private final String fieldName;
private final boolean isfinal;
Field(String type, String fieldName, boolean isfinal) {
this.type = type;
this.fieldName = fieldName;
this.isfinal = isfinal;
}
void printFieldDeclaration(Appendable appendable) throws IOException {
appendable.append("\tprivate ");
if(isfinal) appendable.append("final ");
printTypeAndName(appendable);
appendable.append(';').append(System.lineSeparator());
}
void printTypeAndName(Appendable appendable) throws IOException {
appendable.append(type).append(' ').append(fieldName);
}
void printGetter(Appendable appendable) throws IOException {
appendable.append("\tpublic ");
appendable.append(type).append(" get").append(TOUPPER.convert(fieldName));
appendable.append("() {").append(System.lineSeparator());
appendable.append("\t\treturn ").append(fieldName).append(';');
appendable.append(System.lineSeparator()).append("\t}").append(System.lineSeparator());
}
void printSetter(Appendable appendable) throws IOException {
appendable.append("\tpublic void set");
appendable.append(TOUPPER.convert(fieldName));
appendable.append("(").append(type).append(' ').append(fieldName);
appendable.append(") {").append(System.lineSeparator());
printAssignment(appendable);
appendable.append("\t}").append(System.lineSeparator());
}
void printAssignment(Appendable appendable) throws IOException {
appendable.append("\t\tthis.").append(fieldName).append(" = ").append(fieldName);
appendable.append(';').append(System.lineSeparator());
}
}
private static class SleepingAppendable implements Appendable {
private Random r = new Random();
private Appendable backing;
private boolean sleeping = true;
public SleepingAppendable(Appendable backing) {
this.backing = backing;
}
@Override
public Appendable append(CharSequence csq) throws IOException {
return append(csq, 0, csq.length());
}
@Override
public Appendable append(CharSequence csq, int start, int end)
throws IOException {
for(int i = start; i < end; i++) {
append(csq.charAt(i));
}
sleep(100, 300);
return this;
}
@Override
public Appendable append(char c) throws IOException {
sleep(170, 80);
backing.append(c);
return this;
}
private void sleep(int base, int variation) {
if(!sleeping) return;
try {
Thread.sleep((long) (r.nextInt(80) + 70));
} catch (InterruptedException e) {
}
}
public boolean isSleeping() {
return sleeping;
}
public void setSleeping(boolean sleeping) {
this.sleeping = sleeping;
}
}
}
샘플 프로그램 출력 (단 하나의 프로그램)
public class GetGroup {
private final double thoughRight;
private final double socialYear;
private final double manOne;
private final int appear;
private double man;
private double comeHis;
private double certain;
public GetGroup(double thoughRight, double socialYear, double manOne, int appear) {
this.thoughRight = thoughRight;
this.socialYear = socialYear;
this.manOne = manOne;
this.appear = appear;
}
public double getThoughRight() {
return thoughRight;
}
public double getSocialYear() {
return socialYear;
}
public double getManOne() {
return manOne;
}
public int getAppear() {
return appear;
}
public double getMan() {
return man;
}
public double getComeHis() {
return comeHis;
}
public double getCertain() {
return certain;
}
public void setMan(double man) {
this.man = man;
}
public void setComeHis(double comeHis) {
this.comeHis = comeHis;
}
public void setCertain(double certain) {
this.certain = certain;
}
}
다른 샘플 출력 :
public class TryControl {
private final int over;
private final double thatState;
private final long jobInto;
private final long canPut;
private int policy;
private int neverWhile;
public TryControl(int over, double thatState, long jobInto, long canPut) {
this.over = over;
this.thatState = thatState;
this.jobInto = jobInto;
this.canPut = canPut;
}
public int getOver() {
return over;
}
public double getThatState() {
return thatState;
}
public long getJobInto() {
return jobInto;
}
public long getCanPut() {
return canPut;
}
public int getPolicy() {
return policy;
}
public int getNeverWhile() {
return neverWhile;
}
public void setPolicy(int policy) {
this.policy = policy;
}
public void setNeverWhile(int neverWhile) {
this.neverWhile = neverWhile;
}
}