Created
March 15, 2019 23:17
-
-
Save circlee/6876a17ee4603b48903615d58190e1c1 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(endedAt.plusSeconds(0)); | |
} | |
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()); | |
; | |
List<RangeWrapResult> rangeWrapResults = new ArrayList<>(); | |
for (int i = 0; i < timeList.size() -1 ; i ++) { | |
System.out.println(); | |
final LocalDateTime current = timeList.get(i); | |
final LocalDateTime next = timeList.get(i+1); | |
System.out.println("current : " + current); | |
System.out.println("next : " + next); | |
LocalDateTime checkedCurrent = current.plusSeconds(0); | |
LocalDateTime checkedNext = next.plusSeconds(0); | |
boolean currentIsSomeStart = rangeWrapList.stream().filter(rw -> { | |
if (current.compareTo(rw.startedAt) != 0) { | |
return true; | |
} | |
return false; | |
}).findFirst().isPresent(); | |
if(!currentIsSomeStart) { | |
checkedCurrent = current.plusSeconds(1); | |
} | |
boolean nextIsSomeEnd = rangeWrapList.stream().filter(rw -> { | |
if (current.compareTo(rw.endedAt) != 0) { | |
return true; | |
} | |
return false; | |
}).findFirst().isPresent(); | |
if(!nextIsSomeEnd) { | |
checkedNext = next.minusSeconds(1); | |
} | |
System.out.println("checkedCurrent : " + checkedCurrent); | |
System.out.println("checkedNext : " + checkedNext); | |
RangeWrapResult r = new RangeWrapResult(checkedCurrent, checkedNext , new ArrayList<>()); | |
List<T> wrapList = rangeWrapList.stream().filter(rw -> { | |
if (r.startedAt.compareTo(rw.startedAt) >= 0 && r.endedAt.compareTo(rw.endedAt) <= 0) { | |
return true; | |
} | |
return false; | |
}) | |
.map(rw -> rw.wrapObj) | |
.collect(Collectors.toList()); | |
r.wrapObjList = wrapList; | |
rangeWrapResults.add(r); | |
} | |
rangeWrapResults.stream() | |
.filter(r-> (r.wrapObjList != null && !r.wrapObjList.isEmpty())) | |
.forEach(r -> { | |
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-03 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