use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.
the class TCKChronoLocalDateTime method test_badMinusTemporalUnitChrono.
@Test(dataProvider = "calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
LocalDate refDate = LocalDate.of(2013, 1, 1);
ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
for (Chronology[] clist : data_of_calendars()) {
Chronology chrono2 = clist[0];
ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
TemporalUnit adjuster = new FixedTemporalUnit(cdt2);
if (chrono != chrono2) {
try {
cdt.minus(1, adjuster);
Assert.fail("TemporalUnit.doPlus minus should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDateTime<?> result = cdt.minus(1, adjuster);
assertEquals(result, cdt2, "WithAdjuster failed to replace date");
}
}
}
use of java.time.temporal.TemporalUnit in project jdk8u_jdk by JetBrains.
the class TCKChronoLocalDate method test_badMinusTemporalUnitChrono.
@Test(dataProvider = "calendars")
public void test_badMinusTemporalUnitChrono(Chronology chrono) {
LocalDate refDate = LocalDate.of(2013, 1, 1);
ChronoLocalDate date = chrono.date(refDate);
for (Chronology[] clist : data_of_calendars()) {
Chronology chrono2 = clist[0];
ChronoLocalDate date2 = chrono2.date(refDate);
TemporalUnit adjuster = new FixedTemporalUnit(date2);
if (chrono != chrono2) {
try {
date.minus(1, adjuster);
Assert.fail("TemporalUnit.doAdd minus should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass());
} catch (ClassCastException cce) {
// Expected exception; not an error
}
} else {
// Same chronology,
ChronoLocalDate result = date.minus(1, adjuster);
assertEquals(result, date2, "WithAdjuster failed to replace date");
}
}
}
use of java.time.temporal.TemporalUnit in project perun by CESNET.
the class GroupsManagerBlImpl method extendForStaticDate.
/**
* Returns localDate extended to given date
*
* @param localDate localDate
* @param period period
* @param membershipExpirationRules rules used in check
* @param membershipExpirationAttribute attributes used to check
* @param member member that is checked
* @param group group that is checked
* @return localDate localDate extended to given date
* @throws InternalErrorException internal error
* @throws ExtendMembershipException when cannot extend
*/
private LocalDate extendForStaticDate(LocalDate localDate, String period, LinkedHashMap<String, String> membershipExpirationRules, Attribute membershipExpirationAttribute, Member member, Group group) throws ExtendMembershipException {
// Parse date
Pattern p = Pattern.compile("([0-9]+).([0-9]+).");
Matcher m = p.matcher(period);
if (!m.matches()) {
throw new InternalErrorException("Wrong format of period in Group membershipExpirationRules attribute. Period: " + period);
}
localDate = Utils.getClosestExpirationFromStaticDate(m);
// Is there a grace period?
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName) != null) {
String gracePeriod = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName);
// If the extension is requested in period-gracePeriod then extend to next period
// Get the value of the grace period
p = Pattern.compile("([0-9]+)([dmy]?)");
m = p.matcher(gracePeriod);
if (m.matches()) {
Pair<Integer, TemporalUnit> amountField = Utils.prepareGracePeriodDate(m);
LocalDate gracePeriodDate = localDate.minus(amountField.getLeft(), amountField.getRight());
// Check if we are in grace period
if (gracePeriodDate.isBefore(LocalDate.now())) {
// We are in grace period, so extend to the next period
localDate = localDate.plusYears(1);
}
// If we do not need to set the attribute value, only check if the current member's expiration time is not in grace period
if (membershipExpirationAttribute.getValue() != null) {
LocalDate currentMemberExpirationDate = LocalDate.parse((String) membershipExpirationAttribute.getValue()).minus(amountField.getLeft(), amountField.getRight());
// if today is before that time, user can extend his period
if (currentMemberExpirationDate.isAfter(LocalDate.now())) {
throw new ExtendMembershipException(ExtendMembershipException.Reason.OUTSIDEEXTENSIONPERIOD, (String) membershipExpirationAttribute.getValue(), "Member " + member + " cannot extend because we are outside grace period for GROUP id " + group.getId() + ".");
}
}
}
}
return localDate;
}
use of java.time.temporal.TemporalUnit in project perun by CESNET.
the class UtilsIntegrationTest method prepareGracePeriodDate.
@Test
public void prepareGracePeriodDate() {
System.out.println("Utils.prepareGracePeriodDate");
String gracePeriod = "5d";
Pattern p = Pattern.compile("([0-9]+)([dmy]?)");
Matcher m = p.matcher(gracePeriod);
Pair<Integer, TemporalUnit> fieldAmount = Utils.prepareGracePeriodDate(m);
assertEquals(new Integer(5), fieldAmount.getLeft());
assertEquals(ChronoUnit.DAYS, fieldAmount.getRight());
}
use of java.time.temporal.TemporalUnit in project perun by CESNET.
the class GroupsManagerBlImpl method isMemberInGracePeriod.
/**
* Return true if member is in grace period. If grace period is not set return always true.
* If member has not expiration date return always true.
*
* @param membershipExpirationRules
* @param membershipExpiration
* @return true if member is in grace period. Be carefull about special cases - read method description.
* @throws InternalErrorException
*/
private boolean isMemberInGracePeriod(Map<String, String> membershipExpirationRules, String membershipExpiration) {
// Is a grace period set?
if (membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName) == null) {
// If not grace period is infinite
return true;
}
// does member have expiration date?
if (membershipExpiration == null) {
// if not grace period is infinite
return true;
}
String gracePeriod = membershipExpirationRules.get(AbstractMembershipExpirationRulesModule.membershipGracePeriodKeyName);
// If the extension is requested in period-gracePeriod then extend to next period
Pattern p = Pattern.compile("([0-9]+)([dmy]?)");
Matcher m = p.matcher(gracePeriod);
if (!m.matches()) {
throw new InternalErrorException("Wrong format of gracePeriod in VO membershipExpirationRules attribute. gracePeriod: " + gracePeriod);
}
int amount = Integer.valueOf(m.group(1));
TemporalUnit gracePeriodTimeUnit;
String dmyString = m.group(2);
switch(dmyString) {
case "d":
gracePeriodTimeUnit = ChronoUnit.DAYS;
break;
case "m":
gracePeriodTimeUnit = ChronoUnit.MONTHS;
break;
case "y":
gracePeriodTimeUnit = ChronoUnit.YEARS;
break;
default:
throw new InternalErrorException("Wrong format of gracePeriod in VO membershipExpirationRules attribute. gracePeriod: " + gracePeriod);
}
LocalDate beginOfGracePeriod = LocalDate.parse(membershipExpiration).minus(amount, gracePeriodTimeUnit);
if (beginOfGracePeriod.isBefore(LocalDate.now())) {
return true;
}
return false;
}
Aggregations