Created
March 14, 2019 09:30
-
-
Save circlee/a6f963c77ce6478b8021eb2da6722570 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
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
public class LocalDateTimeRange<T> { | |
List<LocalDateTime> localDateTimes; | |
private List<RangeWrap<T>> rangeWrapList; | |
public LocalDateTimeRange() { | |
localDateTimes = new ArrayList<>(); | |
rangeWrapList = new ArrayList<>(); | |
} | |
public void add(LocalDateTime startedAt, LocalDateTime endedAt, T wrapObj) { | |
rangeWrapList.add(new RangeWrap<T>(startedAt, endedAt, wrapObj)); | |
localDateTimes.add(startedAt.plusSeconds(0)); | |
localDateTimes.add(startedAt.minusSeconds(1)); | |
localDateTimes.add(endedAt.plusSeconds(0)); | |
localDateTimes.add(endedAt.plusSeconds(1)); | |
} | |
public Predicate<LocalDateTime> distinctByKey( Function<LocalDateTime, String> keyExtractor) { | |
Map<Object, Boolean> map = new ConcurrentHashMap<>(); | |
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; | |
} | |
public void show() { | |
List<LocalDateTime> timeList = localDateTimes.stream() | |
.filter(distinctByKey((Function<LocalDateTime, String>)(time) -> time.toString())) | |
.sorted() | |
.collect(Collectors.toList()); | |
; | |
timeList.stream() | |
.forEach(System.out::println); | |
System.out.println("---"); | |
List<LocalDateTime> candidateList = timeList.stream() | |
.filter( dateTime -> { | |
return rangeWrapList.stream().filter( rw -> { | |
if(dateTime.compareTo(rw.startedAt) >= 0 && dateTime.compareTo(rw.endedAt) <= 0) { | |
return true; | |
} | |
return false; | |
}) | |
.findFirst() | |
.isPresent(); | |
}) | |
.collect(Collectors.toList()); | |
List<RangeWrapResult> list = new ArrayList<>(); | |
for (int i = 0; i < candidateList.size(); i += 2) { | |
RangeWrapResult rr = new RangeWrapResult(candidateList.get(i), candidateList.get(i+1), new ArrayList<>()); | |
list.add(rr); | |
} | |
list.stream() | |
.forEach( r -> { | |
List<T> wrapList = rangeWrapList.stream().filter( rw -> { | |
if(r.startedAt.compareTo(rw.startedAt) >= 0 && r.startedAt.compareTo(rw.endedAt) <= 0) { | |
return true; | |
} | |
if(r.endedAt.compareTo(rw.startedAt) >= 0 && r.endedAt.compareTo(rw.endedAt) <= 0) { | |
return true; | |
} | |
return false; | |
}) | |
.map(rw -> rw.wrapObj) | |
.collect(Collectors.toList()); | |
r.wrapObjList = wrapList; | |
System.out.println(r.startedAt + " ~ " + r.endedAt + " " + r.wrapObjList); | |
}); | |
} | |
class RangeWrap<T> { | |
private LocalDateTime startedAt; | |
private LocalDateTime endedAt; | |
private T wrapObj; | |
public RangeWrap(LocalDateTime startedAt, LocalDateTime endedAt, T wrapObj) { | |
this.startedAt = startedAt; | |
this.endedAt = endedAt; | |
this.wrapObj = wrapObj; | |
} | |
} | |
class RangeWrapResult { | |
private LocalDateTime startedAt; | |
private LocalDateTime endedAt; | |
private List<T> wrapObjList; | |
public RangeWrapResult(LocalDateTime startedAt, LocalDateTime endedAt, List<T> wrapObjList) { | |
this.startedAt = startedAt; | |
this.endedAt = endedAt; | |
this.wrapObjList = wrapObjList; | |
} | |
} | |
public static void main(String... args) { | |
LocalDateTime now = LocalDateTime.now(); | |
LocalDateTime as = LocalDateTime.parse("2019-03-01 15:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(as); | |
LocalDateTime ae = LocalDateTime.parse("2019-03-02 14:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(ae); | |
LocalDateTime bs = LocalDateTime.parse("2019-03-02 15:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(bs); | |
LocalDateTime be = LocalDateTime.parse("2019-03-07 14:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(be); | |
LocalDateTime cs = LocalDateTime.parse("2019-03-05 15:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(cs); | |
LocalDateTime ce = LocalDateTime.parse("2019-03-07 14:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); | |
System.out.println(ce); | |
LocalDateTimeRange<String> a = new LocalDateTimeRange<>(); | |
a.add(as, ae, "1"); | |
a.add(bs, be, "2"); | |
a.add(cs, ce, "3"); | |
System.out.println(); | |
System.out.println(); | |
a.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment