Skip to content

Commit

Permalink
Adjust CountStreamers a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Dec 7, 2023
1 parent e47d400 commit 394e40c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
26 changes: 15 additions & 11 deletions src/main/java/by/andd3dfx/common/CountStreamers.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <pre>
Expand All @@ -24,17 +27,17 @@
public class CountStreamers {

public static int count(int[][] times) {
List<EventItem> events = 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<EventItem>();
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;
Expand All @@ -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;
}
}
7 changes: 4 additions & 3 deletions src/test/java/by/andd3dfx/common/CountStreamersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -14,7 +15,7 @@ public void countWithoutOverlapping() {
{40, 50}
};

assertThat(CountStreamers.count(times), is(1));
assertThat(count(times), is(1));
}

@Test
Expand All @@ -24,7 +25,7 @@ public void countWithOverlapping() {
{20, 50}
};

assertThat(CountStreamers.count(times), is(2));
assertThat(count(times), is(2));
}

@Test
Expand All @@ -39,6 +40,6 @@ public void countComplexCase() {
{10, 120}
};

assertThat(CountStreamers.count(times), is(4));
assertThat(count(times), is(4));
}
}

0 comments on commit 394e40c

Please sign in to comment.