I wrote implementation of unix console program word count "wc" in Java. Please evaluate it. Is there any better solution?
import java.util.ArrayList;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
enum CountMode {
LINES,
BYTES,
SYMBOLS,
LONGEST_LENGTH,
WORDS,
DEFAULT
}
enum ReadMode {
FILE,
CONSOLE
}
class Counter {
private CountMode countMode;
private ReadMode readMode;
private ArrayList<File> files;
Counter(CountMode countMode, ReadMode readMode, ArrayList<File> files) {
this.countMode = countMode;
this.readMode = readMode;
this.files = files;
}
public void process() {
HashMap<File, ArrayList<String>> input = getInput();
HashMap<File, Integer> result = new HashMap<>();
for (Map.Entry<File, ArrayList<String>> pair : input.entrySet()) {
switch (countMode) {
case LINES:
result.put(pair.getKey(), countLines(pair.getValue()));
break;
case BYTES:
result.put(pair.getKey(), countBytes(pair.getValue()));
break;
case SYMBOLS:
result.put(pair.getKey(), countSymbols(pair.getValue()));
break;
case LONGEST_LENGTH:
result.put(pair.getKey(), getLongestLength(pair.getValue()));
break;
case WORDS:
result.put(pair.getKey(), countWords(pair.getValue()));
break;
case DEFAULT:
result.put(pair.getKey(), countWords(pair.getValue()));
break;
}
}
for (Map.Entry<File, Integer> data : result.entrySet()) {
if (data.getKey() != null) {
System.out.printf("%d\t%s\n", data.getValue(), data.getKey().getName());
}
else {
System.out.printf("%d\t%s", data.getValue(), "[console]");
}
}
}
private HashMap<File, ArrayList<String>> getInput() {
HashMap<File, ArrayList<String>> result = new HashMap<>();
switch (readMode) {
case FILE:
for (File file : files) {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
ArrayList<String> strings = new ArrayList<>();
String line;
while ((line = bufferedReader.readLine()) != null)
strings.add(line);
result.put(file, strings);
} catch (FileNotFoundException ex) {
System.err.println(ex.getMessage());
} catch (IOException ex) {
System.err.println("IO exception");
}
}
break;
case CONSOLE:
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
ArrayList<String> strings = new ArrayList<>();
String line;
try {
while ((line = bufferedReader.readLine()) != null)
strings.add(line);
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
result.put(null, strings);
break;
}
return result;
}
private int countLines(ArrayList<String> lines) {
return lines.size();
}
private int countBytes(ArrayList<String> lines) { //support UTF-8 only
int count = 0;
for (String line : lines) {
try {
byte[] utf8Bytes = line.getBytes("UTF-8");
count += utf8Bytes.length;
} catch (UnsupportedEncodingException ex) {
System.err.println(ex.getMessage());
}
}
return count;
}
private int countSymbols(ArrayList<String> lines) {
int count = 0;
for (String line : lines) {
count += line.length();
}
return count;
}
private int getLongestLength(ArrayList<String> lines) {
int longest = -1;
for (String line : lines) {
if (line.length() > longest)
longest = line.length();
}
return longest;
}
private int countWords(ArrayList<String> lines) {
int count = 0;
for (String line : lines) {
count += line.split(" ").length;
}
return count;
}
}
public class Main {
public static void main(String[] args) {
CountMode countMode = CountMode.DEFAULT;
ReadMode readMode = ReadMode.CONSOLE;
ArrayList<File> files = new ArrayList<>();
if (args.length > 0) {
switch (args[0]) {
case "-l":
countMode = CountMode.LINES;
break;
case "-c":
countMode = CountMode.BYTES;
break;
case "-m":
countMode = CountMode.SYMBOLS;
break;
case "-L":
countMode = CountMode.LONGEST_LENGTH;
break;
case "-w":
countMode = CountMode.WORDS;
break;
}
}
for (String fileName : args) {
File file = new File(fileName);
if (file.exists() && !file.isDirectory()) {
files.add(file);
readMode = ReadMode.FILE;
}
}
Counter counter = new Counter(countMode, readMode, files);
counter.process();
}
}
wc
program, can you please provide a brief summary of what the program is supposed to do, expected inputs/outputs, etc? \$\endgroup\$man wc
gives you all the info (try google it) \$\endgroup\$