AccountingChronologyBuilder.java

  1. /*
  2.  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
  3.  *
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  *  * Redistributions of source code must retain the above copyright notice,
  10.  *    this list of conditions and the following disclaimer.
  11.  *
  12.  *  * Redistributions in binary form must reproduce the above copyright notice,
  13.  *    this list of conditions and the following disclaimer in the documentation
  14.  *    and/or other materials provided with the distribution.
  15.  *
  16.  *  * Neither the name of JSR-310 nor the names of its contributors
  17.  *    may be used to endorse or promote products derived from this software
  18.  *    without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32. package org.threeten.extra.chrono;

  33. import java.time.DateTimeException;
  34. import java.time.DayOfWeek;
  35. import java.time.Month;

  36. /**
  37.  * Builder to create Accounting calendars.
  38.  * <p>
  39.  * Accounting calendars require setup before use, given how they are used.
  40.  * The following information is required:
  41.  * <ul>
  42.  * <li>ending day-of-week - The day-of-week on which a given accounting year ends.
  43.  * <li>last-in-month vs. nearest-end-of-month - Whether the ending day-of-week is the last in the month,
  44.  * or the nearest to the end of the month (will sometimes be in the <i>next</i> month.
  45.  * <li>month end - Which Gregorian/ISO end-of-month the year ends in/is nearest to.
  46.  * <li>year division - How many 'months' (periods) to divide the accounting year into,
  47.  * and how many weeks are in each.
  48.  * <li>leap-week month - Which month will have the leap 'week' added to it.
  49.  * In practice this is probably the last one, but this does not seem to be required.
  50.  * <li>year start/end offset - Whether the fiscal year starts or ends in the similarly numbered ISO year.
  51.  * If nearest-end-of-month is set and the ending month is December, the effective offset will shift over time.
  52.  * </ul>
  53.  * <p>
  54.  * There are approximately 7 x 2 x 12 x 4 x 12/13 x 2 = 8064 combinations.
  55.  *
  56.  * <h3>Implementation Requirements</h3>
  57.  * This class is a mutable builder suitable for use from a single thread.
  58.  */
  59. public final class AccountingChronologyBuilder {

  60.     /**
  61.      * The day of the week on which a given Accounting year ends.
  62.      */
  63.     private DayOfWeek endsOn;
  64.     /**
  65.      * Whether the calendar ends in the last week of a given Gregorian/ISO month,
  66.      * or nearest to the last day of the month (will sometimes be in the next month).
  67.      */
  68.     private boolean inLastWeek;
  69.     /**
  70.      * Which Gregorian/ISO end-of-month the year ends in/is nearest to.
  71.      */
  72.     private Month end;
  73.     /**
  74.      * How to divide an accounting year.
  75.      */
  76.     private AccountingYearDivision division;
  77.     /**
  78.      * The month which will have the leap-week added.
  79.      */
  80.     private int leapWeekInMonth;

  81.     /**
  82.      * The offset to apply to the year.
  83.      */
  84.     private int yearOffset;

  85.     /**
  86.      * Constructs a new instance of the builder.
  87.      */
  88.     public AccountingChronologyBuilder() {
  89.         // Nothing to setup in the constructor.
  90.     }

  91.     /**
  92.      * Sets the day-of-week on which a given Accounting calendar year ends.
  93.      *
  94.      * @param endsOn The day-of-week on which a given Accounting calendar year ends.
  95.      *
  96.      * @return this, for chaining, not null.
  97.      */
  98.     public AccountingChronologyBuilder endsOn(DayOfWeek endsOn) {
  99.         this.endsOn = endsOn;
  100.         return this;
  101.     }

  102.     /**
  103.      * Sets the Gregorian/ISO month-end which a given Accounting calendar year ends nearest to.
  104.      * Calendars setup this way will occasionally end in the start of the <i>next</i> month.
  105.      * For example, for July, the month ends on any day from July 28th to August 3rd.
  106.      *
  107.      * @param end The Gregorian/ISO month-end a given Accounting calendar year ends nearest to.
  108.      *
  109.      * @return this, for chaining, not null.
  110.      */
  111.     public AccountingChronologyBuilder nearestEndOf(Month end) {
  112.         this.inLastWeek = false;
  113.         this.end = end;
  114.         return this;
  115.     }

  116.     /**
  117.      * Sets the Gregorian/ISO month-end in which a given Accounting calendar year ends.
  118.      * Calendars setup this way will always end in the last week of the given month.
  119.      * For example, for July, the month ends on any day from July 25th to July 31st.
  120.      *
  121.      * @param end The Gregorian/ISO month-end a given Accounting calendar year ends in the last week of.
  122.      *
  123.      * @return this, for chaining, not null.
  124.      */
  125.     public AccountingChronologyBuilder inLastWeekOf(Month end) {
  126.         this.inLastWeek = true;
  127.         this.end = end;
  128.         return this;
  129.     }

  130.     /**
  131.      * Sets how a given Accounting year will be divided.
  132.      *
  133.      * @param division How to divide a given calendar year.
  134.      *
  135.      * @return this, for chaining, not null.
  136.      */
  137.     public AccountingChronologyBuilder withDivision(AccountingYearDivision division) {
  138.         this.division = division;
  139.         return this;
  140.     }

  141.     /**
  142.      * Sets the month in which the leap-week occurs.
  143.      *
  144.      * @param leapWeekInMonth The month in which the leap-week occurs.
  145.      *
  146.      * @return this, for chaining, not null.
  147.      */
  148.     public AccountingChronologyBuilder leapWeekInMonth(int leapWeekInMonth) {
  149.         this.leapWeekInMonth = leapWeekInMonth;
  150.         return this;
  151.     }

  152.     /**
  153.      * Sets the proleptic accounting year to end in the matching Iso year.
  154.      *
  155.      * @return this, for chaining, not null.
  156.      */
  157.     public AccountingChronologyBuilder accountingYearEndsInIsoYear() {
  158.         this.yearOffset = 0;
  159.         return this;
  160.     }


  161.     /**
  162.      * Sets the proleptic accounting year to start in the matching Iso year.
  163.      *
  164.      * @return this, for chaining, not null.
  165.      */
  166.     public AccountingChronologyBuilder accountingYearStartsInIsoYear() {
  167.         this.yearOffset = 1;
  168.         return this;
  169.     }

  170.     /**
  171.      * Completes this builder by creating the {@code AccountingChronology}.
  172.      *
  173.      * @return the created chronology, not null.
  174.      * @throws DateTimeException if the chronology cannot be built.
  175.      */
  176.     public AccountingChronology toChronology() {
  177.         return AccountingChronology.create(endsOn, end, inLastWeek, division, leapWeekInMonth, yearOffset);
  178.     }

  179. }