@Override public int compare(Object o1, Object o2) { CharLongPair e1 = (CharLongPair) o1; CharLongPair e2 = (CharLongPair) o2; if (e1.getL() < e2.getL()) { return -1; } if (e1.getL() == e2.getL()) { return 0; } return 1; // Implies e1.l > e2.l }
private CharLongPair(CharLongPair next) { this(next.getC(), next.getL()); }
private void analyzeFile(File file) { InputStream f = null; try { // Init the main set charOccurrences = new TreeSet<CharLongPair>(new CharLongPairComparator()); f = new BufferedInputStream(new FileInputStream(file)); long totalLength = f.available(); /// Count the characters char c; for (int i = 0; i < totalLength; i++) { c = (char) f.read(); if (c == -1) { break; } Iterator<CharLongPair> it = charOccurrences.iterator(); while (it.hasNext()) { CharLongPair cp = new CharLongPair(it.next()); if (cp.getC() == c) { charOccurrences.remove(cp); cp.setL(cp.getL() + 1); charOccurrences.add(cp); } continue; // Don't create a new dataset } charOccurrences.add(new CharLongPair(c, 1)); } /// Fill the table with the counts // Get the number of rows to display SpinnerNumberModel displayModel = (SpinnerNumberModel) displaySpinner.getModel(); int displayRows = displayModel.getNumber().intValue(); Iterator<CharLongPair> it = charOccurrences.iterator(); Vector tableData = new Vector(); for (; displayRows > 0; displayRows--) { if (!it.hasNext()) { break; } CharLongPair cp = it.next(); Vector rowData = new Vector(); rowData.add(cp.getC()); rowData.add(cp.getL()); rowData.add(100 * (cp.getL() / totalLength)); tableData.add(rowData); } Vector colNames = new Vector(); colNames.add("Character"); colNames.add("Count"); colNames.add("Percentage"); charPercentageTable.setModel(new DefaultTableModel(tableData, colNames)); } catch (FileNotFoundException ex) { Logger.getLogger(StatisticalAnalysisFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(StatisticalAnalysisFrame.class.getName()).log(Level.SEVERE, null, ex); } finally { try { f.close(); } catch (IOException ex) { Logger.getLogger(StatisticalAnalysisFrame.class.getName()).log(Level.SEVERE, null, ex); } } }