[BAEL-6708] Address review comments

This commit is contained in:
rajatgarg 2023-08-09 22:44:14 +05:30
parent 2337e29d0b
commit de16d8c39c
2 changed files with 8 additions and 14 deletions

View File

@ -9,7 +9,6 @@ import java.util.stream.Stream;
import com.google.common.collect.Sets;
public class CartesianProduct {
// Responsible for calculating Cartesian Product using iterative approach.
public List<List<Object>> getCartesianProductIterative(List<List<Object>> sets) {
List<List<Object>> result = new ArrayList<>();
if(sets == null || sets.isEmpty()) {
@ -31,14 +30,12 @@ public class CartesianProduct {
return result;
}
// Responsible for calculating Cartesian Product using recursive approach.
public List<List<Object>> getCartesianProductRecursive(List<List<Object>> sets) {
List<List<Object>> result = new ArrayList<>();
getCartesianProductRecursiveHelper(sets, 0, new ArrayList<>(), result);
return result;
}
// Helper method for recursive approach
private void getCartesianProductRecursiveHelper(List<List<Object>> sets, int index, List<Object> current, List<List<Object>> result) {
if(index == sets.size()) {
result.add(new ArrayList<>(current));
@ -52,12 +49,10 @@ public class CartesianProduct {
}
}
// Responsible for calculating Cartesian Product using streams.
public List<List<Object>> getCartesianProductUsingStreams(List<List<Object>> sets) {
return cartesianProduct(sets,0).collect(Collectors.toList());
}
// Helper method for streams approach.
public Stream<List<Object>> cartesianProduct(List<List<Object>> sets, int index) {
if(index == sets.size()) {
List<Object> emptyList = new ArrayList<>();
@ -71,7 +66,6 @@ public class CartesianProduct {
}));
}
// Responsible for calculating Cartesian Product using Guava library.
public List<List<Object>> getCartesianProductUsingGuava(List<Set<Object>> sets) {
Set<List<Object>> cartesianProduct = Sets.cartesianProduct(sets);
List<List<Object>> cartesianList = new ArrayList<>(cartesianProduct);

View File

@ -19,7 +19,7 @@ public class CartesianProductUnitTest {
);
@Test
public void shouldCalculateCartesianProductUsingStreams() {
public void whenUsingStreams_thenCalculateCartesianProduct() {
List<List<Object>> expected = Arrays.asList(
Arrays.asList(10, "John", 'I'),
Arrays.asList(10, "John", 'J'),
@ -30,13 +30,13 @@ public class CartesianProductUnitTest {
Arrays.asList(20, "Jack", 'I'),
Arrays.asList(20, "Jack", 'J')
);
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingStreams(sets);
assertEquals(expected, cartesianProduct);
}
@Test
public void shouldCalculateCartesianProductUsingRecursion() {
public void whenUsingRecursion_thenCalculateCartesianProduct() {
List<List<Object>> expected = Arrays.asList(
Arrays.asList(10, "John", 'I'),
Arrays.asList(10, "John", 'J'),
@ -47,13 +47,13 @@ public class CartesianProductUnitTest {
Arrays.asList(20, "Jack", 'I'),
Arrays.asList(20, "Jack", 'J')
);
List<List<Object>> cartesianProduct = cp.getCartesianProductRecursive(sets);
assertEquals(expected, cartesianProduct);
}
@Test
public void shouldCalculateCartesianProductUsingIterativeApproach() {
public void whenUsingIterativeApproach_thenCalculateCartesianProduct() {
List<List<Object>> expected = Arrays.asList(
Arrays.asList(20, "Jack", 'J'),
Arrays.asList(10, "Jack", 'J'),
@ -64,13 +64,13 @@ public class CartesianProductUnitTest {
Arrays.asList(20, "John", 'I'),
Arrays.asList(10, "John", 'I')
);
List<List<Object>> cartesianProduct = cp.getCartesianProductIterative(sets);
assertEquals(expected, cartesianProduct);
}
@Test
public void shouldCalculateCartesianProductUsingGuava() {
public void whenUsingGuava_thenCalculateCartesianProduct() {
List<Set<Object>> sets = new ArrayList<>();
sets.add(new HashSet<>(Arrays.asList(10, 20)));
sets.add(new HashSet<>(Arrays.asList("John", "Jack")));
@ -86,8 +86,8 @@ public class CartesianProductUnitTest {
Arrays.asList(10, "Jack", 'I'),
Arrays.asList(10, "Jack", 'J')
);
List<List<Object>> cartesianProduct = cp.getCartesianProductUsingGuava(sets);
assertEquals(expected, cartesianProduct);
}
}