Skip to content

Commit 0b7df67

Browse files
author
William Armiros
committed
added recursive depth
1 parent a7c3e16 commit 0b7df67

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

validator/src/main/java/com/amazon/aoc/helpers/SortUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
public final class SortUtils {
8+
private static final int MAX_RESURSIVE_DEPTH = 10;
89

910
/**
1011
* Given a list of entities, which are X-Ray segments or subsegments, recursively sort each of
@@ -13,13 +14,18 @@ public final class SortUtils {
1314
* @param entities - list of X-Ray entities to sort recursively. Modified in place.
1415
*/
1516
public static void recursiveEntitySort(List<Entity> entities) {
16-
if (entities == null || entities.size() == 0) {
17+
recursiveEntitySort(entities, 0);
18+
}
19+
20+
private static void recursiveEntitySort(List<Entity> entities, int depth) {
21+
if (entities == null || entities.size() == 0 || depth >= MAX_RESURSIVE_DEPTH) {
1722
return;
1823
}
24+
int currDepth = depth + 1;
1925

2026
for (Entity entity : entities) {
21-
if (entity.getSubsegments() != null && entity.getSubsegments().size() > 1) {
22-
recursiveEntitySort(entity.getSubsegments());
27+
if (entity.getSubsegments() != null && !entity.getSubsegments().isEmpty()) {
28+
recursiveEntitySort(entity.getSubsegments(), currDepth);
2329
}
2430
}
2531

validator/src/main/java/com/amazon/aoc/models/xray/Entity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class Entity {
3737
private boolean inProgress;
3838
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
3939
private boolean inferred;
40+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
41+
private boolean stubbed;
4042

4143
private String namespace;
4244

validator/src/test/java/com/amazon/aoc/helpers/SortUtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ public void testNestedEntitySort() {
6868
assertThat(bottomEntities).containsSequence(generated.get(6), generated.get(7));
6969
}
7070

71+
@Test
72+
public void testInfiniteLoop() {
73+
Entity current = new Entity();
74+
List<Entity> entityList = new ArrayList<>();
75+
entityList.add(current);
76+
current.setSubsegments(entityList); // set up an infinite children loop
77+
78+
SortUtils.recursiveEntitySort(entityList);
79+
80+
// Not really testing anything, just making sure we don't infinite loop
81+
assertThat(entityList).hasSize(1);
82+
}
83+
7184
private List<Entity> generateEntities(int n) {
7285
List<Entity> ret = new ArrayList<>();
7386

0 commit comments

Comments
 (0)