BAEL-5558: Sorting By Date in Java (#12132)
* [BAEL-5438] Added Criteria Queries for Employee * [BAEL-5558] Sorting By Date in Java * BAEL-5558: Added and refactored tests name * [BAEL-5558] Refactored the code. * [BAEL-5558] Resolved PMD violation. Co-authored-by: Mayank Agarwal <mayankaggarwal@zeta.tech>
This commit is contained in:
parent
b3375a1c8a
commit
81b9558342
@ -14,4 +14,12 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,72 @@
|
||||
package com.baeldung.collections.sorting;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Employee implements Comparable<Employee>{
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private double salary;
|
||||
private Date joiningDate;
|
||||
|
||||
public Employee(String name, int age, double salary, Date joiningDate) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.salary = salary;
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
|
||||
public Date getJoiningDate() {
|
||||
return joiningDate;
|
||||
}
|
||||
|
||||
public void setJoiningDate(Date joiningDate) {
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return ((Employee) obj).getName()
|
||||
.equals(getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append("(")
|
||||
.append(getName()).append(",")
|
||||
.append(getAge())
|
||||
.append(",")
|
||||
.append(getSalary()).append(",").append(getJoiningDate())
|
||||
.append(")")
|
||||
.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Employee employee) {
|
||||
return getJoiningDate().compareTo(employee.getJoiningDate());
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
package com.baeldung.collections.sorting;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmployeeSortingByDateUnitTest {
|
||||
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateAsc = new ArrayList<>();
|
||||
private List<Employee> employeesSortedByDateDesc = new ArrayList<>();
|
||||
|
||||
@Before
|
||||
public void initVariables() {
|
||||
|
||||
Collections.addAll(employees,
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateDesc,
|
||||
new Employee("John", 23, 5000, new Date()),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateAsc,
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
||||
Collections.sort(employees);
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedList() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee o1, Employee o2) {
|
||||
return o1.getJoiningDate().compareTo(o2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListV1() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp1.getJoiningDate().compareTo(emp2.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
|
||||
Collections.sort(employees, Collections.reverseOrder());
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAsc() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() {
|
||||
|
||||
Collections.sort(employees,
|
||||
(emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate()));
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() {
|
||||
|
||||
Collections.sort(employees, (emp1, emp2) -> {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
});
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() {
|
||||
Collections.sort(employees,
|
||||
Comparator.comparing(Employee::getJoiningDate));
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
|
||||
}
|
@ -32,5 +32,4 @@ public class EmployeeCriteriaIntegrationTest {
|
||||
session.close();
|
||||
assertArrayEquals(expectedSortCritEmployeeList.toArray(), employeeCriteriaQueries.getAllEmployees().toArray());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user