[BAEL-8232] - Removed unwanted dependencies and moved CountCharsExampleUnitTest from core-java to java-strings module
This commit is contained in:
@@ -15,11 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
@@ -40,12 +35,6 @@
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
@@ -53,57 +42,11 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-bytecode</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.codepoetics</groupId>
|
||||
<artifactId>protonpack</artifactId>
|
||||
<version>${protonpack.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>${streamex.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${asspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ibm.icu</groupId>
|
||||
<artifactId>icu4j</artifactId>
|
||||
@@ -154,20 +97,10 @@
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<protonpack.version>1.13</protonpack.version>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
<joda.version>2.10</joda.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<asspectj.version>1.8.9</asspectj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<jmh-core.version>1.19</jmh-core.version>
|
||||
<jmh-generator.version>1.19</jmh-generator.version>
|
||||
<icu4j.version>61.1</icu4j.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.java.countingChars;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
|
||||
|
||||
/***
|
||||
* Example of counting chars in a String.
|
||||
*/
|
||||
public class CountCharsExampleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingLoop_thenCountChars() {
|
||||
String someString = "elephant";
|
||||
char someChar = 'e';
|
||||
int count = 0;
|
||||
for (int i = 0; i < someString.length(); i++) {
|
||||
if (someString.charAt(i) == someChar) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingReplace_thenCountChars() {
|
||||
String someString = "elephant";
|
||||
int count = someString.length() - someString.replace("e", "").length();
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingSplit_thenCountChars() {
|
||||
String someString = "elephant";
|
||||
int count = someString.split("e", -1).length - 1;
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingReqExp_thenCountChars() {
|
||||
Pattern pattern = Pattern.compile("[^e]*e");
|
||||
Matcher matcher = pattern.matcher("elephant");
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingRecursion_thenCountChars() {
|
||||
int count = useRecursion("elephant", 'e', 0);
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
private int useRecursion(String someString, char searchedChar, int index) {
|
||||
if (index >= someString.length()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count = someString.charAt(index) == searchedChar ? 1 : 0;
|
||||
return count + useRecursion(someString, searchedChar, index + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingStringUtils_thenCountChars() throws InterruptedException {
|
||||
int count = StringUtils.countMatches("elephant", "e");
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingJava8Features_thenCountChars() {
|
||||
String someString = "elephant";
|
||||
long count = someString.chars().filter(ch -> ch == 'e').count();
|
||||
assertEquals(2, count);
|
||||
|
||||
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
|
||||
assertEquals(2, count2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenUsingGuavaCharMatcher_thenCountChars() {
|
||||
int count = CharMatcher.is('e').countIn("elephant");
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user