Merge branch 'master' into master
This commit is contained in:
@@ -4,3 +4,4 @@
|
||||
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
|
||||
|
||||
@@ -18,6 +18,18 @@
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -34,6 +46,7 @@
|
||||
|
||||
<properties>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-collections4.version>4.3</commons-collections4.version>
|
||||
<guava.version>28.0-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.checksortedlist;
|
||||
|
||||
public class Employee {
|
||||
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(long id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.algorithms.checksortedlist;
|
||||
|
||||
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.google.common.collect.Comparators;
|
||||
import com.google.common.collect.Ordering;;
|
||||
|
||||
public class SortedListChecker {
|
||||
|
||||
private SortedListChecker() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingIterativeApproach(List<String> listOfStrings) {
|
||||
if (isEmpty(listOfStrings) || listOfStrings.size() == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Iterator<String> iter = listOfStrings.iterator();
|
||||
String current, previous = iter.next();
|
||||
while (iter.hasNext()) {
|
||||
current = iter.next();
|
||||
if (previous.compareTo(current) > 0) {
|
||||
return false;
|
||||
}
|
||||
previous = current;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingIterativeApproach(List<Employee> employees, Comparator<Employee> employeeComparator) {
|
||||
if (isEmpty(employees) || employees.size() == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Iterator<Employee> iter = employees.iterator();
|
||||
Employee current, previous = iter.next();
|
||||
while (iter.hasNext()) {
|
||||
current = iter.next();
|
||||
if (employeeComparator.compare(previous, current) > 0) {
|
||||
return false;
|
||||
}
|
||||
previous = current;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingRecursion(List<String> listOfStrings) {
|
||||
return isSortedRecursive(listOfStrings, listOfStrings.size());
|
||||
}
|
||||
|
||||
public static boolean isSortedRecursive(List<String> listOfStrings, int index) {
|
||||
if (index < 2) {
|
||||
return true;
|
||||
} else if (listOfStrings.get(index - 2)
|
||||
.compareTo(listOfStrings.get(index - 1)) > 0) {
|
||||
return false;
|
||||
} else {
|
||||
return isSortedRecursive(listOfStrings, index - 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingOrderingClass(List<String> listOfStrings) {
|
||||
return Ordering.<String> natural()
|
||||
.isOrdered(listOfStrings);
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingOrderingClass(List<Employee> employees, Comparator<Employee> employeeComparator) {
|
||||
return Ordering.from(employeeComparator)
|
||||
.isOrdered(employees);
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingOrderingClassHandlingNull(List<String> listOfStrings) {
|
||||
return Ordering.<String> natural()
|
||||
.nullsLast()
|
||||
.isOrdered(listOfStrings);
|
||||
}
|
||||
|
||||
public static boolean checkIfSortedUsingComparators(List<String> listOfStrings) {
|
||||
return Comparators.isInOrder(listOfStrings, Comparator.<String> naturalOrder());
|
||||
}
|
||||
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package com.baeldung.algorithms.checksortedlist;
|
||||
|
||||
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingComparators;
|
||||
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingIterativeApproach;
|
||||
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingOrderingClass;
|
||||
import static com.baeldung.algorithms.checksortedlist.SortedListChecker.checkIfSortedUsingRecursion;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SortedListCheckerUnitTest {
|
||||
|
||||
private List<String> sortedListOfString;
|
||||
private List<String> unsortedListOfString;
|
||||
private List<String> singletonList;
|
||||
|
||||
private List<Employee> employeesSortedByName;
|
||||
private List<Employee> employeesNotSortedByName;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
sortedListOfString = asList("Canada", "HK", "LA", "NJ", "NY");
|
||||
unsortedListOfString = asList("LA", "HK", "NJ", "NY", "Canada");
|
||||
singletonList = Collections.singletonList("NY");
|
||||
|
||||
employeesSortedByName = asList(new Employee(1L, "John"), new Employee(2L, "Kevin"), new Employee(3L, "Mike"));
|
||||
employeesNotSortedByName = asList(new Employee(1L, "Kevin"), new Employee(2L, "John"), new Employee(3L, "Mike"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedList_whenUsingIterativeApproach_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingIterativeApproach(sortedListOfString)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleElementList_whenUsingIterativeApproach_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingIterativeApproach(singletonList)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenUsingIterativeApproach_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingIterativeApproach(unsortedListOfString)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedListOfEmployees_whenUsingIterativeApproach_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingIterativeApproach(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedListOfEmployees_whenUsingIterativeApproach_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingIterativeApproach(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedList_whenUsingRecursion_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingRecursion(sortedListOfString)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingleElementList_whenUsingRecursion_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingRecursion(singletonList)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenUsingRecursion_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingRecursion(unsortedListOfString)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedList_whenUsingGuavaOrdering_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingOrderingClass(sortedListOfString)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenUsingGuavaOrdering_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingOrderingClass(unsortedListOfString)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedListOfEmployees_whenUsingGuavaOrdering_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingOrderingClass(employeesSortedByName, Comparator.comparing(Employee::getName))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedListOfEmployees_whenUsingGuavaOrdering_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingOrderingClass(employeesNotSortedByName, Comparator.comparing(Employee::getName))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSortedList_whenUsingGuavaComparators_thenReturnTrue() {
|
||||
assertThat(checkIfSortedUsingComparators(sortedListOfString)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenUsingGuavaComparators_thenReturnFalse() {
|
||||
assertThat(checkIfSortedUsingComparators(unsortedListOfString)).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,4 +5,5 @@
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [Template Engines in Groovy](https://www.baeldung.com/groovy-template-engines)
|
||||
- [Groovy def Keyword](https://www.baeldung.com/groovy-def-keyword)
|
||||
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
|
||||
- [Pattern Matching in Strings in Groovy](https://www.baeldung.com/groovy-pattern-matching)
|
||||
- [Working with XML in Groovy](https://www.baeldung.com/groovy-xml)
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.breakloop;
|
||||
|
||||
public class LoopBreaking {
|
||||
|
||||
public String simpleBreak() {
|
||||
String result = "";
|
||||
for (int outerCounter = 0; outerCounter < 2; outerCounter++) {
|
||||
result += "outer" + outerCounter;
|
||||
for (int innerCounter = 0; innerCounter < 2; innerCounter++) {
|
||||
result += "inner" + innerCounter;
|
||||
if (innerCounter == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String labelBreak() {
|
||||
String result = "";
|
||||
myBreakLabel:
|
||||
for (int outerCounter = 0; outerCounter < 2; outerCounter++) {
|
||||
result += "outer" + outerCounter;
|
||||
for (int innerCounter = 0; innerCounter < 2; innerCounter++) {
|
||||
result += "inner" + innerCounter;
|
||||
if (innerCounter == 0) {
|
||||
break myBreakLabel;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String usingReturn() {
|
||||
String result = "";
|
||||
for (int outerCounter = 0; outerCounter < 2; outerCounter++) {
|
||||
result += "outer" + outerCounter;
|
||||
for (int innerCounter = 0; innerCounter < 2; innerCounter++) {
|
||||
result += "inner" + innerCounter;
|
||||
if (innerCounter == 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return "failed";
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.breakloop;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class LoopBreakingUnitTest {
|
||||
|
||||
private LoopBreaking loopBreaking = new LoopBreaking();
|
||||
|
||||
@Test
|
||||
void whenUsingBreak_shouldBreakInnerLoop() {
|
||||
assertEquals("outer0inner0outer1inner0", loopBreaking.simpleBreak());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingLabeledBreak_shouldBreakInnerLoopAndOuterLoop() {
|
||||
assertEquals("outer0inner0", loopBreaking.labelBreak());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingReturn_shouldBreakInnerLoopAndOuterLoop() {
|
||||
assertEquals("outer0inner0", loopBreaking.usingReturn());
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.DoubleStream;
|
||||
|
||||
public interface SecureRandomDemo {
|
||||
|
||||
public static void generateSecureRandomValues() {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
|
||||
int randomInt = sr.nextInt();
|
||||
long randomLong = sr.nextLong();
|
||||
float randomFloat = sr.nextFloat();
|
||||
double randomDouble = sr.nextDouble();
|
||||
boolean randomBoolean = sr.nextBoolean();
|
||||
|
||||
IntStream randomIntStream = sr.ints();
|
||||
LongStream randomLongStream = sr.longs();
|
||||
DoubleStream randomDoubleStream = sr.doubles();
|
||||
|
||||
byte[] values = new byte[124];
|
||||
sr.nextBytes(values);
|
||||
}
|
||||
|
||||
public static SecureRandom getSecureRandomForAlgorithm(String algorithm) throws NoSuchAlgorithmException {
|
||||
if (algorithm == null || algorithm.isEmpty()) {
|
||||
return new SecureRandom();
|
||||
}
|
||||
|
||||
return SecureRandom.getInstance(algorithm);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>easy-random</artifactId>
|
||||
<name>easy-random</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jeasy</groupId>
|
||||
<artifactId>easy-random-core</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.easy.random.model;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Department {
|
||||
private String depName;
|
||||
|
||||
public Department(String depName) {
|
||||
this.depName = depName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Department.class.getSimpleName() + "[", "]").add("depName='" + depName + "'")
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package org.baeldung.easy.random.model;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Department department;
|
||||
private Collection<Employee> coworkers;
|
||||
private Map<YearQuarter, Grade> quarterGrades;
|
||||
|
||||
public Employee(long id, String firstName, String lastName, Department department) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Employee employee = (Employee) o;
|
||||
return id == employee.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public Collection<Employee> getCoworkers() {
|
||||
return Collections.unmodifiableCollection(coworkers);
|
||||
}
|
||||
|
||||
public Map<YearQuarter, Grade> getQuarterGrades() {
|
||||
return Collections.unmodifiableMap(quarterGrades);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Employee.class.getSimpleName() + "[", "]").add("id=" + id)
|
||||
.add("firstName='" + firstName + "'")
|
||||
.add("lastName='" + lastName + "'")
|
||||
.add("department=" + department)
|
||||
.add("coworkers size=" + ((coworkers == null) ? 0 : coworkers.size()))
|
||||
.add("quarterGrades=" + quarterGrades)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.baeldung.easy.random.model;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Grade {
|
||||
|
||||
private int grade;
|
||||
|
||||
public Grade(int grade) {
|
||||
this.grade = grade;
|
||||
}
|
||||
|
||||
public int getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Grade.class.getSimpleName() + "[", "]").add("grade=" + grade)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.easy.random.model;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private Integer age;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]").add("firstName='" + firstName + "'")
|
||||
.add("lastName='" + lastName + "'")
|
||||
.add("age=" + age)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.baeldung.easy.random.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class YearQuarter {
|
||||
|
||||
private LocalDate startDate;
|
||||
private LocalDate endDate;
|
||||
|
||||
public YearQuarter(LocalDate startDate) {
|
||||
this.startDate = startDate;
|
||||
autoAdjustEndDate();
|
||||
}
|
||||
|
||||
private void autoAdjustEndDate() {
|
||||
endDate = startDate.plusMonths(3L);
|
||||
}
|
||||
|
||||
public LocalDate getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public LocalDate getEndDate() {
|
||||
return endDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
YearQuarter quarter = (YearQuarter) o;
|
||||
return Objects.equals(startDate, quarter.startDate) && Objects.equals(endDate, quarter.endDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(startDate, endDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", YearQuarter.class.getSimpleName() + "[", "]").add("startDate=" + startDate)
|
||||
.add("endDate=" + endDate)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.easy.random.randomizer;
|
||||
|
||||
import org.baeldung.easy.random.model.YearQuarter;
|
||||
import org.jeasy.random.api.Randomizer;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
|
||||
public class YearQuarterRandomizer implements Randomizer<YearQuarter> {
|
||||
|
||||
private LocalDate date = LocalDate.of(1990, Month.SEPTEMBER, 25);
|
||||
|
||||
@Override
|
||||
public YearQuarter getRandomValue() {
|
||||
date = date.plusMonths(3);
|
||||
return new YearQuarter(date);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package org.baeldung.easy.random;
|
||||
|
||||
import org.baeldung.easy.random.model.Employee;
|
||||
import org.baeldung.easy.random.model.Person;
|
||||
import org.baeldung.easy.random.model.YearQuarter;
|
||||
import org.baeldung.easy.random.randomizer.YearQuarterRandomizer;
|
||||
import org.jeasy.random.EasyRandom;
|
||||
import org.jeasy.random.EasyRandomParameters;
|
||||
import org.jeasy.random.FieldPredicates;
|
||||
import org.jeasy.random.TypePredicates;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EasyRandomUnitTest {
|
||||
|
||||
@Test
|
||||
void givenDefaultConfiguration_thenGenerateSingleObject() {
|
||||
EasyRandom generator = new EasyRandom();
|
||||
Person person = generator.nextObject(Person.class);
|
||||
|
||||
assertNotNull(person.getAge());
|
||||
assertNotNull(person.getFirstName());
|
||||
assertNotNull(person.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDefaultConfiguration_thenGenerateObjectsList() {
|
||||
EasyRandom generator = new EasyRandom();
|
||||
List<Person> persons = generator.objects(Person.class, 5)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(5, persons.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCustomConfiguration_thenGenerateSingleEmployee() {
|
||||
EasyRandomParameters parameters = new EasyRandomParameters();
|
||||
parameters.stringLengthRange(3, 3);
|
||||
parameters.collectionSizeRange(5, 5);
|
||||
parameters.excludeField(FieldPredicates.named("lastName").and(FieldPredicates.inClass(Employee.class)));
|
||||
parameters.excludeType(TypePredicates.inPackage("not.existing.pkg"));
|
||||
parameters.randomize(YearQuarter.class, new YearQuarterRandomizer());
|
||||
|
||||
EasyRandom generator = new EasyRandom(parameters);
|
||||
Employee employee = generator.nextObject(Employee.class);
|
||||
|
||||
assertEquals(3, employee.getFirstName().length());
|
||||
assertEquals(5, employee.getCoworkers().size());
|
||||
assertEquals(5, employee.getQuarterGrades().size());
|
||||
assertNotNull(employee.getDepartment());
|
||||
|
||||
assertNull(employee.getLastName());
|
||||
|
||||
for (YearQuarter key : employee.getQuarterGrades().keySet()) {
|
||||
assertEquals(key.getStartDate(), key.getEndDate().minusMonths(3L));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.convertiteratortolist;
|
||||
package com.baeldung.convert.iteratortolist;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||
@@ -20,6 +20,13 @@
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
public interface DateValidator {
|
||||
boolean isValid(String dateStr);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import org.apache.commons.validator.GenericValidator;
|
||||
|
||||
public class DateValidatorUsingApacheValidator implements DateValidator {
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
return GenericValidator.isDate(dateStr, "yyyy-MM-dd", true);
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateValidatorUsingDateFormat implements DateValidator {
|
||||
private String dateFormat;
|
||||
|
||||
public DateValidatorUsingDateFormat(String dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
DateFormat sdf = new SimpleDateFormat(this.dateFormat);
|
||||
sdf.setLenient(false);
|
||||
try {
|
||||
sdf.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
public class DateValidatorUsingDateTimeFormatter implements DateValidator {
|
||||
private DateTimeFormatter dateFormatter;
|
||||
|
||||
public DateValidatorUsingDateTimeFormatter(DateTimeFormatter dateFormatter) {
|
||||
this.dateFormatter = dateFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
try {
|
||||
this.dateFormatter.parse(dateStr);
|
||||
} catch (DateTimeParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
public class DateValidatorUsingLocalDate implements DateValidator {
|
||||
private DateTimeFormatter dateFormatter;
|
||||
|
||||
public DateValidatorUsingLocalDate(DateTimeFormatter dateFormatter) {
|
||||
this.dateFormatter = dateFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
try {
|
||||
LocalDate.parse(dateStr, this.dateFormatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.validator.GenericValidator;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingApacheValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenValidDatePassed_ThenTrue() {
|
||||
assertTrue(GenericValidator.isDate("2019-02-28", "yyyy-MM-dd", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidDatePassed_ThenFalse() {
|
||||
assertFalse(GenericValidator.isDate("2019-02-29", "yyyy-MM-dd", true));
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingDateFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
|
||||
|
||||
assertTrue(validator.isValid("02/28/2019"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInvalidDatePassed_ThenFalse() {
|
||||
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
|
||||
|
||||
assertFalse(validator.isValid("02/30/2019"));
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.ResolverStyle;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingDateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
|
||||
.withResolverStyle(ResolverStyle.STRICT);
|
||||
DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
|
||||
|
||||
assertTrue(validator.isValid("2019-02-28"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInValidDatePassed_ThenFalse() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
|
||||
.withResolverStyle(ResolverStyle.STRICT);
|
||||
DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
|
||||
|
||||
assertFalse(validator.isValid("2019-02-30"));
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingLocalDateUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
|
||||
DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
|
||||
|
||||
assertTrue(validator.isValid("20190228"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInValidDatePassed_ThenFalse() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
|
||||
DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
|
||||
|
||||
assertFalse(validator.isValid("20190230"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.random;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.DoubleStream;
|
||||
|
||||
public interface SecureRandomDemo {
|
||||
|
||||
public static void generateSecureRandomValues() {
|
||||
SecureRandom sr = new SecureRandom();
|
||||
|
||||
int randomInt = sr.nextInt();
|
||||
long randomLong = sr.nextLong();
|
||||
float randomFloat = sr.nextFloat();
|
||||
double randomDouble = sr.nextDouble();
|
||||
boolean randomBoolean = sr.nextBoolean();
|
||||
|
||||
IntStream randomIntStream = sr.ints();
|
||||
LongStream randomLongStream = sr.longs();
|
||||
DoubleStream randomDoubleStream = sr.doubles();
|
||||
|
||||
byte[] values = new byte[124];
|
||||
sr.nextBytes(values);
|
||||
}
|
||||
|
||||
public static SecureRandom getSecureRandomForAlgorithm(String algorithm) throws NoSuchAlgorithmException {
|
||||
if (algorithm == null || algorithm.isEmpty()) {
|
||||
return new SecureRandom();
|
||||
}
|
||||
|
||||
return SecureRandom.getInstance(algorithm);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user