Merge pull request #4 from eugenp/master
Pulled from eugenp:master to sync my fork
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
*/bin/*
|
||||
|
||||
*.class
|
||||
|
||||
# Package Files #
|
||||
|
||||
+5
-7
@@ -1,6 +1,9 @@
|
||||
language: java
|
||||
|
||||
install: travis_wait 40 mvn -q clean install -Dgib.enabled=true
|
||||
install: travis_wait 60 mvn -q test
|
||||
|
||||
before_script:
|
||||
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -XX:-UseGCOverheadLimit'" > ~/.mavenrc
|
||||
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
@@ -15,9 +18,4 @@ cache:
|
||||
- .autoconf
|
||||
- $HOME/.m2
|
||||
|
||||
sudo: required
|
||||
|
||||
env:
|
||||
global:
|
||||
JAVA_OPTS="-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
|
||||
MAVEN_OPTS="-Xmx2048M -Xss128M -XX:MaxPermSize=2048M -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
|
||||
|
||||
|
||||
@@ -10,9 +10,15 @@
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class BigIntegerPrimeChecker implements PrimeChecker{
|
||||
|
||||
@Override
|
||||
public boolean isPrime(int number) {
|
||||
BigInteger bigInt = BigInteger.valueOf(number);
|
||||
return bigInt.isProbablePrime(100);
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BruteForcePrimeChecker implements PrimeChecker{
|
||||
|
||||
@Override
|
||||
public boolean isPrime(int number) {
|
||||
return IntStream.range(2, number).noneMatch(n -> (number % n == 0));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class OptimisedPrimeChecker implements PrimeChecker{
|
||||
|
||||
@Override
|
||||
public boolean isPrime(int number) {
|
||||
return IntStream.range(2, (int)Math.sqrt(number) + 1)
|
||||
.noneMatch(n -> (number % n == 0));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
public interface PrimeChecker {
|
||||
|
||||
public boolean isPrime( int number );
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import org.apache.commons.math3.primes.Primes;
|
||||
|
||||
public class PrimesPrimeChecker implements PrimeChecker{
|
||||
|
||||
@Override
|
||||
public boolean isPrime(int number) {
|
||||
return Primes.isPrime(number);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,12 +21,12 @@ public final class RtState implements State {
|
||||
}
|
||||
|
||||
public State transit(final CharSequence c) {
|
||||
for(final Transition t : this.transitions) {
|
||||
if(t.isPossible(c)) {
|
||||
return t.state();
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Input not accepted: " + c);
|
||||
return transitions
|
||||
.stream()
|
||||
.filter(t -> t.isPossible(c))
|
||||
.map(Transition::state)
|
||||
.findAny()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Input not accepted: " + c));
|
||||
}
|
||||
|
||||
public boolean isFinal() {
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.primechecker.BigIntegerPrimeChecker;
|
||||
import com.baeldung.algorithms.primechecker.PrimeChecker;
|
||||
|
||||
public class BigIntegerPrimeCheckerTest {
|
||||
|
||||
PrimeChecker primeChecker = new BigIntegerPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
|
||||
assertTrue(primeChecker.isPrime(13));
|
||||
assertTrue(primeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){
|
||||
assertTrue(!primeChecker.isPrime(50));
|
||||
assertTrue(!primeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.primechecker.BruteForcePrimeChecker;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class BruteForcePrimeCheckerTest {
|
||||
|
||||
BruteForcePrimeChecker primeChecker = new BruteForcePrimeChecker();
|
||||
|
||||
@Test
|
||||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
|
||||
assertTrue(primeChecker.isPrime(13));
|
||||
assertTrue(primeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){
|
||||
assertTrue(!primeChecker.isPrime(50));
|
||||
assertTrue(!primeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.primechecker.OptimisedPrimeChecker;
|
||||
import com.baeldung.algorithms.primechecker.PrimeChecker;
|
||||
|
||||
public class OptimisedPrimeCheckerTest {
|
||||
|
||||
PrimeChecker primeChecker = new OptimisedPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue(){
|
||||
assertTrue(primeChecker.isPrime(13));
|
||||
assertTrue(primeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse(){
|
||||
assertTrue(!primeChecker.isPrime(50));
|
||||
assertTrue(!primeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.primechecker;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.primechecker.PrimeChecker;
|
||||
import com.baeldung.algorithms.primechecker.PrimesPrimeChecker;
|
||||
|
||||
public class PrimesPrimeCheckerTest {
|
||||
PrimeChecker primeChecker = new PrimesPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void givenPrimeNumber_whenCheckIsPrime_thenTrue() {
|
||||
assertTrue(primeChecker.isPrime(13));
|
||||
assertTrue(primeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonPrimeNumber_whenCheckIsPrime_thenFalse() {
|
||||
assertTrue(!primeChecker.isPrime(50));
|
||||
assertTrue(!primeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
*.docx
|
||||
temp.xls
|
||||
temp.xlsx
|
||||
|
||||
Binary file not shown.
@@ -84,4 +84,5 @@
|
||||
- [Guide to java.util.concurrent.Locks](http://www.baeldung.com/java-concurrent-locks)
|
||||
- [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions)
|
||||
- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency)
|
||||
- [Guide to Java 8 Comparator.comparing()](http://www.baeldung.com/java-8-comparator-comparing)
|
||||
|
||||
|
||||
+1
-1
@@ -198,7 +198,6 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
@@ -250,6 +249,7 @@
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.arraycopy.model;
|
||||
|
||||
public class Address implements Cloneable {
|
||||
private String country;
|
||||
private String state;
|
||||
private String city;
|
||||
private String street;
|
||||
private String zipcode;
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getZipcode() {
|
||||
return zipcode;
|
||||
}
|
||||
|
||||
public void setZipcode(String zipcode) {
|
||||
this.zipcode = zipcode;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object clone() throws CloneNotSupportedException {
|
||||
super.clone();
|
||||
Address address = new Address();
|
||||
address.setCity(this.city);
|
||||
address.setCountry(this.country);
|
||||
address.setState(this.state);
|
||||
address.setStreet(this.street);
|
||||
address.setZipcode(this.zipcode);
|
||||
return address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.arraycopy.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Employee implements Serializable {
|
||||
private static final long serialVersionUID = -2454619097207585825L;
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.pow;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class PowerExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int intResult = (int) Math.pow(2, 3);
|
||||
System.out.println("Math.pow(2, 3) = " + intResult);
|
||||
|
||||
double dblResult = Math.pow(4.2, 3);
|
||||
System.out.println("Math.pow(4.2, 3) = " + Math.pow(4.2, 3));
|
||||
|
||||
DecimalFormat df = new DecimalFormat(".00");
|
||||
System.out.println("Math.pow(4.2, 3) rounded = " + df.format(dblResult));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.baeldung.arraycopy;
|
||||
|
||||
import com.baeldung.arraycopy.model.Address;
|
||||
import com.baeldung.arraycopy.model.Employee;
|
||||
import org.apache.commons.lang3.SerializationUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ArrayCopyUtilTest {
|
||||
private static Employee[] employees;
|
||||
private static final int MAX = 2;
|
||||
|
||||
@BeforeClass
|
||||
public static void setup(){
|
||||
createEmployeesArray();
|
||||
}
|
||||
|
||||
private static void createEmployeesArray() {
|
||||
employees = new Employee[MAX];
|
||||
Employee employee;
|
||||
for(int i = 0; i < MAX; i++) {
|
||||
employee = new Employee();
|
||||
employee.setName("Emp"+i);
|
||||
employee.setId(i);
|
||||
employees[i] = employee;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaSystemsArrayCopy_thenSuccessful(){
|
||||
int[] array = {23, 43, 55};
|
||||
int[] copiedArray = new int[3];
|
||||
|
||||
System.arraycopy(array, 0, copiedArray, 0, 3);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaSystemsArrayCopy_thenSuccessful(){
|
||||
int[] array = {23, 43, 55, 12, 65, 88, 92};
|
||||
int[] copiedArray = new int[3];
|
||||
|
||||
System.arraycopy(array, 2, copiedArray, 0, 3);
|
||||
|
||||
Assert.assertTrue(3 == copiedArray.length);
|
||||
Assert.assertTrue(copiedArray[0] == array[2]);
|
||||
Assert.assertTrue(copiedArray[1] == array[3]);
|
||||
Assert.assertTrue(copiedArray[2] == array[4]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedSubSequenceViaArraysCopyOfRange_thenSuccessful(){
|
||||
int[] array = {23, 43, 55, 12, 65, 88, 92};
|
||||
|
||||
int[] copiedArray = Arrays.copyOfRange(array, 1, 4);
|
||||
|
||||
Assert.assertTrue(3 == copiedArray.length);
|
||||
Assert.assertTrue(copiedArray[0] == array[1]);
|
||||
Assert.assertTrue(copiedArray[1] == array[2]);
|
||||
Assert.assertTrue(copiedArray[2] == array[3]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaArraysCopyOf_thenValueChangeIsSuccessful(){
|
||||
int[] array = {23, 43, 55, 12};
|
||||
int newLength = array.length;
|
||||
|
||||
int[] copiedArray = Arrays.copyOf(array, newLength);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
array[0] = 9;
|
||||
Assert.assertTrue(copiedArray[0] != array[0]);
|
||||
copiedArray[1] = 12;
|
||||
Assert.assertTrue(copiedArray[1] != array[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfNonPrimitiveType_whenCopiedViaArraysCopyOf_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = Arrays.copyOf(employees, employees.length);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element caused change in the copied array
|
||||
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfPrimitiveType_whenCopiedViaArrayClone_thenValueChangeIsSuccessful(){
|
||||
int[] array = {23, 43, 55, 12};
|
||||
|
||||
int[] copiedArray = array.clone();
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, array);
|
||||
array[0] = 9;
|
||||
Assert.assertTrue(copiedArray[0] != array[0]);
|
||||
copiedArray[1] = 12;
|
||||
Assert.assertTrue(copiedArray[1] != array[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = employees.clone();
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);;
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element changed the copied array
|
||||
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfCloneableNonPrimitiveType_whenCopiedViaArrayClone_thenDoShallowCopy(){
|
||||
Address[] addresses = createAddressArray();
|
||||
|
||||
Address[] copiedArray = addresses.clone();
|
||||
|
||||
addresses[0].setCity(addresses[0].getCity()+"_Changed");
|
||||
Assert.assertArrayEquals(copiedArray, addresses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfSerializableNonPrimitiveType_whenCopiedViaSerializationUtils_thenDoDeepCopy(){
|
||||
Employee[] copiedArray = SerializationUtils.clone(employees);
|
||||
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element didn't change in the copied array
|
||||
Assert.assertFalse(
|
||||
copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfNonPrimitiveType_whenCopiedViaStream_thenDoShallowCopy(){
|
||||
Employee[] copiedArray = Arrays.stream(employees).toArray(Employee[]::new);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, employees);
|
||||
employees[0].setName(employees[0].getName()+"_Changed");
|
||||
//change in employees' element didn't change in the copied array
|
||||
Assert.assertTrue(copiedArray[0].getName().equals(employees[0].getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArraysOfPrimitiveType_whenCopiedViaStream_thenSuccessful(){
|
||||
String[] strArray = {"orange", "red", "green'"};
|
||||
|
||||
String[] copiedArray = Arrays.stream(strArray).toArray(String[]::new);
|
||||
|
||||
Assert.assertArrayEquals(copiedArray, strArray);
|
||||
}
|
||||
|
||||
private Address[] createAddressArray(){
|
||||
Address[] addresses = new Address[1];
|
||||
addresses[0] = createAddress();
|
||||
return addresses;
|
||||
}
|
||||
|
||||
private Address createAddress() {
|
||||
Address address = new Address();
|
||||
address.setCountry("USA");
|
||||
address.setState("CA");
|
||||
address.setCity("San Francisco");
|
||||
address.setStreet("Street 1");
|
||||
address.setZipcode("59999");
|
||||
return address;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@ import java.util.stream.Stream;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CountdownLatchExampleTest {
|
||||
public class CountdownLatchExampleIntegrationTest {
|
||||
@Test
|
||||
public void whenParallelProcessing_thenMainThreadWillBlockUntilCompletion() throws InterruptedException {
|
||||
// Given
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.enums;
|
||||
|
||||
import com.baeldung.enums.Pizza.PizzaStatusEnum;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -69,11 +70,31 @@ public class PizzaUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPizaOrder_whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
||||
public void whenDelivered_thenPizzaGetsDeliveredAndStatusChanges() {
|
||||
Pizza pz = new Pizza();
|
||||
pz.setStatus(Pizza.PizzaStatusEnum.READY);
|
||||
pz.deliver();
|
||||
assertTrue(pz.getStatus() == Pizza.PizzaStatusEnum.DELIVERED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {
|
||||
String pizzaEnumValue = "READY";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void whenConvertedIntoEnum_thenThrowsException() {
|
||||
String pizzaEnumValue = "rEAdY";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenInvalidEnumValueContentWiseAsString_whenConvertedIntoEnum_thenThrowsException() {
|
||||
String pizzaEnumValue = "invalid";
|
||||
PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingComparing_thenCheckingSort() {
|
||||
public void whenComparing_thenSortedByName() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
|
||||
Arrays.sort(employees, employeeNameComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -68,7 +68,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingComparingWithComparator_thenCheckingSort() {
|
||||
public void whenComparingWithComparator_thenSortedByNameDesc() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName, (s1, s2) -> {
|
||||
return s2.compareTo(s1);
|
||||
});
|
||||
@@ -76,9 +76,18 @@ public class Java8ComparatorTest {
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
assertTrue(Arrays.equals(employees, sortedEmployeesByNameDesc));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReversed_thenSortedByNameDesc() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
|
||||
Comparator<Employee> employeeNameComparatorReversed = employeeNameComparator.reversed();
|
||||
Arrays.sort(employees, employeeNameComparatorReversed);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
assertTrue(Arrays.equals(employees, sortedEmployeesByNameDesc));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingComparingInt_thenCheckingSort() {
|
||||
public void whenComparingInt_thenSortedByAge() {
|
||||
Comparator<Employee> employeeAgeComparator = Comparator.comparingInt(Employee::getAge);
|
||||
Arrays.sort(employees, employeeAgeComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -86,7 +95,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingComparingLong_thenCheckingSort() {
|
||||
public void whenComparingLong_thenSortedByMobile() {
|
||||
Comparator<Employee> employeeMobileComparator = Comparator.comparingLong(Employee::getMobile);
|
||||
Arrays.sort(employees, employeeMobileComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -94,7 +103,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingComparingDouble_thenCheckingSort() {
|
||||
public void whenComparingDouble_thenSortedBySalary() {
|
||||
Comparator<Employee> employeeSalaryComparator = Comparator.comparingDouble(Employee::getSalary);
|
||||
Arrays.sort(employees, employeeSalaryComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -102,7 +111,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingNaturalOrder_thenCheckingSort() {
|
||||
public void whenNaturalOrder_thenSortedByName() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.<Employee> naturalOrder();
|
||||
Arrays.sort(employees, employeeNameComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -110,7 +119,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingReverseOrder_thenCheckingSort() {
|
||||
public void whenReverseOrder_thenSortedByNameDesc() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.<Employee> reverseOrder();
|
||||
Arrays.sort(employees, employeeNameComparator);
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
@@ -118,7 +127,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingNullFirst_thenCheckingSort() {
|
||||
public void whenNullsFirst_thenSortedByNameWithNullsFirst() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
|
||||
Comparator<Employee> employeeNameComparator_nullFirst = Comparator.nullsFirst(employeeNameComparator);
|
||||
Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullFirst);
|
||||
@@ -127,7 +136,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingNullLast_thenCheckingSort() {
|
||||
public void whenNullsLast_thenSortedByNameWithNullsLast() {
|
||||
Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
|
||||
Comparator<Employee> employeeNameComparator_nullLast = Comparator.nullsLast(employeeNameComparator);
|
||||
Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullLast);
|
||||
@@ -136,7 +145,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingThenComparing_thenCheckingSort() {
|
||||
public void whenThenComparing_thenSortedByAgeName() {
|
||||
Comparator<Employee> employee_Age_Name_Comparator = Comparator.comparing(Employee::getAge).thenComparing(Employee::getName);
|
||||
|
||||
Arrays.sort(someMoreEmployees, employee_Age_Name_Comparator);
|
||||
@@ -145,7 +154,7 @@ public class Java8ComparatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeArray_whenUsingThenComparingInt_thenCheckingSort() {
|
||||
public void whenThenComparing_thenSortedByNameAge() {
|
||||
Comparator<Employee> employee_Name_Age_Comparator = Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge);
|
||||
|
||||
Arrays.sort(someMoreEmployees, employee_Name_Age_Comparator);
|
||||
@@ -153,40 +162,6 @@ public class Java8ComparatorTest {
|
||||
assertTrue(Arrays.equals(someMoreEmployees, sortedEmployeesByNameAge));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void printData() {
|
||||
// System.out.println("employees");
|
||||
// System.out.println(Arrays.toString(employees));
|
||||
//
|
||||
// System.out.println("employeesArrayWithNulls");
|
||||
// System.out.println(Arrays.toString(employeesArrayWithNulls));
|
||||
//
|
||||
// System.out.println("sortedEmployeesByName");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesByName));
|
||||
//
|
||||
// System.out.println("sortedEmployeesByNameDesc");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesByNameDesc));
|
||||
//
|
||||
// System.out.println("sortedEmployeesByAge");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesByAge));
|
||||
//
|
||||
// System.out.println("sortedEmployeesByMobile");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesByMobile));
|
||||
//
|
||||
// System.out.println("sortedEmployeesBySalary");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesBySalary));
|
||||
//
|
||||
// System.out.println("sortedEmployeesArray_WithNullsFirst");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesArray_WithNullsFirst));
|
||||
//
|
||||
// System.out.println("sortedEmployeesArray_WithNullsLast");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesArray_WithNullsLast));
|
||||
//
|
||||
// System.out.println("sortedEmployeesByNameAge");
|
||||
// System.out.println(Arrays.toString(sortedEmployeesByNameAge));
|
||||
//
|
||||
// System.out.println("someMoreEmployees");
|
||||
// System.out.println(Arrays.toString(someMoreEmployees));
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+18
-34
@@ -1,52 +1,39 @@
|
||||
package com.baeldung.list.flattennestedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.hamcrest.collection.IsIterableContainingInOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FlattenNestedListTest {
|
||||
List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four"));
|
||||
|
||||
@Test
|
||||
public void givenListOfListOfString_flattenNestedList1() {
|
||||
// given
|
||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||
public void givenString_flattenNestedList1() {
|
||||
List<String> ls = flattenListOfListsImperatively(lol);
|
||||
|
||||
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||
|
||||
// when
|
||||
List<String> ls = flattenListOfListsImperatively(list);
|
||||
|
||||
// then
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
//TODO content assertion
|
||||
assertTrue(ls.size() == 8);
|
||||
// assert content
|
||||
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfListOfString_flattenNestedList2() {
|
||||
// given
|
||||
List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three");
|
||||
List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three");
|
||||
List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three");
|
||||
public void givenString_flattenNestedList2() {
|
||||
List<String> ls = flattenListOfListsStream(lol);
|
||||
|
||||
List<List<String>> list = Arrays.asList(ls1, ls2, ls3);
|
||||
|
||||
// when
|
||||
List<String> ls = flattenListOfListsStream(list);
|
||||
|
||||
// then
|
||||
assertNotNull(ls);
|
||||
assertTrue(ls.size() == 9);
|
||||
//TODO content assertion
|
||||
assertTrue(ls.size() == 8);
|
||||
// assert content
|
||||
assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four"));
|
||||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) {
|
||||
@@ -56,9 +43,6 @@ public class FlattenNestedListTest {
|
||||
}
|
||||
|
||||
public <T> List<T> flattenListOfListsStream(List<List<T>> list) {
|
||||
return list.stream()
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
return list.stream().flatMap(Collection::stream).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.io.*;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class JavaProcessUnitTest {
|
||||
public class JavaProcessUnitIntegrationTest {
|
||||
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
|
||||
|
||||
private static class StreamGobbler implements Runnable {
|
||||
+38
-38
@@ -1,38 +1,38 @@
|
||||
package org.baeldung.java.streams;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadPoolInParallelStreamTest {
|
||||
|
||||
@Test
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
|
||||
long firstNum = 1;
|
||||
long lastNum = 1_000_000;
|
||||
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
|
||||
List<Long> aList = new ArrayList<>();
|
||||
Stream<Long> parallelStream = aList.parallelStream();
|
||||
|
||||
assertTrue(parallelStream.isParallel());
|
||||
}
|
||||
}
|
||||
package org.baeldung.java.streams;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadPoolInParallelStreamIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
|
||||
long firstNum = 1;
|
||||
long lastNum = 1_000_000;
|
||||
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
|
||||
List<Long> aList = new ArrayList<>();
|
||||
Stream<Long> parallelStream = aList.parallelStream();
|
||||
|
||||
assertTrue(parallelStream.isParallel());
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,7 @@
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<archiveBaseDirectory>${project.basedir}</archiveBaseDirectory>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user