Passive Aggressive Style (Static Types)

As Java is statically typed, the type checking is already done on compile time. To simulate the manual type checking we created three variants of the original:

Passive Aggressive Style (Static Types) - Passive Aggressive Style (Types Checked) - Passive Aggressive Style (Types Cast)

TF_22_StaticTypes

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
 
public class TF_22_StaticTypes {
 
	private static void checkAssertionsEnabled() {
	  try {
	    assert false : "Let's see if you are even willing to recognize how wrong you treat me!";
	  } catch (Throwable e) {
	    return; // OK. At least you hear me when I openly complain.
	  }
	  throw new RuntimeException("You insensitive beeing!\n"
	      + "You could at least try to hear my complaints!\n"
	      + "Pass the argument -ea to the VM to enable the assertions.");
	}
 
	private static List<String> extractWords(String pathToFile) throws FileNotFoundException {
	  assert (pathToFile != null) && (pathToFile.length() > 0) : "I need a non-empty string! I quit!";
 
	  List<String> wordList = new ArrayList<String>();
	  Scanner scanner = new Scanner(new File(pathToFile));
	  scanner.useDelimiter("[\\W_]+");
	  while (scanner.hasNext()) {
	    wordList.add(scanner.next().toLowerCase());
	  }
	  scanner.close();
 
	  return wordList;
	}
 
	private static List<String> removeStopWords(List<String> wordList) throws IOException {
 
	  Set<String> stopWords = new HashSet<String>();
	  List<String> result = new ArrayList<String>();
 
	  FileReader fileReader = new FileReader("stop_words.txt");
	  BufferedReader reader = new BufferedReader(fileReader);
	  for (String stopWord : reader.readLine().split(",")) {
	    stopWords.add(stopWord);
	  }
	  reader.close();
 
	  for (char c = 'a'; c <= 'z'; c++) {
	    stopWords.add(Character.toString(c));
	  }
 
	  for (String word : wordList)
	    if (!stopWords.contains(word))
	      result.add(word);
 
	  return result;
	}
 
	private static Map<String, Integer> frequencies(List<String> wordList) {
	  assert wordList.size() > 0 : "I need a non-empty list! I quit!";
 
	  Map<String, Integer> frequencies = new HashMap<String, Integer>();
	  for (String word : wordList) {
	    Integer count = frequencies.get(word);
	    if (count == null) {
	      frequencies.put(word, 1);
	    } else {
	      frequencies.put(word, count + 1);
	    }
	  }
	  return frequencies;
	}
 
	private static List<Entry<String, Integer>> sort(Map<String, Integer> frequencies) {
	  assert frequencies.size() > 0 : "I need a non-empty dictionary! I quit!";
 
	  List<Entry<String, Integer>> sortedFrequencies = new ArrayList<Entry<String, Integer>>();
	  sortedFrequencies.addAll(frequencies.entrySet());
	  Comparator<Entry<String, Integer>> comparator = new Comparator<Entry<String, Integer>>() {
	    public int compare(Entry<String, Integer> left, Entry<String, Integer> right) {
	      return -left.getValue().compareTo(right.getValue());
	    }
	  };
	  Collections.sort(sortedFrequencies, comparator);
 
	  return sortedFrequencies;
	}
 
	public static void main(String[] args) {
	  checkAssertionsEnabled();
	  try {
	    assert args.length >= 1 : "You idiot! I need an input file! I quit!";
	    List<Map.Entry<String, Integer>> wordFrequencies = sort(frequencies(removeStopWords(extractWords(args[0]))));
 
	    assert wordFrequencies.size() >= 25 : "OMG! Less than 25 words! I QUIT!";
	    int count = 25;
	    for (Entry<String, Integer> entry : wordFrequencies) {
	      System.out.println(entry.getKey() + " - " + entry.getValue());
	      if (count-- <= 0)
	        break;
	    }
 
	  } catch (Throwable e) {
	    System.err.println("Something wrong: " + e.getMessage());
	    e.printStackTrace();
	  }
	}
 
}
Last modified: 2017/08/29 14:25
*