Last active
October 24, 2019 17:55
-
-
Save FWidm/139b02fdbb6209e1041dc235eada6ed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.fwidm.streams; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
public class GroupByFun { | |
public static void main(String[] args) { | |
GroupByFun groupByFun = new GroupByFun(); | |
List<Event> events = groupByFun.init(); | |
List<String> group = groupByFun.group(events, GroupByFun::byYear); | |
System.out.println(group); | |
group = groupByFun.group(events, GroupByFun::byYearMonth); | |
System.out.println(group); | |
group = groupByFun.group(events, GroupByFun::byYearQuarter); | |
System.out.println(group); | |
} | |
private List<Event> init() { | |
LocalDateTime baseDate = LocalDateTime.of(2019, 1, 1, 0, 0, 0, 0); | |
List<Event> events = Arrays.asList( | |
new Event("0xBEEF", baseDate, 0L), | |
new Event("0xBEEF", baseDate.plusMonths(1), 1L), | |
new Event("0xBEEF", baseDate.plusMonths(1).plusDays(2), 3L), | |
new Event("0xBEEF", baseDate.plusMonths(1).plusDays(4), 4L), | |
new Event("0xBEEF", baseDate.plusMonths(1).plusDays(7), 5L), | |
new Event("0xBEEF", baseDate.plusMonths(2), 2L), | |
new Event("0xBEEF", baseDate.plusMonths(2).plusDays(4), 6L), | |
new Event("0xBEEF", baseDate.plusMonths(2).plusDays(24), 7L), | |
new Event("0xBEEF", baseDate.plusMonths(3).plusDays(12), 8L), | |
new Event("0xBEEF", baseDate.plusMonths(3).plusDays(20), 9L), | |
new Event("0xBEEF", baseDate.plusMonths(4).plusDays(4), 10L), | |
new Event("0xBEEF", baseDate.plusMonths(5), 13L), | |
new Event("0xBEEF", baseDate.plusMonths(6), 20L), | |
new Event("0xBEEF", baseDate.plusMonths(7), 23L), | |
new Event("0xBEEF", baseDate.plusMonths(8), 24L), | |
new Event("0xBEEF", baseDate.plusMonths(9), 36L), | |
new Event("0xBEEF", baseDate.plusMonths(10), 40L), | |
new Event("0xBEEF", baseDate.plusMonths(11), 42L), | |
new Event("0xBEEF", baseDate.plusMonths(12), 42L), | |
new Event("0xBEEF", baseDate.plusMonths(13), 45L), | |
new Event("0xBEEF", baseDate.plusMonths(13), 56L), | |
new Event("0xBEEF", baseDate.plusMonths(14), 60L), | |
new Event("0xBEEF", baseDate.plusMonths(15), 54L), | |
new Event("0xBEEF", baseDate.plusMonths(16), 100L)); | |
return events; | |
} | |
private static String byYear(Event e) { | |
return e.getDateTime().format(DateTimeFormatter.ofPattern("yyyy")); | |
} | |
private static String byYearMonth(Event e) { | |
return e.getDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM")); | |
} | |
private static String byYearQuarter(Event e) { | |
int year = e.getDateTime().getYear(); | |
switch (e.getDateTime().format(DateTimeFormatter.ofPattern("Q"))) { | |
case "1": | |
return String.format("%s-I", year); | |
case "2": | |
return String.format("%s-II", year); | |
case "3": | |
return String.format("%s-III", year); | |
default: | |
return String.format("%s-IV", year); | |
} | |
} | |
private List<String> group(List<Event> events, Function<Event, String> func) { | |
Map<String, Event> stringEventMap = events.stream() | |
.sorted(Comparator.comparing(Event::getDateTime)) | |
.collect(Collectors.groupingBy(func, | |
Collectors.collectingAndThen( | |
Collectors.toList(), | |
this::getLast))); | |
return stringEventMap.entrySet().stream() | |
.sorted(Comparator.comparing(Map.Entry::getKey)) | |
.map(e -> String.format("%s=%s", e.getKey(), e.getValue().value)) | |
.collect(Collectors.toList()); | |
} | |
private <T> T getLast(List<T> list) { | |
return list.get(list.size() - 1); | |
} | |
public class Event { | |
String parentUuid; | |
LocalDateTime dateTime; | |
Long value; | |
public Event(String parentUuid, LocalDateTime dateTime, Long value) { | |
this.parentUuid = parentUuid; | |
this.dateTime = dateTime; | |
this.value = value; | |
} | |
public String getParentUuid() { | |
return parentUuid; | |
} | |
public void setParentUuid(String parentUuid) { | |
this.parentUuid = parentUuid; | |
} | |
public LocalDateTime getDateTime() { | |
return dateTime; | |
} | |
public void setDateTime(LocalDateTime dateTime) { | |
this.dateTime = dateTime; | |
} | |
public Long getValue() { | |
return value; | |
} | |
public void setValue(Long value) { | |
this.value = value; | |
} | |
@Override | |
public String toString() { | |
return "Event{" + | |
"parentUuid='" + parentUuid + '\'' + | |
", dateTime=" + dateTime + | |
", value=" + value + | |
'}'; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[2019=42, 2020=100] | |
[2019-01=0, 2019-02=5, 2019-03=7, 2019-04=9, 2019-05=10, 2019-06=13, 2019-07=20, 2019-08=23, 2019-09=24, 2019-10=36, 2019-11=40, 2019-12=42, 2020-01=42, 2020-02=56, 2020-03=60, 2020-04=54, 2020-05=100] | |
[2019-I=7, 2019-II=13, 2019-III=24, 2019-IV=42, 2020-I=60, 2020-II=100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment