Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.Console;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.io.Reader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import java.util.Arrays;
  14.  
  15. public class FileReaderSecond {
  16. static ArrayList<Mapper> readClasses = new ArrayList<Mapper>();
  17. static int lines = 0;
  18. static String strCurrentLine;
  19.  
  20. public FileReaderSecond(String string) {
  21. // TODO Auto-generated constructor stub
  22. }
  23.  
  24. private static int findIndexOfClassByName(String className) {
  25. for (int i = 0; i < readClasses.size(); i++) {
  26. if (new String(className).equals(readClasses.get(i).getMainClassName())) {
  27. return i;
  28. }
  29. }
  30. // If not in the list, create new class and add to arrayList
  31. readClasses.add(new Mapper(className));
  32. return (readClasses.size() - 1); // last one is newly added class
  33. }
  34.  
  35. private static int getAncestorClassCount(Mapper childClass) {
  36. if (childClass.getParentClass() == null) {
  37. return 0;
  38. } else {
  39. return 1 + getAncestorClassCount(childClass.getParentClass()); // recursive method
  40. }
  41. }
  42.  
  43. public static void main(String args[]) throws IOException {
  44.  
  45. // File Read
  46. BufferedReader objReader = null;
  47.  
  48. InputStreamReader in = new InputStreamReader(System.in);
  49. BufferedReader location = new BufferedReader(in);
  50.  
  51. System.out.println("Enter The location");
  52. String name = location.readLine();
  53.  
  54. // File file = new
  55. // File("C:\\Users\\Sumi\\eclipse-workspace\\Sample\\src\\spm.java");
  56. File file = new File(name);
  57.  
  58. try {
  59.  
  60. java.io.FileReader fr = new java.io.FileReader(file);
  61.  
  62. objReader = new BufferedReader(fr);
  63.  
  64. Mapper childClassObject = null;
  65.  
  66. // ArrayList<String> classlist = new ArrayList<String>();
  67. // ArrayList<String> ancestorlist = new ArrayList<String>();
  68.  
  69. // Loop Lines in the file
  70. while ((strCurrentLine = objReader.readLine()) != null) {
  71. String originlLine = strCurrentLine;
  72. strCurrentLine = strCurrentLine.trim().toUpperCase();
  73. String[] excludedLineChars = new String[] { "{", "}", "{{", "}}", "{{{", "}}}" };
  74. List<String> excludedLineCharsList = Arrays.asList(excludedLineChars);
  75.  
  76. if (strCurrentLine != "" && !excludedLineCharsList.contains(strCurrentLine)) {
  77. // Excluding methods
  78. if (!strCurrentLine.startsWith("PUBLIC") && !strCurrentLine.startsWith("PRIVATE")
  79. && !strCurrentLine.startsWith("PROTECTED") && !strCurrentLine.startsWith("STATIC")
  80. && !strCurrentLine.startsWith("VOID")) {
  81.  
  82. lines++;
  83.  
  84. // regular expression
  85. String rel = "class\\s+((?:[a-z][a-z]*[0-9]*[a-z0-9]*))\\sextends\\s((?:[a-z][a-z]*[0-9]*[a-z0-9]*))";
  86.  
  87. Pattern p = Pattern.compile(rel, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
  88. Matcher m = p.matcher(strCurrentLine);
  89. // System.out.println("regular");
  90. // System.out.println("printing lines ==> " +strCurrentLine);
  91. //
  92.  
  93. if (m.find()) {
  94. String childClassName = m.group(1);
  95. String parentClassName = m.group(2);
  96.  
  97. if (parentClassName != null) {
  98. int parentClassIndex = findIndexOfClassByName(parentClassName); // add to the
  99. // readClasses
  100. // and return the index
  101. Mapper parentClassObject = readClasses.get(parentClassIndex);
  102.  
  103. int childClassIndex = findIndexOfClassByName(childClassName);
  104. childClassObject = readClasses.get(childClassIndex);
  105.  
  106. childClassObject.addParentClass(parentClassObject);
  107. } else {
  108. // when we call findIndexOfClassByName, will also add to readClasses if not
  109. // exist
  110. findIndexOfClassByName(childClassName);
  111. }
  112.  
  113. }
  114.  
  115. if (childClassObject != null) {
  116. childClassObject.addOneLine(originlLine.trim());
  117. }
  118.  
  119. } else {
  120. // System.out.println("Starting with excluding methods : " + strCurrentLine);
  121. }
  122. }
  123. }
  124.  
  125. } catch (FileNotFoundException e) {
  126. e.printStackTrace();
  127. }
  128.  
  129. // finally loop the readclasses and print the ancestor count
  130. int maxAncestorClasscount = 0;
  131.  
  132. int sum = 0;
  133.  
  134. for (Mapper readClass : readClasses) {
  135. int weight = 0;
  136. int ancestorClassCount = getAncestorClassCount(readClass);
  137. int totLines = readClass.getTotalLines();
  138. weight = (ancestorClassCount + 1) * totLines;
  139.  
  140. System.out.print(readClass.getMainClassName() + " [Lines " + totLines + " ]");
  141. System.out.println(" ==> Ancestor Class Count : " + ancestorClassCount + " | Sum : " + sum);
  142.  
  143. if (ancestorClassCount > maxAncestorClasscount) {
  144. maxAncestorClasscount = ancestorClassCount;
  145. }
  146.  
  147. for (String line : readClass.getLines()) {
  148. System.out.println(line + "-----------------> " + (ancestorClassCount + 1));
  149. sum += (ancestorClassCount + 1);
  150. }
  151. System.out.print("\n\n");
  152. } // end of for loop readclass
  153. System.out.println("\n\nTotal sum of Inheritance is : " + sum);
  154.  
  155. // get the total lines in the program
  156. System.out.println("Total Lines in the program : " + lines);
  157. System.out.println(
  158. "---------------------------------------------------------------------------------------------------");
  159.  
  160. // final
  161.  
  162. System.out.println(
  163. "\n\nComplexity of a class due its inheritance(CCi) = Number of ancestor classes of the class+1");
  164. System.out.println("CCi = " + (maxAncestorClasscount + 1));
  165.  
  166. System.out.println("\n========================================================================");
  167.  
  168. }
  169.  
  170. }
  171.  
  172. class Mapper {
  173. String mainclassName;
  174. int totalLines;
  175. Mapper parentClass;
  176. List<String> lines;
  177.  
  178. Mapper(String mainclassName) {
  179. this.mainclassName = mainclassName;
  180. this.totalLines = 0;
  181. this.parentClass = null;
  182. this.lines = new ArrayList<>();
  183. }
  184.  
  185. public String getMainClassName() {
  186. return this.mainclassName;
  187. }
  188.  
  189. public void addParentClass(Mapper mainClass) {
  190. this.parentClass = mainClass;
  191. }
  192.  
  193. public Mapper getParentClass() {
  194. return this.parentClass;
  195. }
  196.  
  197. public void addOneLine(String line) {
  198. lines.add(line);
  199. this.totalLines += 1;
  200. }
  201.  
  202. public int getTotalLines() {
  203. return this.totalLines - 1 > 0 ? this.totalLines - 1 : 0;
  204. }
  205.  
  206. public List<String> getLines() {
  207. return this.lines;
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement