Skip to content

Instantly share code, notes, and snippets.

@otaviomacedo
Created January 12, 2011 17:24
Show Gist options
  • Save otaviomacedo/776502 to your computer and use it in GitHub Desktop.
Save otaviomacedo/776502 to your computer and use it in GitHub Desktop.
Solution to Kata 13
package kata13;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import static org.apache.commons.lang.StringUtils.isNotBlank;
public class LineCounter {
private static enum State {INSIDE_COMMENT, OUTSIDE_COMMENT}
public static int countLines(Reader r) throws IOException {
BufferedReader reader = new BufferedReader(r);
int count = 0;
String line;
State state = State.OUTSIDE_COMMENT;
while ((line = reader.readLine()) != null){
state = commentBeginning(line, state);
count += isCountable(line, state) ? 1 : 0;
state = commentEnding(line, state);
}
return count;
}
private static boolean isCountable(String line, State state) {
return !line.trim().startsWith("//") && isNotBlank(line) && state == State.OUTSIDE_COMMENT;
}
private static State commentBeginning(String line, State state) {
return line.trim().startsWith("/*") ? State.INSIDE_COMMENT : state;
}
private static State commentEnding(String line, State state) {
return line.trim().contains("*/") ? State.OUTSIDE_COMMENT : state;
}
}
@contactash
Copy link

Excellent!! Neat and Clean solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment