List of usage examples for java.time DayOfWeek equals
public boolean equals(Object obj)
From source file:squash.booking.lambdas.core.RuleManager.java
@Override public Optional<BookingRule> addRuleExclusion(String dateToExclude, BookingRule bookingRuleToAddExclusionTo, boolean isSquashServiceUserCall) throws Exception { if (!initialised) { throw new IllegalStateException("The rule manager has not been initialised"); }// w w w . j a v a 2 s .c o m lifecycleManager.throwIfOperationInvalidForCurrentLifecycleState(false, isSquashServiceUserCall); logger.log("About to add exclusion for " + dateToExclude + " to booking rule: " + bookingRuleToAddExclusionTo.toString()); // We retry the addition of the exclusion if necessary if we get a // ConditionalCheckFailed exception, i.e. if someone else modifies // the database between us reading and writing it. return RetryHelper.DoWithRetries((ThrowingSupplier<Optional<BookingRule>>) (() -> { ImmutablePair<Optional<Integer>, Set<BookingRule>> versionedBookingRules = getVersionedBookingRules(); Set<BookingRule> existingBookingRules = versionedBookingRules.right; // Check the BookingRule we're adding the exclusion to still // exists Optional<BookingRule> existingRule = existingBookingRules.stream() .filter(rule -> rule.equals(bookingRuleToAddExclusionTo)).findFirst(); if (!existingRule.isPresent()) { logger.log("Trying to add an exclusion to a booking rule that does not exist."); throw new Exception("Booking rule exclusion addition failed"); } // Check rule is recurring - we cannot add exclusions to // non-recurring rules if (!existingRule.get().getIsRecurring()) { logger.log("Trying to add an exclusion to a non-recurring booking rule."); throw new Exception("Booking rule exclusion addition failed"); } // Check that the rule exclusion we're creating does not exist // already. if (ArrayUtils.contains(existingRule.get().getDatesToExclude(), dateToExclude)) { logger.log("An identical booking rule exclusion exists already - so quitting early"); return Optional.empty(); } // Check the exclusion is for the right day of the week. DayOfWeek dayToExclude = dayOfWeekFromDate(dateToExclude); DayOfWeek dayOfBookingRule = dayOfWeekFromDate(existingRule.get().getBooking().getDate()); if (!dayToExclude.equals(dayOfBookingRule)) { logger.log("Exclusion being added and target booking rule are for different days of the week."); throw new Exception("Booking rule exclusion addition failed"); } // Check it is not in the past, relative to now, or to the // Booking rule start date. Date bookingRuleStartDate = new SimpleDateFormat("yyyy-MM-dd") .parse(existingRule.get().getBooking().getDate()); Date excludedDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateToExclude); if (excludedDate.before(bookingRuleStartDate)) { logger.log("Exclusion being added is before target booking rule start date."); throw new Exception("Booking rule exclusion addition failed"); } if (excludedDate.before(new SimpleDateFormat("yyyy-MM-dd") .parse(getCurrentLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))))) { logger.log("Exclusion being added is in the past."); throw new Exception("Booking rule exclusion addition failed"); } // Check we'll not exceed the maximum number of dates to exclude // (this limit is here as SimpleDB has a 1024-byte limit for // attribute // values). Set<String> datesToExclude = Sets.newHashSet(bookingRuleToAddExclusionTo.getDatesToExclude()); if (datesToExclude.size() >= maxNumberOfDatesToExclude) { logger.log("The maximum number of booking rule exclusions(" + maxNumberOfDatesToExclude + ") exists already."); throw new Exception("Booking rule exclusion addition failed - too many exclusions"); } logger.log("Proceeding to add the new rule exclusion"); datesToExclude.add(dateToExclude); String attributeName = getAttributeNameFromBookingRule(bookingRuleToAddExclusionTo); String attributeValue = StringUtils.join(datesToExclude, ","); logger.log("ItemName: " + ruleItemName); logger.log("AttributeName: " + attributeName); logger.log("AttributeValue: " + attributeValue); ReplaceableAttribute bookingRuleAttribute = new ReplaceableAttribute(); bookingRuleAttribute.setName(attributeName); bookingRuleAttribute.setValue(attributeValue); bookingRuleAttribute.setReplace(true); optimisticPersister.put(ruleItemName, versionedBookingRules.left, bookingRuleAttribute); BookingRule updatedBookingRule = new BookingRule(bookingRuleToAddExclusionTo); updatedBookingRule.setDatesToExclude(datesToExclude.toArray(new String[datesToExclude.size()])); logger.log("Added new rule exclusion"); return Optional.of(updatedBookingRule); }), Exception.class, Optional.of("Database put failed - conditional check failed"), logger); }