/** * Creates a period from the given duration and end point. * * @param duration the duration of the interval, null means zero-length * @param endInstant the interval end, null means now * @param type which set of fields this period supports, null means standard */ protected BasePeriod(ReadableDuration duration, ReadableInstant endInstant, PeriodType type) { super(); type = checkPeriodType(type); long durationMillis = DateTimeUtils.getDurationMillis(duration); long endMillis = DateTimeUtils.getInstantMillis(endInstant); long startMillis = FieldUtils.safeSubtract(endMillis, durationMillis); Chronology chrono = DateTimeUtils.getInstantChronology(endInstant); iType = type; iValues = chrono.get(this, startMillis, endMillis); }
/** * Adds the value of a field in this period. * * @param values the array of values to update * @param field the field to set * @param value the value to set * @throws IllegalArgumentException if field is is null or not supported. */ protected void addFieldInto(int[] values, DurationFieldType field, int value) { int index = indexOf(field); if (index == -1) { if (value != 0 || field == null) { throw new IllegalArgumentException("Period does not support field '" + field + "'"); } } else { values[index] = FieldUtils.safeAdd(values[index], value); } }
/** * Compares this object with the specified object for equality based on the millisecond instant, * chronology and time zone. * * <p>Two objects which represent the same instant in time, but are in different time zones (based * on time zone id), will be considered to be different. Only two objects with the same {@link * DateTimeZone}, {@link Chronology} and instant are equal. * * <p>See {@link #isEqual(ReadableInstant)} for an equals method that ignores the Chronology and * time zone. * * <p>All ReadableInstant instances are accepted. * * @param readableInstant a readable instant to check against * @return true if millisecond and chronology are equal, false if not or the instant is null or of * an incorrect type */ public boolean equals(Object readableInstant) { // must be to fulfil ReadableInstant contract if (this == readableInstant) { return true; } if (readableInstant instanceof ReadableInstant == false) { return false; } ReadableInstant otherInstant = (ReadableInstant) readableInstant; return getMillis() == otherInstant.getMillis() && FieldUtils.equals(getChronology(), otherInstant.getChronology()); }
/** * Adds the fields from another period. * * @param values the array of values to update * @param period the period to add from, not null * @return the updated values * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected int[] addPeriodInto(int[] values, ReadablePeriod period) { for (int i = 0, isize = period.size(); i < isize; i++) { DurationFieldType type = period.getFieldType(i); int value = period.getValue(i); if (value != 0) { int index = indexOf(type); if (index == -1) { throw new IllegalArgumentException( "Period does not support field '" + type.getName() + "'"); } else { values[index] = FieldUtils.safeAdd(getValue(index), value); } } } return values; }
/** * Adds a duration to this instant specifying how many times to add. * * <p>This will typically change the value of most fields. * * @param duration the duration to add, null means add zero * @param scalar direction and amount to add, which may be negative * @throws ArithmeticException if the result exceeds the capacity of the instant */ public void add(ReadableDuration duration, int scalar) { if (duration != null) { add(FieldUtils.safeMultiply(duration.getMillis(), scalar)); } }
/** * Add an amount of time to the datetime. * * @param duration the millis to add * @throws ArithmeticException if the result exceeds the capacity of the instant */ public void add(long duration) { setMillis(FieldUtils.safeAdd(getMillis(), duration)); // set via this class not super }
/** * Returns a new instance with the years value negated. * * @return the new period with a negated value * @throws ArithmeticException if the result overflows an int */ public Years negated() { return Years.years(FieldUtils.safeNegate(getValue())); }
/** * Returns a new instance with the years multiplied by the specified scalar. * * <p>This instance is immutable and unaffected by this method call. * * @param scalar the amount to multiply by, may be negative * @return the new period multiplied by the specified scalar * @throws ArithmeticException if the result overflows an int */ public Years multipliedBy(int scalar) { return Years.years(FieldUtils.safeMultiply(getValue(), scalar)); }
/** * Returns a new instance with the specified number of years taken away. * * <p>This instance is immutable and unaffected by this method call. * * @param years the amount of years to take away, may be negative * @return the new period minus the specified number of years * @throws ArithmeticException if the result overflows an int */ public Years minus(int years) { return plus(FieldUtils.safeNegate(years)); }
/** * Returns a new instance with the specified number of years added. * * <p>This instance is immutable and unaffected by this method call. * * @param years the amount of years to add, may be negative * @return the new period plus the specified number of years * @throws ArithmeticException if the result overflows an int */ public Years plus(int years) { if (years == 0) { return this; } return Years.years(FieldUtils.safeAdd(getValue(), years)); }