Last active
July 10, 2016 06:24
-
-
Save disc99/f2a708d932796a271de453db0a3d756f 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 example; | |
import lombok.Value; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.Validator; | |
import javax.validation.constraints.AssertTrue; | |
import java.time.LocalDateTime; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import static example.P0.*; | |
import static example.P1.*; | |
import static example.P2.*; | |
import static example.P3.*; | |
import static example.P4.*; | |
public class DateTimeSample { | |
List<It0> its0; | |
List<It1> its1; | |
List<It2> its2; | |
Its3 its3; | |
Its4 its4; | |
Validator validator; | |
void non_method(It0 in) { | |
boolean invalid = its0.stream() | |
.anyMatch(it -> it.getStart().getValue().isAfter(in.getStart().getValue()) | |
|| it.getEnd().getValue().isBefore(in.getEnd().getValue())); | |
} | |
void use_wrapper_method(It1 in) { | |
boolean invalid = its1.stream() | |
.anyMatch(it -> it.getStart().isAfter(in.getStart()) || it.getEnd().isBefore(in.getEnd())); | |
} | |
void use_period_class(It2 in) { | |
boolean invalid = its2.stream() | |
.anyMatch(it -> it.getPeriod().isOverlap(in.getPeriod())); | |
} | |
void use_first_class_collection(It3 in) { | |
boolean invalid = its3.isOverlapPeriod(in); | |
} | |
void use_bean_validation(It4 in) { | |
Its4 its = its4.add(in); | |
Set<ConstraintViolation<Its4>> invalid = validator.validate(its); | |
} | |
} | |
// Non method | |
class P0 { | |
@Value | |
static class It0 { | |
long id; | |
ItDate1 start; | |
ItDate1 end; | |
} | |
@Value | |
static class ItDate0 { | |
LocalDateTime value; | |
} | |
} | |
// Wrapper method | |
class P1 { | |
@Value | |
static class It1 { | |
long id; | |
ItDate1 start; | |
ItDate1 end; | |
} | |
@Value | |
static class ItDate1 { | |
LocalDateTime value; | |
boolean isAfter(ItDate1 date) { | |
return value.isAfter(date.getValue()); | |
} | |
boolean isBefore(ItDate1 date) { | |
return value.isBefore(date.getValue()); | |
} | |
} | |
} | |
// Use period | |
class P2 { | |
@Value | |
static class It2 { | |
long id; | |
ItPeriod2 period; | |
} | |
@Value | |
static class ItPeriod2 { | |
ItDate2 start; | |
ItDate2 end; | |
boolean isOverlap(ItPeriod2 period) { | |
return period.getStart().isAfter(start) || period.getEnd().isBefore(end); | |
} | |
} | |
@Value | |
static class ItDate2 { | |
LocalDateTime value; | |
boolean isAfter(ItDate2 date) { | |
return value.isAfter(date.getValue()); | |
} | |
boolean isBefore(ItDate2 date) { | |
return value.isBefore(date.getValue()); | |
} | |
} | |
} | |
// Use first class collection | |
class P3 { | |
@Value | |
static class Its3 { | |
List<It3> its; | |
boolean isOverlapPeriod(It3 it3) { | |
return its.stream().anyMatch(it -> it.getPeriod().isOverlap(it3.getPeriod())); | |
} | |
} | |
@Value | |
static class It3 { | |
long id; | |
ItPeriod3 period; | |
} | |
@Value | |
static class ItPeriod3 { | |
ItDate3 start; | |
ItDate3 end; | |
boolean isOverlap(ItPeriod3 period) { | |
return period.getStart().isAfter(start) || period.getEnd().isBefore(end); | |
} | |
} | |
@Value | |
static class ItDate3 { | |
LocalDateTime value; | |
boolean isAfter(ItDate3 date) { | |
return value.isAfter(date.getValue()); | |
} | |
boolean isBefore(ItDate3 date) { | |
return value.isBefore(date.getValue()); | |
} | |
} | |
} | |
// Use Bean Validation | |
class P4 { | |
@Value | |
static class Its4 { | |
List<It4> its; | |
Its4 add(It4 it) { | |
List<It4> newIts = new ArrayList<>(its); | |
newIts.add(it); | |
return new Its4(newIts); | |
} | |
@AssertTrue | |
boolean isNotOverlaped() { | |
for (int i = 0; i < its.size(); i++) { | |
for (int j = i + 1; j < its.size(); j++) { | |
if (its.get(i).getPeriod().isOverlap(its.get(j).getPeriod())) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
} | |
@Value | |
static class It4 { | |
long id; | |
ItPeriod4 period; | |
} | |
@Value | |
static class ItPeriod4 { | |
ItDate3 start; | |
ItDate3 end; | |
boolean isOverlap(ItPeriod4 period) { | |
return period.getStart().isAfter(start) || period.getEnd().isBefore(end); | |
} | |
} | |
@Value | |
static class ItDate4 { | |
LocalDateTime value; | |
boolean isAfter(ItDate4 date) { | |
return value.isAfter(date.getValue()); | |
} | |
boolean isBefore(ItDate4 date) { | |
return value.isBefore(date.getValue()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment