diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 9b1e3c2..ba6137d 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -1441,7 +1441,7 @@ public static String startCase(final String input) { // split into a word when we encounter a space, or an underscore, or a dash, or a switch from lower to upper case String[] words = words(input, "\\s|_|-|(?<=[a-z])(?=[A-Z])"); return Arrays.stream(words).filter(w -> !w.trim().isEmpty()) - .map(w -> upperFirst(w.toLowerCase())).collect(joining(" ")); + .map(w -> upperFirst(w.toLowerCase())).collect(joining(" ")); } public static String escapeRegExp(final String input) { @@ -1473,5 +1473,106 @@ private static boolean isNullOrEmpty(String input) { return input == null || input.isEmpty(); } + /** + * Replace multiple value + * + * @param input The Input String value + * @param select The values to replacement + * @param replace The value to replace with + * @return String value + */ + public static String replace(String input, String replace, String... select) { + validate(input, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + validate(replace, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + for (String s : select) + input = input.replaceAll(s, replace); + return input; + } + + /** + * Get Number of Word in String + * + * @param input The Input String value + * @return int value + */ + public static int wordCount(String input) { + int count = 0; + char ch[] = new char[input.length()]; + for (int i = 0; i < input.length(); i++) { + ch[i] = input.charAt(i); + if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0))) + count++; + } + return count; + + + } + + + /** + * Get Number of Specific Word in String + * + * @param input The Input String value + * @param search The Keyword String + * @return int count value + */ + public static int wordCount(String input, String search) { + int index = 0; + int count = 0; + while (true) { + index = input.indexOf(search, index + 1); + if (index == -1) + break; + count++; + } + return count; + } + + /** + * Get Number of Specific Word in String Ignore Case + * @param input The Input String value + * @param search The Keyword String + * @return int count value + */ + public static int wordCountIgnoreCase(String input, String search) { + return wordCount(input.toUpperCase(), search.toUpperCase()); + } + /** + * Get Number of sentence in String + * + * @param input The Input String value + * @return int value + */ + public static int sentenceCount(String input) { + String delimiters = "?!."; + int result = 0; + for (int i = 0; i < input.length(); i++) { + if (delimiters.indexOf(input.charAt(i)) != -1) { + result++; + } + } + return result; + } + + /** + * Get Title version of string + * + * @param input The Input String value + * @return String value + */ + public static String title(String input) { + char output[] = new char[input.length()]; + char ch[] = new char[input.length()]; + for (int i = 0; i < (input.length()-1); i++) { + ch[i] = input.charAt(i); + if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0))) + output[i] = Character.toUpperCase(ch[i]); + else + output[i] = ch[i]; + + } + return new String(output).trim(); + } + } diff --git a/src/main/java/strman/TemplateBulider.java b/src/main/java/strman/TemplateBulider.java new file mode 100644 index 0000000..4f6b260 --- /dev/null +++ b/src/main/java/strman/TemplateBulider.java @@ -0,0 +1,58 @@ +package strman; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TemplateBulider { + + private static final String REGEX = "<=(.*?)\\>"; + + private final String template; + private final Map properties; + + public TemplateBulider(String template) { + this.template = template; + this.properties = buildProperties(template); + } + + public String execute() { + String result = template; + for (Map.Entry entry : properties.entrySet()) { + if (null == entry.getValue()) + continue; + String key = "<=" + entry.getKey() + ">"; + result = result.replace(key, entry.getValue()); + } + return result; + } + + public void add(String key, String value) { + if (!properties.containsKey(key)) + throw new RuntimeException("No veriable definitation \"" + key + "\""); + properties.replace(key, value); + } + + public void add(String key, Number value) { + add(key, value.toString()); + } + + protected static Map buildProperties(String template) { + Map valueMap = new HashMap<>(); + Pattern patten = Pattern.compile(REGEX); + Matcher m = patten.matcher(template); + while (m.find()) { + String key = m.group().substring(2, m.group().length() - 1); + if (valueMap.containsKey(key)) + throw new RuntimeException("Duplicate value definition \"" + key + "\""); + valueMap.put(key, null); + } + return valueMap; + } + + @Override + public String toString() { + return template; + } +} diff --git a/src/test/java/strman/StrmanTests.java b/src/test/java/strman/StrmanTests.java index 8ab7ffe..30df049 100644 --- a/src/test/java/strman/StrmanTests.java +++ b/src/test/java/strman/StrmanTests.java @@ -1238,4 +1238,56 @@ public void escapeRegExp_shouldEscapeRegExp() throws Exception { assertThat(escapeRegExp("How much is (2+3)? 5"), equalTo("How much is \\(2\\+3\\)\\? 5")); assertThat(escapeRegExp("\\s|_|-|(?<=[a-z])(?=[A-Z])"), equalTo("\\\\s\\|_\\|\\-\\|\\(\\?<=\\[a\\-z\\]\\)\\(\\?=\\[A\\-Z\\]\\)")); } + + @Test + public void replace_shouldMultipleTargetValueReplacedToOne() throws Exception { + assertThat(replace("Hello, my name is john and I'm 7 year old", "Baru Baru", "my", "is", "I'm"), equalTo("Hello, Baru Baru name Baru Baru john and Baru Baru 7 year old")); + assertThat(replace("A dog is running and a fox is sleeing and other dogs is eating", "animal", "dog", "fox", "dog"), equalTo("A animal is running and a animal is sleeing and other animals is eating")); + assertThat(replace("USA enter world war II in 1941, Before them United State didn't enter", "American", "USA", "United State"), equalTo("American enter world war II in 1941, Before them American didn't enter")); + } + + @Test + public void wordCount_shouldReturnRightNumberOfWord() throws Exception{ + assertThat(wordCount("Don’t cry because it’s over, smile because it happened."), equalTo(9)); + assertThat(wordCount("No one can make you feel inferior without your consent."),equalTo(10)); + assertThat(wordCount("The only way of finding the limits of the possible is by going beyond them into the impossible.”"),equalTo(18)); + } + + @Test + public void spceificWordCount_shouldReturnRightNumberOfWord() throws Exception{ + assertThat(wordCount("Nory was a Catholic because her mother was a Catholic, and Nory’s mother was a Catholic because her father was a Catholic, and her father was a Catholic because his mother was a Catholic, or had been.","Catholic"), equalTo(6)); + assertThat(wordCountIgnoreCase("Nory was a catholic because her mother was a Catholic, and Nory’s mother was a catholic because her father was a Catholic, and her father was a Catholic because his mother was a catholic, or had been.","catholic"),equalTo(6)); + assertThat(wordCount("I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy.","happy"),equalTo(4)); + } + + @Test + public void templateBuilder_shouldBuldCorrectly() throws Exception{ + TemplateBulider template = new TemplateBulider("Hello, my name is <=name>, <=age> years old."); + template.add("name", "John"); + template.add("age", 24); + assertThat(template.execute(),equalTo("Hello, my name is John, 24 years old.")); + } + + @Test(expected = RuntimeException.class) + public void templateBuilder_shouldExceptionOnDuplicateValueDefinition(){ + new TemplateBulider("Hello, my name is <=name>, <=age> years old. I'm <=name>"); + } + + @Test(expected = RuntimeException.class) + public void templateBuilder_shouldExceptionNoValueDefinition(){ + TemplateBulider template = new TemplateBulider("Hello, my name is <=name>, <=age> years old."); + template.add("address", "Yangon"); + } + + @Test + public void sentenceCount_shouldReturnRightNumberofSentence() { + assertThat(sentenceCount("This's a car. Isn't it? I thougt it! I know it."), equalTo(4)); + assertThat(sentenceCount("Use short sentences to create punch and make a point. Use phrases and even words as sentences. Really. Do not use too many sentences -- about three or four is usually enough. Use a short sentence as a summary after a longer description."), equalTo(5)); + } + + @Test + public void title_shouldBeCapitalizeTheSentence(){ + assertThat(title("computer programming "),equalTo("Computer Programming")); + assertThat(title("software development life cycle "),equalTo("Software Development Life Cycle")); + } }