Created
November 24, 2020 18:45
-
-
Save thanoojgithub/029cf662a3ebf378b81b4dd5ffd81726 to your computer and use it in GitHub Desktop.
Basic Auto Gen Class Util
This file contains hidden or 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
package com.autogenclass; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class AutoGenClassUtil { | |
public static void main(String[] args) { | |
String filePath = "/home/hduser/codebase/CoreJava8Assignments/src/main/java/com/autogenclass/Customer.txt"; | |
String fileName = Paths.get(filePath).getFileName().toString(); | |
fileName = (fileName != null && fileName.lastIndexOf(".") > 0) | |
? fileName.substring(0, fileName.lastIndexOf(".")) | |
: fileName; | |
String generateAClassString = GetClassStructure.generateAClassString(fileName, filePath); | |
try { | |
new File("/home/hduser/codebase/CoreJava8Assignments/src/main/java/com/autogenclass/" + fileName + ".java") | |
.delete(); | |
Files.write(Paths.get( | |
"/home/hduser/codebase/CoreJava8Assignments/src/main/java/com/autogenclass/" + fileName + ".java"), | |
generateAClassString.getBytes()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
class ReadDataFromFile { | |
public static String getClassName(String fileName) { | |
String s1 = fileName.substring(0, 1).toUpperCase(); | |
String nameCapitalized = s1 + fileName.substring(1); | |
return nameCapitalized; | |
} | |
public static String getFieldDataFromFile(String filePath) { | |
List<String[]> list = null; | |
try (Stream<String> stream = Files.lines(Paths.get(filePath))) { | |
List<String> collect = stream.collect(Collectors.toList()); | |
list = new ArrayList<>(); | |
for (String line : collect) { | |
String[] split = line.split(" "); | |
if (split.length >= 2) | |
list.add(split); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return String.join("", getFieldList(list)); | |
} | |
public static List<String> getFieldList(List<String[]> fields) { | |
List<String> list = new ArrayList<>(); | |
for (String[] strArry : fields) { | |
list.add(genenrateFieldData(strArry[0], strArry[1])); | |
} | |
return list; | |
} | |
public static String genenrateFieldData(String fieldName, String fieldType) { | |
String fieldTypeStr = ""; | |
if ("INT".equals(fieldType)) { | |
System.out.println(FieldType.INT.getFieldType()); | |
fieldTypeStr = "\n\tprivate " + FieldType.INT.getFieldType() + " " + fieldName + ";"; | |
} else if ("VARCHAR".equals(fieldType)) { | |
System.out.println(FieldType.INT.getFieldType()); | |
fieldTypeStr = "\n\tprivate " + FieldType.VARCHAR.getFieldType() + " " + fieldName + ";"; | |
} else if ("DATE".equals(fieldType)) { | |
System.out.println(FieldType.DATE.getFieldType()); | |
fieldTypeStr = "\n\tprivate " + FieldType.DATE.getFieldType() + " " + fieldName + ";"; | |
} | |
return fieldTypeStr; | |
} | |
} | |
class GetClassStructure { | |
public static String getClassDeclarationPart(String className) { | |
return "package com.autogenclass; \n\npublic class " + className + " {"; | |
} | |
public static String getClassEndPart() { | |
return "\n}"; | |
} | |
public static String generateAClassString(String fileName, String filePath) { | |
return getClassDeclarationPart(fileName) + ReadDataFromFile.getFieldDataFromFile(filePath) + getClassEndPart(); | |
} | |
} | |
enum FieldType { | |
INT("int"), VARCHAR("String"), DATE("java.time.LocalDate"); | |
private FieldType(final String fieldType) { | |
this.fieldType = fieldType; | |
} | |
private String fieldType; | |
public String getFieldType() { | |
return fieldType; | |
} | |
} |
This file contains hidden or 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
id INT | |
name VARCHAR | |
date DATE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment