List of usage examples for org.joda.time LocalDate compareTo
public int compareTo(ReadablePartial partial)
From source file:org.killbill.billing.invoice.tree.NodeInterval.java
License:Apache License
/** * Return the first node satisfying the date and match callback. * * @param targetDate target date for possible match nodes whose interval comprises that date * @param callback custom logic to decide if a given node is a match * @return the found node or null if there is nothing. *//*from ww w.j a va 2 s. co m*/ public NodeInterval findNode(final LocalDate targetDate, final SearchCallback callback) { Preconditions.checkNotNull(callback); Preconditions.checkNotNull(targetDate); if (targetDate.compareTo(getStart()) < 0 || targetDate.compareTo(getEnd()) > 0) { return null; } NodeInterval curChild = leftChild; while (curChild != null) { if (curChild.getStart().compareTo(targetDate) <= 0 && curChild.getEnd().compareTo(targetDate) >= 0) { if (callback.isMatch(curChild)) { return curChild; } NodeInterval result = curChild.findNode(targetDate, callback); if (result != null) { return result; } } curChild = curChild.getRightSibling(); } return null; }
From source file:org.killbill.billing.invoice.tree.SubscriptionItemTree.java
License:Apache License
private void checkItemsListState(final List<InvoiceItem> orderedList) { LocalDate prevRecurringEndDate = null; LocalDate prevRepairEndDate = null; for (InvoiceItem cur : orderedList) { switch (cur.getInvoiceItemType()) { case FIXED: break; case RECURRING: if (prevRecurringEndDate != null) { Preconditions.checkState(prevRecurringEndDate.compareTo(cur.getStartDate()) <= 0); }//from w ww .j a va 2s .co m prevRecurringEndDate = cur.getEndDate(); break; case REPAIR_ADJ: if (prevRepairEndDate != null) { Preconditions.checkState(prevRepairEndDate.compareTo(cur.getStartDate()) <= 0); } prevRepairEndDate = cur.getEndDate(); break; default: Preconditions.checkState(false, "Unexpected item type " + cur.getInvoiceItemType()); } } }
From source file:org.killbill.billing.invoice.usage.ContiguousIntervalConsumableInArrear.java
License:Apache License
/** * Builds the transitionTimes associated to that usage section. Those are determined based on billing events for when to start and when to stop, * the per usage billingPeriod and finally the targetDate. * <p/>//from w ww .j av a2 s. c om * Those transition dates define the well defined billing granularity periods that should be billed for that specific usage section. * * @param closedInterval whether there was a last billing event referencing the usage section or whether this is ongoing and * then targetDate will define the endDate. * @return */ public ContiguousIntervalConsumableInArrear build(final boolean closedInterval) { Preconditions.checkState(!isBuilt.get()); Preconditions.checkState( (!closedInterval && billingEvents.size() >= 1) || (closedInterval && billingEvents.size() >= 2)); final LocalDate startDate = new LocalDate(billingEvents.get(0).getEffectiveDate(), getAccountTimeZone()); if (targetDate.isBefore(startDate)) { return this; } final LocalDate endDate = closedInterval ? new LocalDate(billingEvents.get(billingEvents.size() - 1).getEffectiveDate(), getAccountTimeZone()) : targetDate; final BillingIntervalDetail bid = new BillingIntervalDetail(startDate, endDate, targetDate, getBCD(), usage.getBillingPeriod()); int numberOfPeriod = 0; // First billingCycleDate prior startDate LocalDate nextBillCycleDate = bid.getFutureBillingDateFor(numberOfPeriod); if (startDate.compareTo(rawUsageStartDate) >= 0) { transitionTimes.add(startDate); } while (!nextBillCycleDate.isAfter(endDate)) { if (nextBillCycleDate.isAfter(startDate)) { if (nextBillCycleDate.compareTo(rawUsageStartDate) >= 0) { transitionTimes.add(nextBillCycleDate); } } numberOfPeriod++; nextBillCycleDate = bid.getFutureBillingDateFor(numberOfPeriod); } isBuilt.set(true); return this; }
From source file:org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.java
License:Apache License
/** * Builds the transitionTimes associated to that usage section. Those are determined based on billing events for when to start and when to stop, * the per usage billingPeriod and finally the targetDate. * <p/>/*w w w .j a v a2 s. c om*/ * Those transition dates define the well defined billing granularity periods that should be billed for that specific usage section. * * @param closedInterval whether there was a last billing event referencing the usage section or whether this is ongoing and * then targetDate will define the endDate. */ public ContiguousIntervalUsageInArrear build(final boolean closedInterval) { Preconditions.checkState(!isBuilt.get()); Preconditions.checkState( (!closedInterval && billingEvents.size() >= 1) || (closedInterval && billingEvents.size() >= 2)); final LocalDate startDate = internalTenantContext.toLocalDate(billingEvents.get(0).getEffectiveDate()); if (targetDate.isBefore(startDate)) { return this; } final LocalDate endDate = closedInterval ? internalTenantContext.toLocalDate(billingEvents.get(billingEvents.size() - 1).getEffectiveDate()) : targetDate; final BillingIntervalDetail bid = new BillingIntervalDetail(startDate, endDate, targetDate, getBCD(), usage.getBillingPeriod(), usage.getBillingMode()); int numberOfPeriod = 0; // First billingCycleDate prior startDate LocalDate nextBillCycleDate = bid.getFutureBillingDateFor(numberOfPeriod); if (startDate.compareTo(rawUsageStartDate) >= 0) { transitionTimes.add(startDate); } while (!nextBillCycleDate.isAfter(endDate)) { if (nextBillCycleDate.isAfter(startDate)) { if (nextBillCycleDate.compareTo(rawUsageStartDate) >= 0) { transitionTimes.add(nextBillCycleDate); } } numberOfPeriod++; nextBillCycleDate = bid.getFutureBillingDateFor(numberOfPeriod); } if (closedInterval && transitionTimes.size() > 0 && endDate.isAfter(transitionTimes.get(transitionTimes.size() - 1))) { transitionTimes.add(endDate); } isBuilt.set(true); return this; }
From source file:org.killbill.billing.invoice.usage.ContiguousIntervalUsageInArrear.java
License:Apache License
private LocalDate computeNextNotificationDate() { LocalDate result = null; final Iterator<BillingEvent> eventIt = billingEvents.iterator(); BillingEvent nextEvent = eventIt.next(); while (eventIt.hasNext()) { final BillingEvent thisEvent = nextEvent; nextEvent = eventIt.next();/* w w w . j av a2s.c o m*/ final LocalDate startDate = internalTenantContext.toLocalDate(thisEvent.getEffectiveDate()); final LocalDate endDate = internalTenantContext.toLocalDate(nextEvent.getEffectiveDate()); final BillingIntervalDetail bid = new BillingIntervalDetail(startDate, endDate, targetDate, thisEvent.getBillCycleDayLocal(), usage.getBillingPeriod(), BillingMode.IN_ARREAR); final LocalDate nextBillingCycleDate = bid.getNextBillingCycleDate(); result = (result == null || result.compareTo(nextBillingCycleDate) < 0) ? nextBillingCycleDate : result; } final LocalDate startDate = internalTenantContext.toLocalDate(nextEvent.getEffectiveDate()); final BillingIntervalDetail bid = new BillingIntervalDetail(startDate, null, targetDate, nextEvent.getBillCycleDayLocal(), usage.getBillingPeriod(), BillingMode.IN_ARREAR); final LocalDate nextBillingCycleDate = bid.getNextBillingCycleDate(); result = (result == null || result.compareTo(nextBillingCycleDate) < 0) ? nextBillingCycleDate : result; return result; }
From source file:org.killbill.billing.invoice.usage.RawUsageOptimizer.java
License:Apache License
@VisibleForTesting LocalDate getOptimizedRawUsageStartDate(final LocalDate firstEventStartDate, final LocalDate targetDate, final Iterable<InvoiceItem> existingUsageItems, final Map<String, Usage> knownUsage) { if (!existingUsageItems.iterator().hasNext()) { return firstEventStartDate; }/*from ww w .j av a2 s . co m*/ // Extract all usage billing period known in that catalog final Set<BillingPeriod> knownUsageBillingPeriod = ImmutableSet .copyOf(Iterables.transform(knownUsage.values(), new Function<Usage, BillingPeriod>() { @Nullable @Override public BillingPeriod apply(final Usage input) { return input.getBillingPeriod(); } })); // Make sure all usage items are sorted by endDate final List<InvoiceItem> sortedUsageItems = USAGE_ITEM_ORDERING.sortedCopy(existingUsageItems); // Compute an array with one date per BillingPeriod: // If BillingPeriod is never defined in the catalog (no need to look for items), we initialize its value // such that it cannot be chosen // final LocalDate[] perBillingPeriodMostRecentConsumableInArrearItemEndDate = new LocalDate[BillingPeriod .values().length - 1]; // Exclude the NO_BILLING_PERIOD int idx = 0; for (BillingPeriod bp : BillingPeriod.values()) { if (bp != BillingPeriod.NO_BILLING_PERIOD) { final LocalDate makerDateThanCannotBeChosenAsTheMinOfAllDates = targetDate .plusMonths(config.getMaxRawUsagePreviousPeriod() * bp.getNumberOfMonths()); perBillingPeriodMostRecentConsumableInArrearItemEndDate[idx++] = (knownUsageBillingPeriod .contains(bp)) ? null : makerDateThanCannotBeChosenAsTheMinOfAllDates; } } final ListIterator<InvoiceItem> iterator = sortedUsageItems.listIterator(sortedUsageItems.size()); while (iterator.hasPrevious()) { final InvoiceItem previous = iterator.previous(); Preconditions.checkState(previous instanceof UsageInvoiceItem); final UsageInvoiceItem item = (UsageInvoiceItem) previous; final Usage usage = knownUsage.get(item.getUsageName()); if (perBillingPeriodMostRecentConsumableInArrearItemEndDate[usage.getBillingPeriod() .ordinal()] == null) { perBillingPeriodMostRecentConsumableInArrearItemEndDate[usage.getBillingPeriod().ordinal()] = item .getEndDate(); if (!containsNullEntries(perBillingPeriodMostRecentConsumableInArrearItemEndDate)) { break; } } } // Extract the min from all the dates LocalDate targetStartDate = null; idx = 0; for (BillingPeriod bp : BillingPeriod.values()) { if (bp != BillingPeriod.NO_BILLING_PERIOD) { final LocalDate tmp = perBillingPeriodMostRecentConsumableInArrearItemEndDate[idx]; final LocalDate targetBillingPeriodDate = tmp != null ? tmp.minusMonths(config.getMaxRawUsagePreviousPeriod() * bp.getNumberOfMonths()) : null; if (targetStartDate == null || (targetBillingPeriodDate != null && targetBillingPeriodDate.compareTo(targetStartDate) < 0)) { targetStartDate = targetBillingPeriodDate; } idx++; } } final LocalDate result = targetStartDate.compareTo(firstEventStartDate) > 0 ? targetStartDate : firstEventStartDate; return result; }
From source file:org.killbill.billing.plugin.analytics.reports.analysis.Smoother.java
License:Apache License
private List<XY> smooth(final List<XY> inputData, final Function<XY, LocalDate> truncator) { final List<XY> smoothedData = new LinkedList<XY>(); LocalDate currentTruncatedDate = truncator.apply(inputData.get(0)); Float accumulator = (float) 0; int accumulatorSize = 0; for (final XY xy : inputData) { final LocalDate zeTruncatedDate = truncator.apply(xy); //noinspection ConstantConditions if (zeTruncatedDate.compareTo(currentTruncatedDate) != 0) { smoothedData.add(new XY(currentTruncatedDate, computeSmoothedValue(accumulator, accumulatorSize))); accumulator = (float) 0; accumulatorSize = 0;//from w w w. j a va 2 s .co m } accumulator += xy.getY(); accumulatorSize++; currentTruncatedDate = zeTruncatedDate; } return smoothedData; }
From source file:org.killbill.billing.subscription.api.SubscriptionApiBase.java
License:Apache License
private DateTime getDryRunEffectiveDate(@Nullable final LocalDate inputDate, final SubscriptionBase subscription, final InternalTenantContext context) { if (inputDate == null) { return null; }/*from w ww . j ava2s. c o m*/ // We first use context account reference time to get a candidate) final DateTime tmp = context.toUTCDateTime(inputDate); // If we realize that the candidate is on the same LocalDate boundary as the subscription startDate but a bit prior we correct it to avoid weird things down the line if (inputDate.compareTo(context.toLocalDate(subscription.getStartDate())) == 0 && tmp.compareTo(subscription.getStartDate()) < 0) { return subscription.getStartDate(); } else { return tmp; } }
From source file:org.kuali.kpme.core.accrualcategory.rule.service.AccrualCategoryRuleServiceImpl.java
License:Educational Community License
public AccrualCategoryRule getAccrualCategoryRuleForDate(AccrualCategory accrualCategory, LocalDate currentDate, LocalDate serviceDate) {//from w w w . jav a 2 s .c om if (serviceDate == null) { return null; } List<AccrualCategoryRule> acrList = this .getActiveAccrualCategoryRules(accrualCategory.getLmAccrualCategoryId()); for (AccrualCategoryRule acr : acrList) { String uot = acr.getServiceUnitOfTime(); int startTime = acr.getStart().intValue(); int endTime = acr.getEnd().intValue(); LocalDate startDate = serviceDate; LocalDate endDate = serviceDate; if (uot.equals("M")) { // monthly startDate = startDate.plusMonths(startTime); endDate = endDate.plusMonths(endTime).minusDays(1); } else if (uot.endsWith("Y")) { // yearly startDate = startDate.plusYears(startTime); endDate = endDate.plusYears(endTime).minusDays(1); } // max days in months differ, if the date is bigger than the max day, set it to the max day of the month if (startDate.getDayOfMonth() > startDate.dayOfMonth().getMaximumValue()) { startDate = startDate.withDayOfMonth(startDate.dayOfMonth().getMaximumValue()); } if (endDate.getDayOfMonth() > endDate.dayOfMonth().getMaximumValue()) { endDate = endDate.withDayOfMonth(endDate.dayOfMonth().getMaximumValue()); } if (currentDate.compareTo(startDate) >= 0 && currentDate.compareTo(endDate) <= 0) { return acr; } } return null; }
From source file:org.kuali.kpme.core.department.validation.DepartmentValidation.java
License:Educational Community License
boolean validateRolePresent(List<DepartmentPrincipalRoleMemberBo> roleMembers, LocalDate effectiveDate) { boolean valid = true; for (ListIterator<DepartmentPrincipalRoleMemberBo> iterator = roleMembers.listIterator(); iterator .hasNext();) {//from w w w. j a va 2s . c om int index = iterator.nextIndex(); RoleMemberBo roleMember = iterator.next(); Role role = KimApiServiceLocator.getRoleService().getRole(roleMember.getRoleId()); valid &= roleMember.isActive(); if (StringUtils.equals(role.getName(), KPMERole.TIME_DEPARTMENT_ADMINISTRATOR.getRoleName()) || StringUtils.equals(role.getName(), KPMERole.LEAVE_DEPARTMENT_ADMINISTRATOR.getRoleName())) { String prefix = "roleMembers[" + index + "]."; if (roleMember.getActiveToDateValue() != null) { if (effectiveDate.compareTo(roleMember.getActiveToDate().toLocalDate()) >= 0 || roleMember .getActiveFromDateValue().compareTo(roleMember.getActiveToDateValue()) >= 0) { this.putFieldError(prefix + "expirationDate", "error.role.expiration"); valid = false; } else if (TKUtils.getDaysBetween(roleMember.getActiveFromDate().toLocalDate(), roleMember.getActiveToDate().toLocalDate()) > 180) { this.putFieldError(prefix + "expirationDate", "error.role.expiration.duration"); valid = false; } } } } if (!valid) { this.putGlobalError("role.required"); } return valid; }