Created
January 12, 2011 17:24
-
-
Save otaviomacedo/776502 to your computer and use it in GitHub Desktop.
Solution to Kata 13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent!! Neat and Clean solution