class StringUtils {

	public static int countWords(String str, boolean all) {
		if (str == null || str.length() == 0) {
			return 0;
		}
		String[] words;
		if (all) {
			words = str.split("\\s+"); // Splits at whitespaces.
		} else {
			words = str.split("[\\s\\*\\#\\_\\|]+");
		}

		int count = 0;
		for (String word : words) {
			if (word.length() > 0) {
				count++;
			}
		}

		return count;
	}

}

class StringUtilTests {

	@Test
	public void testCountWords() {

		assertThat(StringUtils.countWords("Hi", true), is(equalTo(1)));
		assertThat(StringUtils.countWords("Hi there", true), is(equalTo(2)));
		assertThat(StringUtils.countWords("Hi  there", true), is(equalTo(2)));
		assertThat(StringUtils.countWords("The quick brown fox jumps over the lazy dog", true), is(equalTo(9)));
		assertThat(StringUtils.countWords("The * quick # brown _ fox | jumps over the lazy dog", true), is(equalTo(13)));
		assertThat(StringUtils.countWords("The * quick # brown _ fox | jumps over the lazy dog", false), is(equalTo(9)));
	}

}