diff --git a/src/main/java/by/andd3dfx/common/CountStreamers.java b/src/main/java/by/andd3dfx/common/CountStreamers.java index 7098f87..105db7d 100644 --- a/src/main/java/by/andd3dfx/common/CountStreamers.java +++ b/src/main/java/by/andd3dfx/common/CountStreamers.java @@ -1,11 +1,14 @@ package by.andd3dfx.common; import lombok.AllArgsConstructor; +import lombok.Data; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.List; + +import static by.andd3dfx.common.CountStreamers.EventType.START; +import static by.andd3dfx.common.CountStreamers.EventType.STOP; /** *
@@ -24,17 +27,17 @@ public class CountStreamers { public static int count(int[][] times) { - Listevents = new ArrayList<>(); - for (int[] time : times) { - events.add(new EventItem(time[0], EventType.START)); - events.add(new EventItem(time[1], EventType.STOP)); + var events = new ArrayList (); + for (var time : times) { + events.add(new EventItem(time[0], START)); + events.add(new EventItem(time[1], STOP)); } - Collections.sort(events, Comparator.comparingInt(eventItem -> eventItem.time)); + Collections.sort(events, Comparator.comparingInt(EventItem::getTime)); - int current = 0; - int max = 0; - for (EventItem event : events) { + var current = 0; + var max = 0; + for (var event : events) { current += (event.eventType == EventType.START) ? +1 : -1; if (current > max) { max = current; @@ -47,9 +50,10 @@ public enum EventType { START, STOP } + @Data @AllArgsConstructor public static class EventItem { - int time; - EventType eventType; + private int time; + private EventType eventType; } } diff --git a/src/test/java/by/andd3dfx/common/CountStreamersTest.java b/src/test/java/by/andd3dfx/common/CountStreamersTest.java index 38edcb6..34e3538 100644 --- a/src/test/java/by/andd3dfx/common/CountStreamersTest.java +++ b/src/test/java/by/andd3dfx/common/CountStreamersTest.java @@ -2,6 +2,7 @@ import org.junit.Test; +import static by.andd3dfx.common.CountStreamers.count; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -14,7 +15,7 @@ public void countWithoutOverlapping() { {40, 50} }; - assertThat(CountStreamers.count(times), is(1)); + assertThat(count(times), is(1)); } @Test @@ -24,7 +25,7 @@ public void countWithOverlapping() { {20, 50} }; - assertThat(CountStreamers.count(times), is(2)); + assertThat(count(times), is(2)); } @Test @@ -39,6 +40,6 @@ public void countComplexCase() { {10, 120} }; - assertThat(CountStreamers.count(times), is(4)); + assertThat(count(times), is(4)); } }