Revision: 17610 http://sourceforge.net/p/gate/code/17610 Author: markagreenwood Date: 2014-03-10 08:37:37 +0000 (Mon, 10 Mar 2014) Log Message: ----------- closed a few resource leaks and some generics tidying
Modified Paths: -------------- gate/trunk/src/main/gate/util/reporting/DocTimeReporter.java gate/trunk/src/main/gate/util/reporting/PRTimeReporter.java Modified: gate/trunk/src/main/gate/util/reporting/DocTimeReporter.java =================================================================== --- gate/trunk/src/main/gate/util/reporting/DocTimeReporter.java 2014-03-10 02:20:13 UTC (rev 17609) +++ gate/trunk/src/main/gate/util/reporting/DocTimeReporter.java 2014-03-10 08:37:37 UTC (rev 17610) @@ -35,6 +35,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.io.IOUtils; + import gate.util.reporting.exceptions.BenchmarkReportExecutionException; import gate.util.reporting.exceptions.BenchmarkReportFileAccessException; import gate.util.reporting.exceptions.BenchmarkReportInputFileFormatException; @@ -72,7 +74,7 @@ /** Total time taken by the given pipeline for the current logical run. */ private float globalTotal = 0; /** A LinkedHashMap containing the documents matching the given PRs. */ - private LinkedHashMap<String, Object> docContainer = new LinkedHashMap<String, Object>(); + private LinkedHashMap<String, String> docContainer = new LinkedHashMap<String, String>(); /** * Folder where the benchmark.txt files are created for specific pipeline log * entries. @@ -160,6 +162,7 @@ * @return An Object containing modified hierarchical structure of processing * elements with totals and All others embedded in it. */ + @SuppressWarnings("unchecked") @Override public Object calculate(Object reportContainer) { return sortHashMapByValues( @@ -174,22 +177,22 @@ * An Object of type LinkedHashMap to be sorted by its values. * @return An Object containing the sorted LinkedHashMap. */ - private LinkedHashMap sortHashMapByValues(LinkedHashMap passedMap) { - List mapKeys = new ArrayList(passedMap.keySet()); - List mapValues = new ArrayList(passedMap.values()); + private LinkedHashMap<?,?> sortHashMapByValues(LinkedHashMap<String,String> passedMap) { + List<String> mapKeys = new ArrayList<String>(passedMap.keySet()); + List<String> mapValues = new ArrayList<String>(passedMap.values()); Collections.sort(mapValues, new ValueComparator()); Collections.sort(mapKeys); // Reversing the collection to sort the values in descending order Collections.reverse(mapValues); - LinkedHashMap sortedMap = new LinkedHashMap(); + LinkedHashMap<String,String> sortedMap = new LinkedHashMap<String,String>(); - Iterator<Integer> valueIt = mapValues.iterator(); + Iterator<String> valueIt = mapValues.iterator(); while (valueIt.hasNext()) { - Object val = valueIt.next(); + String val = valueIt.next(); Iterator<String> keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { - Object key = keyIt.next(); + String key = keyIt.next(); String comp1 = passedMap.get(key).toString(); String comp2 = val.toString(); @@ -214,24 +217,25 @@ * @return An Object containing the LinkedHashMap with the element values * totaled. */ - private LinkedHashMap<String, Object> doTotal( + @SuppressWarnings("unchecked") + private LinkedHashMap<String, String> doTotal( LinkedHashMap<String, Object> reportContainer) { LinkedHashMap<String, Object> myHash = reportContainer; Iterator<String> i = myHash.keySet().iterator(); while (i.hasNext()) { - Object key = i.next(); + String key = i.next(); if (myHash.get(key) instanceof LinkedHashMap) { docContainer = doTotal((LinkedHashMap<String, Object>) (myHash .get(key))); } else { if (docContainer.get(key) == null) { - docContainer.put((String) key, myHash.get(key)); + docContainer.put(key, (String)myHash.get(key)); } else { // Do total if value already exists - int val = Integer.parseInt((String) docContainer.get(key)) + int val = Integer.parseInt(docContainer.get(key)) + Integer.parseInt((String) myHash.get(key)); - docContainer.put((String) key, Integer.toString(val)); + docContainer.put(key, Integer.toString(val)); } } } @@ -248,6 +252,7 @@ * @param outputFile * Path where to save the report. */ + @SuppressWarnings("unchecked") @Override public void printReport(Object reportSource, File outputFile) { if (printMedia.equalsIgnoreCase(MEDIA_TEXT)) { @@ -269,6 +274,7 @@ */ private void printToText(Object reportContainer, File outputFile) { ArrayList<String> printLines = new ArrayList<String>(); + @SuppressWarnings("unchecked") LinkedHashMap<String, Object> rcHash = (LinkedHashMap<String, Object>) reportContainer; String docs = ""; @@ -473,6 +479,7 @@ * @param docName * Name of the document being processed. */ + @SuppressWarnings("unchecked") private void organizeEntries(LinkedHashMap<String, Object> store, String matchedPR, String bTime, String docName) { allDocs.add(docName); @@ -956,8 +963,9 @@ */ private long tail(File fileToBeRead, int chunkSize) throws BenchmarkReportInputFileFormatException { + RandomAccessFile raf = null; try { - RandomAccessFile raf = new RandomAccessFile(fileToBeRead, "r"); + raf = new RandomAccessFile(fileToBeRead, "r"); Vector<String> lastNlines = new Vector<String>(); int delta = 0; long curPos = 0; @@ -998,6 +1006,9 @@ e.printStackTrace(); return -1; } + finally { + IOUtils.closeQuietly(raf); + } } /** Modified: gate/trunk/src/main/gate/util/reporting/PRTimeReporter.java =================================================================== --- gate/trunk/src/main/gate/util/reporting/PRTimeReporter.java 2014-03-10 02:20:13 UTC (rev 17609) +++ gate/trunk/src/main/gate/util/reporting/PRTimeReporter.java 2014-03-10 08:37:37 UTC (rev 17610) @@ -36,6 +36,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.commons.io.IOUtils; + import gate.util.reporting.exceptions.BenchmarkReportExecutionException; import gate.util.reporting.exceptions.BenchmarkReportInputFileFormatException; import gnu.getopt.Getopt; @@ -253,6 +255,7 @@ * @param bTime * time(in milliseconds) of the benchmarkID token being processed. */ + @SuppressWarnings("unchecked") private void organizeEntries(LinkedHashMap<String, Object> store, String[] tokens, String bTime) { if (tokens.length > 0 && store.containsKey(tokens[0])) { @@ -316,6 +319,7 @@ * processing elements sorted in descending order of processing time * taken. */ + @SuppressWarnings("unchecked") private LinkedHashMap<String, Object> sortReport( LinkedHashMap<String, Object> gStore) { Iterator<String> i = gStore.keySet().iterator(); @@ -375,21 +379,21 @@ * * @return An Object containing the sorted LinkedHashMap. */ - private LinkedHashMap sortHashMapByValues(LinkedHashMap passedMap) { - List mapKeys = new ArrayList(passedMap.keySet()); - List mapValues = new ArrayList(passedMap.values()); + private LinkedHashMap<String,String> sortHashMapByValues(LinkedHashMap<String,String> passedMap) { + List<String> mapKeys = new ArrayList<String>(passedMap.keySet()); + List<String> mapValues = new ArrayList<String>(passedMap.values()); Collections.sort(mapValues, new ValueComparator()); Collections.sort(mapKeys); Collections.reverse(mapValues); - LinkedHashMap sortedMap = new LinkedHashMap(); + LinkedHashMap<String,String> sortedMap = new LinkedHashMap<String,String>(); - Iterator<Integer> valueIt = mapValues.iterator(); + Iterator<String> valueIt = mapValues.iterator(); while (valueIt.hasNext()) { - Object val = valueIt.next(); + String val = valueIt.next(); Iterator<String> keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { - Object key = keyIt.next(); + String key = keyIt.next(); String comp1 = passedMap.get(key).toString(); String comp2 = val.toString(); @@ -415,6 +419,7 @@ * @return An Object containing modified hierarchical structure of processing * elements with totals and All others embedded in it. */ + @SuppressWarnings("unchecked") @Override public Object calculate(Object reportContainer) { LinkedHashMap<String, Object> globalStore = @@ -442,6 +447,7 @@ * @return An integer containing the sub total. */ + @SuppressWarnings("unchecked") private int getTotal(LinkedHashMap<String, Object> reportContainer) { int total = 0; int diff = 0; @@ -482,6 +488,7 @@ * @param outputFile * Path where to save the report. */ + @SuppressWarnings("unchecked") @Override public void printReport(Object reportSource, File outputFile) { if (printMedia.equalsIgnoreCase(MEDIA_TEXT)) { @@ -504,6 +511,7 @@ * @param suppressZeroTimeEntries * Indicate whether or not to show 0 millisecond entries. */ + @SuppressWarnings("unchecked") private void printToText(Object reportContainer, File outputFile, boolean suppressZeroTimeEntries) { LinkedHashMap<String, Object> globalStore = @@ -541,6 +549,7 @@ * @param suppressZeroTimeEntries * Indicate whether or not to show 0 millisecond entries. */ + @SuppressWarnings("unchecked") private void prettyPrint(LinkedHashMap<String, Object> gStore, String separator, boolean suppressZeroTimeEntries) { @@ -707,6 +716,7 @@ * @param suppressZeroTimeEntries * Indicate whether or not to show 0 millisecond entries. */ + @SuppressWarnings("unchecked") private void generateCollapsibleHTMLTree(LinkedHashMap<String, Object> gStore, boolean suppressZeroTimeEntries) { Iterator<String> i = gStore.keySet().iterator(); @@ -824,8 +834,9 @@ */ private long tail(File fileToBeRead, int chunkSize) throws BenchmarkReportInputFileFormatException { + RandomAccessFile raf = null; try { - RandomAccessFile raf = new RandomAccessFile(fileToBeRead, "r"); + raf = new RandomAccessFile(fileToBeRead, "r"); Vector<String> lastNlines = new Vector<String>(); int delta = 0; long curPos = raf.length() - 1; @@ -866,6 +877,9 @@ e.printStackTrace(); return -1; } + finally { + IOUtils.closeQuietly(raf); + } } /** @@ -1027,6 +1041,7 @@ /** * Calls store, calculate and printReport for generating the actual report. */ + @SuppressWarnings("unchecked") private void generateReport() throws BenchmarkReportInputFileFormatException { Timer timer = null; @@ -1208,7 +1223,7 @@ * A Comparator class to compare the values of the LinkedHashMaps containing * processing elements and time taken by them. */ -class ValueComparator implements Comparator { +class ValueComparator implements Comparator<String> { /** * Provides the comparison logic between the processing time taken by * processing elements @@ -1223,9 +1238,9 @@ * obj1) */ @Override - public int compare(Object obj1, Object obj2) { - int i1 = Integer.parseInt((String) obj1); - int i2 = Integer.parseInt((String) obj2); + public int compare(String obj1, String obj2) { + int i1 = Integer.parseInt(obj1); + int i2 = Integer.parseInt(obj2); return i1 - i2; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/13534_NeoTech _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs