UNPKG

7.39 kBJavaScriptView Raw
1import {MAX_SAFE_INTEGER, MIN_SAFE_INTEGER} from '../MathUtil';
2import {requireInstance, requireNonNull} from '../assert';
3import {TemporalAdjuster} from './TemporalAdjuster';
4import {TemporalAmount} from './TemporalAmount';
5import {TemporalUnit} from './TemporalUnit';
6import {Temporal} from './Temporal';
7
8export class DefaultInterfaceTemporal extends Temporal {
9 /**
10 * Returns an adjusted object of the same type as this object with the adjustment made.
11 * This adjusts this date-time according to the rules of the specified adjuster. A simple adjuster might simply set the one of the fields, such as the year field. A more complex adjuster might set the date to the last day of the month. A selection of common adjustments is provided in {@link TemporalAdjusters}. These include finding the "last day of the month" and "next Wednesday". The adjuster is responsible for handling special cases, such as the varying lengths of month and leap years.
12 *
13 * Some example code indicating how and why this method is used:
14 *
15 * <pre>
16 * date = date.with(Month.JULY); // most key classes implement TemporalAdjuster
17 * date = date.with(lastDayOfMonth()); // static import from TemporalAdjusters
18 * date = date.with(next(WEDNESDAY)); // static import from TemporalAdjusters and DayOfWeek
19 * </pre>
20 *
21 * ### Specification for implementors
22 * Implementations must not alter either this object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
23 *
24 * @param {TemporalAdjuster} adjuster - the adjuster to use, not null
25 * @return {Temporal} an object of the same type with the specified adjustment made, not null
26 * @throws DateTimeException - if unable to make the adjustment
27 * @throws ArithmeticException - if numeric overflow occurs
28 */
29 withAdjuster(adjuster) {
30 requireNonNull(adjuster, 'adjuster');
31 requireInstance(adjuster, TemporalAdjuster, 'adjuster');
32 return adjuster.adjustInto(this);
33 }
34
35 /**
36 * Returns an object of the same type as this object with an amount added.
37 * This adjusts this temporal, adding according to the rules of the specified amount. The amount is typically a {@link Period} but may be any other type implementing the {@link TemporalAmount} interface, such as {@link Duration}.
38 *
39 * Some example code indicating how and why this method is used:
40 *
41 * <pre>
42 * date = date.plus(period); // add a Period instance
43 * date = date.plus(duration); // add a Duration instance
44 * date = date.plus(workingDays(6)); // example user-written workingDays method
45 * </pre>
46 *
47 * Note that calling plus followed by minus is not guaranteed to return the same date-time.
48 *
49 * ### Specification for implementors
50 * Implementations must not alter either this object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
51 *
52 * @param {TemporalAmount} amount - the amount to add, not null
53 * @return {Temporal} an object of the same type with the specified adjustment made, not null
54 * @throws DateTimeException - if the addition cannot be made
55 * @throws ArithmeticException - if numeric overflow occurs
56 */
57 plusAmount(amount) {
58 requireNonNull(amount, 'amount');
59 requireInstance(amount, TemporalAmount, 'amount');
60 return amount.addTo(this);
61 }
62
63 /**
64 * Returns an object of the same type as this object with an amount subtracted.
65 * This adjusts this temporal, subtracting according to the rules of the specified amount. The
66 * amount is typically a {@link Period} but may be any other type implementing the {@link TemporalAmount} interface, such as Duration.
67 *
68 * Some example code indicating how and why this method is used:
69 *
70 * <pre>
71 * date = date.minus(period); // subtract a Period instance
72 * date = date.minus(duration); // subtract a Duration instance
73 * date = date.minus(workingDays(6)); // example user-written workingDays method
74 * </pre>
75 *
76 * Note that calling plus followed by minus is not guaranteed to return the same date-time.
77 *
78 * ### Specification for implementors
79 * Implementations must not alter either this object. Instead, an adjusted copy of the original
80 * must be returned. This provides equivalent, safe behavior for immutable and mutable
81 * implementations.
82 *
83 * @param {TemporalAmount} amount - the amount to subtract, not null
84 * @return {Temporal} an object of the same type with the specified adjustment made, not null
85 * @throws DateTimeException - if the subtraction cannot be made
86 * @throws ArithmeticException - if numeric overflow occurs
87 */
88 minusAmount(amount) {
89 requireNonNull(amount, 'amount');
90 requireInstance(amount, TemporalAmount, 'amount');
91 return amount.subtractFrom(this);
92 }
93
94 /**
95 * Returns an object of the same type as this object with the specified period subtracted.
96 * This method returns a new object based on this one with the specified period subtracted. For example, on a {@link LocalDate}, this could be used to subtract a number of years, months or days. The returned object will have the same observable type as this object.
97 *
98 * In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st March, then subtracting one month would be unclear. In cases like this, the field is responsible for resolving the result. Typically it will choose the previous valid date, which would be the last valid day of February in this example.
99 *
100 * If the implementation represents a date-time that has boundaries, such {@link as} LocalTime, then the permitted units must include the boundary unit, but no multiples of the boundary unit. For example, {@link LocalTime} must accept `DAYS` but not `WEEKS` or `MONTHS`.
101 *
102 * ### Specification for implementors
103 * Implementations must behave in a manor equivalent to the default method behavior.
104 * Implementations must not alter either this object or the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
105 *
106 * @param {number} amountToSubtract - the amount of the specified unit to subtract, may be negative
107 * @param {TemporalUnit} unit - the unit of the period to subtract, not null
108 * @return {Temporal} an object of the same type with the specified period subtracted, not null
109 * @throws DateTimeException - if the unit cannot be subtracted
110 * @throws ArithmeticException - if numeric overflow occurs
111 */
112 minusAmountUnit(amountToSubtract, unit) {
113 requireNonNull(amountToSubtract, 'amountToSubtract');
114 requireNonNull(unit, 'unit');
115 requireInstance(unit, TemporalUnit, 'unit');
116 return (amountToSubtract === MIN_SAFE_INTEGER ? this.plusAmountUnit(MAX_SAFE_INTEGER, unit).plusAmountUnit(1, unit) : this.plusAmount(-amountToSubtract, unit));
117 }
118}